CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Hybrid View

  1. #1
    Join Date
    Apr 2005
    Posts
    103

    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.

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    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

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Dealing with 'broken' network drive

    Just MAP the drive, using Credentials.
    NET USE /?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Apr 2005
    Posts
    103

    Question 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?

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Dealing with 'broken' network drive

    What error is given? Please name number and error text for further considerations.

  7. #7
    Join Date
    Apr 2009
    Posts
    394

    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

  8. #8
    Join Date
    Apr 2005
    Posts
    103

    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?

  9. #9
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.

  10. #10
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.

  11. #11
    Join Date
    Apr 2005
    Posts
    103

    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

  12. #12
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.

  13. #13
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    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

  14. #14
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.

  15. #15
    Join Date
    Apr 2009
    Posts
    394

    Re: Dealing with 'broken' network drive

    Quote 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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured