Post by bzcommand on Mar 23, 2020 16:52:31 GMT -5
I just recently downloaded Liberty Basic to give it a whirl, mainly for it's INPUTCSV command. As I don't program much but when I do it's usually something to do with CSV files.
Anyway, I've got this little project going and I'm working with a file that is downloaded form a web browser.
Because windows and web browsers like to automatically add the (1) to a filename if that file already exists in the downloads folder I was my program to check for the existence of the filename I'm working with with the (1) appended on it so I can kill the old file and then rename the (1) to the name it is supposed to be.
Anyway, so I'm starting with an IF fileexist statement. The problem is, all the various examples I've gotten from the web have a problem when the file does NOT exist. The cause an exception error and bomb out of the program. :-(
Here is the code I'm using which works great if the file I'm testing for exists and bombs if the file does not exist. So if the file does not exist I never get to the final "Print cleanit$" line. It bombs out of the program before that print statement. I do get the items in the ELSE section of the if to print so the If test is working.
All the print statements are there for my troubleshooting.
These functions were all from the web, not my doing. I did play with the file exist function a little, but my changes made no difference to my outcomes. :-(
Thanks for any help.
Anyway, I've got this little project going and I'm working with a file that is downloaded form a web browser.
Because windows and web browsers like to automatically add the (1) to a filename if that file already exists in the downloads folder I was my program to check for the existence of the filename I'm working with with the (1) appended on it so I can kill the old file and then rename the (1) to the name it is supposed to be.
Anyway, so I'm starting with an IF fileexist statement. The problem is, all the various examples I've gotten from the web have a problem when the file does NOT exist. The cause an exception error and bomb out of the program. :-(
Here is the code I'm using which works great if the file I'm testing for exists and bombs if the file does not exist. So if the file does not exist I never get to the final "Print cleanit$" line. It bombs out of the program before that print statement. I do get the items in the ELSE section of the if to print so the If test is working.
All the print statements are there for my troubleshooting.
Global False : False = 0
Global True : True = 1
dim info$(0, 0)
' FYI, tried both the above and with (10,10) no diff.
dim sdtickary$(10000,10)
' set the path to the users downloads folder
todownloads$ = GetEnvironmentVariable$("USERPROFILE") + "\Downloads\"
' check for the existence of a duplicated download and fix it
print todownloads$
print todownloads$ + "sdtickets (1).csv"
print todownloads$ + "sdtickets.csv"
toimport$ = "c:\data_import\"
if fileExists(todownloads$ + "sdtickets (1).csv") > 0 then
cleanit$ = todownloads$ + "sdtickets.csv"
changeit$ = todownloads$ + "sdtickets (1).csv"
'presuming the orig filename was there causing the (1) version we delete it
print "cleanit: " + cleanit$
print "changeit: " + changeit$
kill cleanit$
'now we rename the (1) file to the correct orig filename before moving on to processing
name changeit$ as cleanit$
print "Fixed It"
print
else
print
print "no need for an else statement. if the if fails then we process normally"
print
end if
print cleanit$
end
function fileExists(fullPath$)
files pathOnly$(fullPath$), filenameOnly$(fullPath$), info$()
fileExists = val(info$(0, 0))
end function
'return just the directory path from a full file path
function pathOnly$(fullPath$)
pathOnly$ = fullPath$
while right$(pathOnly$, 1) <> "\" and pathOnly$ <> ""
pathOnly$ = left$(pathOnly$, len(pathOnly$)-1)
wend
end function
'return just the filename from a full file path
function filenameOnly$(fullPath$)
pathLength = len(pathOnly$(fullPath$))
filenameOnly$ = right$(fullPath$, len(fullPath$)-pathLength)
end function
function GetEnvironmentVariable$(lpName$)
'get the value of an environment variable
nSize = 1024
[Retry]
lpBuffer$ = space$(nSize)
calldll #kernel32, "GetEnvironmentVariableA", _
lpName$ as ptr, _
lpBuffer$ as ptr, _
nSize as ulong, _
result as ulong
select case
' buffer too small
case result > nSize
nSize = result
goto [Retry]
' variable found
case result > 0
GetEnvironmentVariable$ = left$(lpBuffer$, result)
end select
end function
These functions were all from the web, not my doing. I did play with the file exist function a little, but my changes made no difference to my outcomes. :-(
Thanks for any help.