"Slow" DirExist

Just starting out? Need help? Post your questions and find answers here.
AMpos
Enthusiast
Enthusiast
Posts: 128
Joined: Fri Jun 05, 2020 12:47 am

"Slow" DirExist

Post 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.
User avatar
Jac de Lad
Enthusiast
Enthusiast
Posts: 106
Joined: Wed Jul 15, 2020 7:10 am
Contact:

Re: "Slow" DirExist

Post 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.
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: "Slow" DirExist

Post by Marc56us »

[...] if the shared folder computer is not online [...]
Old (but fast) method to check if network drive is (still) connected

Code: Select all

net use | grep "Z:"
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.

:wink:
User avatar
kpeters58
Enthusiast
Enthusiast
Posts: 341
Joined: Tue Nov 22, 2011 5:11 pm
Location: Kelowna, BC, Canada

Re: "Slow" DirExist

Post 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")
PB 5.73 on Windows 10 & OS X High Sierra
AMpos
Enthusiast
Enthusiast
Posts: 128
Joined: Fri Jun 05, 2020 12:47 am

Re: "Slow" DirExist

Post 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.
Olli
Addict
Addict
Posts: 1238
Joined: Wed May 27, 2020 12:26 pm

Re: "Slow" DirExist

Post 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.
Post Reply