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

Dir() Error 52: Bad file name or number

Expert 100+
P: 1,287
I'm using

Expand|Select|Wrap|Line Numbers
  1. strFileName = "\\serverName\folderName\fileName.txt"
  2. If Dir(strFileName) > "" Then
  3. ...
This worked fine for me, even when I disconnected from the network, but when a user ran it on an unconnected laptop the Dir line threw an Error 52: Bad file name or number. It seems like Dir should just return an empty string instead of an error in any case.

Anyone know why it would do that?
Feb 6 '09 #1
Share this Question
Share on Google+
3 Replies


ADezii
Expert 5K+
P: 8,607
@ChipR
This appears to be the Default Behavior for Network Paths, just set a
Trap for Error Number 52 as in
Expand|Select|Wrap|Line Numbers
  1. On Error GoTo Err_Test_Click
  2. strFileName = "\\serverName\folderName\fileName.txt"
  3.  
  4. If Len(Dir(strFileName)) > 0 Then
  5.   'File/Path OK, code execution begins here
  6. Else
  7.   'don't really need this, an Error will be generated
  8. End If
  9.  
  10. Exit_Test_Click:
  11.   Exit Sub
  12.  
  13. Err_Test_Click:
  14.   If Err.Number = 52 Then   'Bad File Name or Number
  15.     MsgBox "File or Network Path not found!"
  16.   Else
  17.     MsgBox Err.Description, vbExclamation, "Error in Test_Click()"
  18.   End If
  19.     Resume Exit_Test_Click
Feb 6 '09 #2

Expert 100+
P: 1,287
Thanks ADezii, I did that to handle the error, but I was surprised by what seems to me to be an undocumented error result. It worries me about what other errors could magically pop up that I can't possibly forsee, but I guess I'll just have to fix them quick when they happen.
Feb 6 '09 #3

ADezii
Expert 5K+
P: 8,607
@ChipR
Do not despair, ChipR, for here is a clean and efficient alternative:
Expand|Select|Wrap|Line Numbers
  1. 'Must set a Reference to the Microsoft Scripting Runtime
  2. Dim fso As FileSystemObject
  3. Dim fil As File
  4.  
  5. Set fso = New Scripting.FileSystemObject
  6.  
  7. If fso.FileExists("\\serverName\folderName\fileName.txt") Then
  8.   'code execution here
  9. Else
  10.   MsgBox "File and/or Path cannot be found", vbCritical, "File Not Found"
  11. End If
  12.  
Feb 7 '09 #4

Post your reply

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