Page 1 of 1
"Slow" DirExist
Posted: Mon Oct 26, 2020 12:34 pm
by AMpos
I have made this proc to check if a directory exist or not:
Code: Select all
Procedure.i DirExist(path.s)
If FileSize(path)=-2
ProcedureReturn #True
Else
ProcedureReturn
EndIf
EndProcedure
Also, I have a "Z:" unit (windows) on my computer: it is a shared folder (another computer through a wifi connection), drop on "my computer".
The shared folder is something like "\\computer\files\here\" and Z: is really that. And I want to know if "Z:\\big\" does exist or not, before trying to copy files there.
The problem is that if my DirExist() tries to check for "Z:" existence,
e=DirExist("Z:\\"+"big\") if the shared folder computer is not online, it takes ages to check for it, even crashing the program.
Notice that if the other computer is online, it is fast as expected, even if the folder exist or not.
Re: "Slow" DirExist
Posted: Mon Oct 26, 2020 1:41 pm
by Jac de Lad
Checking if a folder over a network connection exists can take up to 30 seconds on windows. I encountered this problem a long time ago. Also using API or other tricks doesn't speed that up. It is as it is.
One solution I sometimes use is starting the test in a separate thread and terminating it after a given amount if time. Usually if the folder is available the response is pretty fast, just some seconds. If the folder is not available windows takes the whole 30 seconds. So if the thread is still running after 5 seconds it usually is not available.
This is not a foolproof method, but maybe it helps.
Re: "Slow" DirExist
Posted: Mon Oct 26, 2020 2:42 pm
by Marc56us
[...] if the shared folder computer is not online [...]
Old (but fast) method to check if network drive is (still) connected
So in PB code
Code: Select all
Run = RunProgram("cmd", ~"/c net use | grep \"Z:\"", "", #PB_Program_Open | #PB_Program_Read)
If Run
While ProgramRunning(Run)
If AvailableProgramOutput(Run)
Output$ = ReadProgramString(Run)
EndIf
Wend
CloseProgram(Run)
EndIf
; Debug Output$
If Left(Output$, 2) = "OK"
Debug "Z: is connected"
Else
Debug "Network drive is not connected"
EndIf
There is surely an API to do the same thing. Test if it is faster.

Re: "Slow" DirExist
Posted: Mon Oct 26, 2020 4:40 pm
by kpeters58
Just a note on the code and not the described issue:
Your code can be shortened to:
Code: Select all
Procedure.i DirExist(path.s)
ProcedureReturn Bool(FileSize(path) = -2)
EndProcedure
Instead of using such 'mini' procedures, you could consider using a macro instead for speed & simplicity:
Code: Select all
Macro DirExists(Path)
Bool(FileSize(Path) = -2)
EndMacro
Debug DirExists("c:\temp")
Re: "Slow" DirExist
Posted: Mon Oct 26, 2020 7:03 pm
by AMpos
I will give a shot to this "net use..." thing. The real problem is that I don't if the user has selected a normal folder, or a networked one.
My idea was disable the "copy" button if destination wasn't available, but perhaps, give them an error msg after copying error is enough.
Anyway, thank you.
Perhaps I will do this "time limit" option.
Re: "Slow" DirExist
Posted: Tue Oct 27, 2020 9:31 am
by Olli
Connecting a computer to another one provides a publishing of IP addresses. Is it a faster way to check if a external IP is existing ? I suppose. Even if Z: is not linked to this IP, this check could help not to waste a time when there is no IP, so no distant drive.