By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,166 Members | 1,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,166 IT Pros & Developers. It's quick & easy.

Searching all drives for a file

100+
P: 443
I am trying to use the code from scanandsearchallDirs and run into a problem the code is
Expand|Select|Wrap|Line Numbers
  1. Dim intDir As Integer
  2. Dim strDrive As String
  3. Dim lngErr As Long
  4. For intDir = 65 To 90
  5. On Error GoTo 0
  6. On Error Resume Next
  7. strDrive = Dir(Chr(intDir) & ":\*.*")
  8.  
Where I run into the problem is converting the 65 to a letter A. the strdrive just comes up with a blank. Can someone tell me what's wrong.
Thanks.
Jan 3 '17 #1

✓ answered by jforbes

You also might want to try something like this:
Expand|Select|Wrap|Line Numbers
  1. Public Function walkDrives() As Boolean
  2.  
  3.     Dim oFSO As FileSystemObject
  4.     Dim oDrive As Drive
  5.  
  6.     Set oFSO = New FileSystemObject
  7.     For Each oDrive In oFSO.Drives
  8.         Debug.Print oDrive.DriveLetter, oDrive.IsReady
  9.     Next oDrive
  10.  
  11.     Set oFSO = Nothing
  12.     walkDrives = True
  13.  
  14. End Function

Share this Question
Share on Google+
7 Replies


PhilOfWalton
Expert 100+
P: 1,430
Do you still have a floppy disk drive (A:)?

Phil
Jan 3 '17 #2

NeoPa
Expert Mod 15k+
P: 31,494
Hi Tom.

Please report any errors you have clearly. I suspect Phil has found the problem in spite of the lack details though.
Jan 4 '17 #3

100+
P: 443
I'm not getting any errors, it's just that strDrive shows "" I've tried upping the 65 to 67 thinking that if 65 should be A then 67 should be C. I still show the strDrive as ""
Jan 4 '17 #4

NeoPa
Expert Mod 15k+
P: 31,494
Ah. That's probably because you're setting strDrive from the Dir() call rather than the code that formulates the string that you pass to that function. On the other hand, I suppose one would expect there to be at least some files returned from that call - being as 67 should ==> "C".

Try this code and see what you get :
Expand|Select|Wrap|Line Numbers
  1. Dim intDir As Integer
  2. Dim strDrive As String
  3.  
  4. For intDir = 0 To 23
  5.     strDrive = Chr(Asc("C") + intDir) & ":\*.*"
  6.     Debug.Print strDrive,
  7.     strDrive = Dir(strDrive)
  8.     Debug.Print strDrive
  9. Next intDir
In case you're unsure, 67 is indeed the ASCII code for "C". Try the following in the Debug pane to confirm :
Expand|Select|Wrap|Line Numbers
  1. X="C":?Asc(X);
Jan 4 '17 #5

PhilOfWalton
Expert 100+
P: 1,430
Tom, assuming this is related to an earlier question, this code will tell you what drives are available.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Function AvailableDrives()
  3.  
  4.     Dim DriveSpec As String
  5.     Dim objFSO, boolFound, objDrive
  6.     Dim i As Integer
  7.  
  8.     Set objFSO = CreateObject("Scripting.FileSystemObject")
  9.  
  10.     For i = 67 To 90
  11.         DriveSpec = Chr$(i) & ":"
  12.  
  13.         boolFound = objFSO.DriveExists(DriveSpec)
  14.         If boolFound Then
  15.             Set objDrive = objFSO.GetDrive(DriveSpec)
  16.             If objDrive.IsReady Then
  17.                 AvailableDrives = objDrive.DriveLetter
  18.             Else
  19.                 AvailableDrives = Null
  20.             End If
  21.             Set objDrive = Nothing
  22.         Else
  23.             AvailableDrives = Empty
  24.         End If
  25.         If Len(AvailableDrives) > 0 Then
  26.             Debug.Print "Drive " & Chr$(i) & ": exists"
  27.         End If
  28.     Next i
  29.  
  30.     Set objFSO = Nothing
  31.  
  32.  End Function
  33.  
As I stated in an earlier post, making assumptions of which folder is used to store your data is very dodgy.

Phil
Jan 4 '17 #6

jforbes
Expert 100+
P: 1,107
You also might want to try something like this:
Expand|Select|Wrap|Line Numbers
  1. Public Function walkDrives() As Boolean
  2.  
  3.     Dim oFSO As FileSystemObject
  4.     Dim oDrive As Drive
  5.  
  6.     Set oFSO = New FileSystemObject
  7.     For Each oDrive In oFSO.Drives
  8.         Debug.Print oDrive.DriveLetter, oDrive.IsReady
  9.     Next oDrive
  10.  
  11.     Set oFSO = Nothing
  12.     walkDrives = True
  13.  
  14. End Function
Jan 4 '17 #7

100+
P: 443
I have everything working just like I wanted it to. Thanks for all the help. See Ya'all when I run into another question, you guys are great. Thanks again.
Jan 4 '17 #8

Post your reply

Sign in to post your reply or Sign up for a free account.