View Full Version : file_exists confusion
sandyrepope
07-07-2007, 20:02
I've read the help on file_exists but I don't understand what it needs to work. I need my script to check for a file on the disk.
With file_exists do I need a path? If so, what command do I need to use to get the correct path?
I just need more information than the help file gives.
Thanks
Sandy
Petr Schreiber
07-07-2007, 20:27
Hi sandy,
what kind of file do you need to check then when you have no full path ?
If it is some file relative to script directory, you could try:
FILE_EXISTS( APP_SOURCEPATH+"MyFile.txt")
If you could give example I could help better :)
Bye,
Petr
sandyrepope
08-07-2007, 01:58
Petr, tried your line and it worked so I don't have an example. I just wanted the script to check to see if a certain file was on the disk and do some things if it was.
Thanks for the line that I needed!
Sandy
ErosOlmi
08-07-2007, 10:12
Sandy,
I will add some more info into help. Here, as Petr said, the problem seems to be the path (ie file name). FILE_EXISTS need the full path of the file to be checked. If no full path, FILE_EXISTS search in current directory but sometimes you cannot be sure what current directory is, so always better to pass full path names. DIR_GetCurrent will get back the current directory
APP_SourcePath or APP_ScriptPath functions will always return the script path, so this can be a start.
Ciao
Eros
sandyrepope
08-07-2007, 18:29
Here is a sample script using file_exists:
uses "UI"
USES "File"
dim fName as string
dim sFile as long
fName = "testing.txt"
look
function look()
' sFile = file_exists(app_sourcepath + fName)
sFile = file_exists(dir_getcurrent + "\" + fName)
msgbox 0, dir_getcurrent
If sFile = %true then
msgbox 0, "file exists"
else
msgbox 0, "file doesn't exist!"
end if
end function
As you can see... dir_getcurrent doesn't work with file_exists unless there is a backslash ( \ ) placed before the file name. Note: app_sourcepath provides the \.
I've also noticed that in the help, under dir_getcurrent, it talks about the drivename. The entry is very confusing as to what it's talking about.
Thanks
Sandy
ErosOlmi
08-07-2007, 18:53
Ok, I will try to add more info in help. You are right.
A drive name is the name of the disk: C:, D:, E:, ...
Every drive have a Current Directory. In the system there is also a CurrentDrive that is the one taken from dir_getcurrent in case you do not specify any drive name.
I know, it is quite confusing, and that's why a sied to always specify a file with full path.
Anyhow, I will fix dir_getcurrent to return a "\" at the end.
Thanks
Eros