I have tried the following without any luck. They all returns true even if I add Chr(34) before and after sFileName

But they all works, if the file doesn't exist in e.g. the C:\Temp folder

Option Explicit
On Error Resume Next
Const TristateFalse = 0
Public oFso : Set oFso = CreateObject("Scripting.FileSystemObject")
Public oFh, oFile
Public cFiles
Public sEnvWinDir : sEnvWinDir = oEnvProcess("WINDIR")
Public sEnvWinSys32 : sEnvWinSys32 = sEnvWinDir & "\System32"
Public sNotExistFile = "NotExistFile.txt"
Public sFileName : sFileName = sEnvWinSys32 & "\" & sNotExistFile
Public sTmpFolder : sTmpFolder = "C:\Temp"
Public sMsg
Public bStatus, bExist



' Example #1
If (oFso.FileExists(sFileName)) Then
  sMsg = sFileName & " exists."
Else
  sMsg = sFileName & " doesn't exist."
End If
WScript.Echo(sMsg)

' Example #2
If oFso.FileExists(sFileName) Then
  sMsg = sFileName & " exists."
Else
  sMsg = sFileName & " doesn't exist."
End If
WScript.Echo(sMsg)

' Example #3
bExist = oFso.FileExists(sFileName)
If bExist = True Then
  sMsg = sFileName & " exists."
Else
  sMsg = sFileName & " doesn't exist."
End If
WScript.Echo(sMsg)

' Example #4
Set oFh=oFso.GetFile(sFileName)
If Err.Number <> 0 Then
  sMsg = sFileName & " doesn't exist."
Else
  sMsg = sFileName & " exists."
End If
WScript.Echo(sMsg)

' Example #5
Set oFh = oFso.GetFolder(sEnvWinSys32)
Set cFiles = oFh.Files
For Each oFile in colFiles
  If sNotExistFile = oFile.Name Then
    sMsg = sFileName & " doesn't exist."
  Else
    sMsg = sFileName & " exists."
  End If
Next  
WScript.Echo(sMsg)

' Example #6
bStatus = oFso.CopyFile(sFileName, sTmpFolder, True)
If Err.Number <> 0 Then
  sMsg = sFileName & " doesn't exist."
Else
  sMsg = sFileName & " exists."
End If
WScript.Echo(sMsg)

' Example #7
bStatus = oFso.CopyFile(sFileName, sTmpFolder, False)
If Err.Number <> 0 Then
  sMsg = sFileName & " doesn't exist."
Else
  sMsg = sFileName & " exists."
End If
WScript.Echo(sMsg)


Sincerely

Jorgen Malmgren
IT-Programmer
Answer Summary:
Cancel
3 Comments   [ + ] Show Comments

Comments

  • Just for info, does it work on 32bit systems?
    Which version of c/wscript.exe are you calling? (i.e. the 32bit or 64bit)
  • Yes they all works on 32-bit, I'm using 64-bit cscript.exe
    • and the 64-bit works on e.g. C:\Temp, but not on C:\Windows\system32, C:\Windows\SysWOW64, C:\ProgramFiles
      • Try defining oEnvProcess and then I'll have another look
  • I changed these 2 lines, but the answer with all 7 examples still is: File doesn't exist, even if i created the C:\Windows\System32\NotExistFile.txt file.

    Public oFh, oFile, oEnvProcess
    Public sNotExistFile: sNotExistFile = "NotExistFile.txt"
Please log in to comment

Answer Chosen by the Author

1
Hi,
you do not seem to create an object of whatever type oEnvProcess is, yet you attempt to use a method from it.
As you have defined On Error Resume Next using an none instanced object, no errors are thrown and you attempt to use if, it will return NULL or an empty string, and therefore the "dynamic" string to a directory is invalid, but the "hard-coded" directory string is ok (C:\Temp).

I suggest you get a decent Code-Editor with debugger, and never use On Error Resume Next unless you really know what you are doing.

The above assumes that you have given me all of the information, and is based on what you have posted to date.

