B4J Question [solved] File Exist on Windows

udg

Expert
Licensed User
Longtime User
Hi all,
today I found some unexpected behaviour and I'd like to hear from you.
I purposedly set right and wrong path for File.Exist, but it seems to work ok in any case.
Here it is my testing
B4X:
DataFolder = DBUtils.getdbfolder("myapp")
Log("DataFolder: "&DataFolder)  'backslashes as expected since we are on a Windows PC
'test for a file in a sub-folder. Returns True altough a forwarding slash is used
Log("Image exists : " & File.Exists(DataFolder, "images/test.png"))
'another test, again returns True
Log("Image exists : " & File.Exists(DataFolder&"\images", "test.png"))
Two considerations:
- it seems that we can place the sub-folder reference indifferently in the dir or the filename parameter
- it seems that something automatically corrects slash/backslashes

ps: I didn't test it on a Linux machine so I can't say if on that it behaves differently.
 
D

Deleted member 103

Guest
Have you tried this yet?
B4X:
    Log("Image exists : " & File.Exists(File.DirApp, "images\test.png"))
    'another test, again returns True
    Log("Image exists : " & File.Exists(File.DirApp & "\images", "test.png"))
 
Upvote 0

udg

Expert
Licensed User
Longtime User
I tried different combinations. Feel free to experiment more with the attached test program.
BTW, it returns TRUE on every test log .

ps: even CopyAsync shows the same behaviour (slashes ok for a Windows machine path)

Edit: I'm redading that Windows accepts both slash and backslash as a directory separator
 

Attachments

  • TestFE.zip
    1.3 KB · Views: 298
Last edited:
Upvote 0

sorex

Expert
Licensed User
Longtime User
I believe I had one case where I left the filename just empty and put it into the directory value.

it just seems to merge both with (back)slash correction.

the only issue I got so far with file operations was with long folder names that got chopped off to myfold~1 which made it hard to remove unneeded parts of the path.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
I'm going to mark the thread "[solved]" since from my recent readings it seems that Windows takes care of the slash/backslash point (it accepts both formats).
That leads to the conclusion that's best to always use regular slash (forwarding) since it will work both for Windows and Linux (Mac too, I believe).
As far as the proper place to put a sub-folder, well, logically I should place any dir-related references with the first parameter, leaving just the eventual filename and extension for the second one.
So, final version of my test, should be:
B4X:
Log("Image exists : " & File.Exists(DataFolder&"/images", "test.png"))

udg
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
If you want to know the correct separator for the os system, use
B4X:
log(GetSystemProperty("file.separator",""))
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…