-
June 4th, 2010, 10:06 AM
#1
Dealing with 'broken' network drive
Here's a question for you tech geeks. I have a VB6 program that looks for a .txt file on a network drive. But recently this drive failed - which caused my program to hang up for a very long time. How can I reduce the wait in case the drive fails again? I'm using the DIR command to see if the file exists - and the program hung up for a long time before returning.
-
June 4th, 2010, 06:07 PM
#2
Re: Dealing with 'broken' network drive
Maybe our recently discovered shell32 objects could be of help here, since they are able to access network drives as well. You best split the name of the file into its path and name features. Then:
Code:
dim shl, fld, fit
set shl = CreateObject("shell.application")
set fld = shl.Namespace(yourPathName)
If Not fld Is Nothing Then
For Each fit in fld.FolderItems
If fit.Name = yourFileName Then 'you have found the file
Next
End If
-
June 4th, 2010, 06:36 PM
#3
Re: Dealing with 'broken' network drive
Just MAP the drive, using Credentials.
-
June 7th, 2010, 06:23 AM
#4
Re: Dealing with 'broken' network drive
To WoF...
Your code "For Each fit in fld.FolderItems" gives me an error. Suggestions?
To dglienna...
Sorry... can you be more explicit?
-
June 7th, 2010, 07:43 PM
#5
Re: Dealing with 'broken' network drive
You can map a network drive, using that command. You can even supply the username/password, and get a drive letter to use (and even reconnects on logon). Then, you use THAT drive letter X: and it's the mapped drive
-
June 8th, 2010, 10:36 AM
#6
Re: Dealing with 'broken' network drive
What error is given? Please name number and error text for further considerations.
-
June 9th, 2010, 02:51 AM
#7
Re: Dealing with 'broken' network drive
We have been discussing this in another thread here at CC and the solution I found was the use of the Trim Function. So, change this...
Code:
set fld = shl.Namespace(yourPathName)
to this...
Code:
set fld = shl.Namespace(Trim(yourPathName))
Good Luck
-
June 9th, 2010, 06:31 AM
#8
Re: Dealing with 'broken' network drive
vb5prgrmr: Nope... That's not it. Look - maybe I had a typo the 1st time I tried it 'cause I'm getting a different result now. Here's the code:
F = "\\something"
Set shl = CreateObject("shell.application")
Set fld = shl.Namespace(F)
If Not fld Is Nothing Then
For Each fit In fld.FolderItems
If fit.Name = F Then
F = F
End If
Next
End If
No matter what I set 'F' to I get the same result. That is, when I execute 'Set fld = shl.Namespace(F)', 'fld' always equals 'nothing'. I've tried F = "\\something\something\something" format, F = "\\something\something\something\", F="C:", and F = "C:\". Always same result.
Am I missing something stupid?
-
June 9th, 2010, 08:24 AM
#9
Re: Dealing with 'broken' network drive
hmm I tried the code as shown in your post and I get fld=nothing.
I changed it as VB5prgrmr suggested and the value of fld is correct.
Code:
Set fld = shl.Namespace(Trim(f))
Of course when the trim is added I then get an error on this line
Code:
For Each fit In fld.FolderItems
Error 438 Object doesn't support this property or method
Always use [code][/code] tags when posting code.
-
June 9th, 2010, 08:36 AM
#10
Re: Dealing with 'broken' network drive
Made a change and now is working fine
Code:
Set fld = shl.Namespace(Trim(f))
If Not fld Is Nothing Then
For Each fit In fld.Items
Always use [code][/code] tags when posting code.
-
June 9th, 2010, 09:30 AM
#11
Re: Dealing with 'broken' network drive
Ok folks... Maybe you live in a different PC/VB world than me...
When I execute <For Each fit In fld.Items> fit.name doesn't give me an error but neither does it seem to have any value. However just 'fit' does - as I cycle through the For Each loop 'fit' gives me the names of all the files & folders in the drive. Remember - my goal was to determine whether a newtork drive was alive. By tweeking around I found the following seems to work:
Set Shl = CreateObject("shell.application")
Set Fld = Shl.Namespace(Trim(F))
If Not Fld Is Nothing Then
If Fld.items.Count > 0 Then
'folder exists!
End If
End If
If the drive is allive the reply is immediate. But if a drive is down it takes about 3 seconds for it to come to this conclusion. That's ok... it's shorter that the DIR command I was using... But quicker would be nice!
Frankly, the following work just as well...
Public Function FileExists(OrigFile As String)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
FExists = fso.FileExists(OrigFile)
End Function
Public Function DirExists(OrigFile As String)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
DirExists = fso.folderexists(OrigFile)
End Function
-
June 9th, 2010, 09:41 AM
#12
Re: Dealing with 'broken' network drive
FSO has more overhead and is much slower at just about everything. I would reccommend it never be used in a VB program.
IMO it should be used only in VBScript and Embedded VB where the normal file i/o is not available.
Always use [code][/code] tags when posting code.
-
June 9th, 2010, 10:15 AM
#13
Re: Dealing with 'broken' network drive
Well, I think the time overhead in just determining the existing of a file or folder can just be neglected.
In a loop where a folder is scanned, though, I'd also advise against the FSO.
Also I'm not sure if the FSO can access a server path like "\\Computer\Folder".
I think FSO can only acces a network share if it is bound to a drive letter.
Shell objects however can also browse non-filesystem folder items like Desktop, Networks and workgroups.
Concerning the code, I think the work is already done, if the Namespace() method does return an object.
No need to look for fld.Items.Count, I think. If fld is not Nothing, the folder (or drive) exists.
Code:
Set Shl = CreateObject("shell.application")
Set Fld = Shl.Namespace(Trim(F))
If Not Fld Is Nothing Then
'folder exists!
End If
-
June 9th, 2010, 10:27 AM
#14
Re: Dealing with 'broken' network drive
Even when not in a loop I would advise against using the FSO in any instance where there is another method available.
Sure if you are simply opening a text file and reading a line or 2 or checking to see if a file exists or some other simple thing the speed of FSO is not a major hinderence but even so the traditional file methods are much faster, use less memory and do not rely on an external scripting engine.
Always use [code][/code] tags when posting code.
-
June 9th, 2010, 11:49 AM
#15
Re: Dealing with 'broken' network drive
Originally Posted by DataMiser
Of course when the trim is added I then get an error on this line
Code:
For Each fit In fld.FolderItems
Error 438 Object doesn't support this property or method
Yes it does as I/we have not found a way to do a for each on it yet and presently we are using a for loop against the count in the other thread here at CG.
Also as I noted in the other thread, if the variable within the namespace parens is a variant, no need for the trim function within the parens...
Good Luck
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|