Why doesn't VBSCRIPT fileexist work if the file doesn't exist in %windir%\system32 on Windows 7 professional 64 bit
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
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
3 Comments
[ + ] Show comments
Answers (3)
Answer Summary:
Please log in to answer
Posted by:
Pressanykey
8 years ago
Top Answer
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
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
Comments:
-
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%") - anonymous_129629 8 years ago
Posted by:
anonymous_129629
8 years ago
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)
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)
Comments:
-
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 - Pressanykey 8 years ago-
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 - anonymous_129629 8 years ago
-
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. - anonymous_9363 8 years ago
Posted by:
anonymous_129629
8 years ago
Which version of c/wscript.exe are you calling? (i.e. the 32bit or 64bit) - Pressanykey 8 years ago
Public oFh, oFile, oEnvProcess
Public sNotExistFile: sNotExistFile = "NotExistFile.txt" - anonymous_129629 8 years ago