0

In a VBScript, I want to know if a file exists:

Set fso = CreateObject("Scripting.FileSystemObject")
If Not (fso.FileExists(file)) Then
  msg = " doesn't exist." 
End If  

My files are on internal network.

Is there a way to distinguish:

  • file really doesn't exist
  • access denied

I try with fso.OpenTextFile but the result for these two cases is always: Err.Number = 5.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Are you saying that `fso.FileExists(file)` returns `False` even if a file exists but you don't have permission? I would have unexpected an `Permission Denied` error to be generated... – user692942 Feb 11 '16 at 12:06
  • Non-accessible files exist (so .FileExists will raise no error, but return True) and .OpenTextFile will return "Permission denied" if an existing file isn't accessible. – Ekkehard.Horner Feb 11 '16 at 12:47
  • @Ekkehard.Horner Which is what I was trying to established seemed strange the OP was suggesting that `FileExists` was returning `False`. – user692942 Feb 11 '16 at 12:54
  • Error 5 in VBScript is `Invalid procedure call or argument` so it's likely that you are actually doing something wrong when you call both `FileExists` and `OpenTextFile`. Since the common dominator is the `file` variable are you sure `file` contains a valid value? – user692942 Feb 11 '16 at 15:40

1 Answers1

3

To distinguish between non-existing and non-accessible files you need .FileExists and .OpenTextFile:

Option Explicit

Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")

Function ReadFile(p, ByRef m)
  If goFS.FileExists(p) Then
     Dim aErr
     On Error Resume Next
      Set ReadFile = goFS.OpenTextFile(p)
      aErr = Array(Err.Number, Err.Description)
     On Error GoTo 0
     If aErr(0) Then
        m = p & " - " & aErr(1)
        Set ReadFile = Nothing
     Else
        m = ""
     End If
  Else
     Set ReadFile = Nothing
     m = p & " - no such file"
  End If
End Function

Dim p, m
For Each p In Split("e:\roots.own e:\nosuchfile e:\dirsbf.tmp")
    Dim tsIn : Set tsIn = ReadFile(p, m)
    If tsIn Is Nothing Then
       WScript.Echo "fail", m
    Else
       ' read from tsIn
       tsIn.Close
       WScript.Echo "ok"
    End If
Next

Output:

cscript 35338634.vbs
fail e:\roots.own - Permission denied
fail e:\nosuchfile - no such file
ok

Thanks to Ansgar's observation, the function can be improved:

Function ReadFile(p, ByRef m)
  Dim aErr
 On Error Resume Next
  Set ReadFile = goFS.OpenTextFile(p)
  aErr = Array(Err.Number, Err.Description)
 On Error GoTo 0
  If aErr(0) Then
     m = p & " - " & aErr(1)
     Set ReadFile = Nothing
  Else
     m = ""
  End If
End Function
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • 3
    `FileExists` isn't needed here. `OpenTextFile` raises different errors for either case (error 53: file not found; error 70: access denied). – Ansgar Wiechers Feb 11 '16 at 13:04
  • Thank you both. I will accept your solution because my question was not clear enough. I still have this problem (because my files are sometimes in a directory that i have no right to access but i think there is no solution in vbs). – Marius Seheia Feb 11 '16 at 14:46
  • @MariusSeheia You can't enumerate contents of a folder you don't have access permissions for. You'd need a service providing you with a definitive list of the files/folders on the system to solve this. – Ansgar Wiechers Feb 11 '16 at 17:11