Cheers
Phil
Answered 02/15/2016 by: Pressanykey
Red Belt

  • Hi Phil - Thanks - Changing the following of my lines solved this issue, and now it works:

    Public oShell : Set oShell = WScript.CreateObject("WScript.Shell")
    Public sEnvWinDir : sEnvWinDir = oShell.ExpandEnvironmentStrings("%WINDIR%")
Please log in to comment

Answers

0
I'm using Notepad++ as my editor.

Example #1-#4 works now, but #5-#7 doesn't work.

Option Explicit
On Error Resume Next
Const TristateFalse = 0
Public oFso : Set oFso = CreateObject("Scripting.FileSystemObject")
Public oShell : Set oShell = WScript.CreateObject("WScript.Shell")
Public oFh
Public cFiles
Public sEnvWinDir : sEnvWinDir = oShell.ExpandEnvironmentStrings("%WINDIR%")
Public sEnvWinSys32 : sEnvWinSys32 = sEnvWinDir & "\System32"
Public sNotExistFile: sNotExistFile = "NotExistFile.txt"
Public sFileName : sFileName = sEnvWinSys32 & "\" & sNotExistFile
Public sTmpFolder : sTmpFolder = "C:\Temp"
Public sMsg
Public bStatus, bExist



' Example #1
If (oFso.FileExists(sFileName)) Then
  sMsg = sFileName & " exists."
Else
  sMsg = sFileName & " doesn't exist."
End If
WScript.Echo(sMsg)

' Example #2
If oFso.FileExists(sFileName) Then
  sMsg = sFileName & " exists."
Else
  sMsg = sFileName & " doesn't exist."
End If
WScript.Echo(sMsg)

' Example #3
bExist = oFso.FileExists(sFileName)
If bExist = True Then
  sMsg = sFileName & " exists."
Else
  sMsg = sFileName & " doesn't exist."
End If
WScript.Echo(sMsg)

' Example #4
Set oFh=oFso.GetFile(sFileName)
If Err.Number <> 0 Then
  sMsg = sFileName & " doesn't exist."
Else
  sMsg = sFileName & " exists."
End If
WScript.Echo(sMsg)

' Example #5
Set oFh = oFso.GetFolder(sEnvWinSys32)
Set cFiles = oFh.Files
For Each oFile in colFiles
  If sNotExistFile = oFile.Name Then
    sMsg = sFileName & " doesn't exist."
  Else
    sMsg = sFileName & " exists."
  End If
Next 
WScript.Echo(sMsg)

' Example #6
bStatus = oFso.CopyFile(sFileName, sTmpFolder, True)
If Err.Number <> 0 Then
  sMsg = sFileName & " doesn't exist."
Else
  sMsg = sFileName & " exists."
End If
WScript.Echo(sMsg)

' Example #7
bStatus = oFso.CopyFile(sFileName, sTmpFolder, False)
If Err.Number <> 0 Then
  sMsg = sFileName & " doesn't exist."
Else
  sMsg = sFileName & " exists."
End If
WScript.Echo(sMsg)
Answered 02/15/2016 by: jMalmgren
White Belt

  • Please take out the On Error Resume Next, if you don't have a debugger you will get now where...
    Also, please confirm the following situations are required:

    32bit CScript.exe on 32bit System
    32bit CScript.exe on 64bit System
    64bit CScript.exe on 64bit System

    Also, exactly what are you trying to accomplish?

    Phil
    • Hi Phil. OK Done. I only try to accomplish to use the fileexists function without any errors on any folder on a 64-bit Windows computer
      • Look up what the "On Error..." construct does! Then download, install and learn how to use the Microsoft Script Debugger. It's not great but it beats having to 'MsgBox "[whatever]"' everywhere to try and work out what's going wrong.

        If you have Office 2003 or 2007 anywhere, install the Microsoft Script Editor which is a much better debugger.
Please log in to comment
0
VBScab, Hi, I wasn't able to Reply your thread, so I answer you here below.

I will consider your suggestions.
Answered 02/16/2016 by: jMalmgren
White Belt

Please log in to comment
Answer this question or Comment on this question for clarity