Jump to content

VBScript Fileexists not working


Recommended Posts

Hey, i'm writing a vbs script that needs to check if a file exists.

obj.FileExists("C:\Program Files\Installshield Installation Information\{guid}\setup.exe")

When this code is executed the FileExists always returns true even if the file doesn't exist. I've done some testing and found that the problem is the spaces. Once it reaches the first space it stops reading, so technically it's only looking for C:\Program. Is there any kind of escape character that needs to go in front of the spaces? I've done some researching online and it seems people say this shouldn't happen, but it does, and I can guarantee it's because of the spaces.

Link to comment
Share on other sites


I tried that and it still didn't work, it fails every time that way

Change
obj.FileExists("C:\Program Files\Installshield Installation Information\{guid}\setup.exe")

To this

obj.FileExists(Chr(34) & "C:\Program Files\Installshield Installation Information\{guid}\setup.exe"  & Chr(34))

Link to comment
Share on other sites

You shouldn't need any quotes. Try this:

Dim filespec, msg
filespec = "E:\Program Files\Internet Explorer\iexplore.exe"
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(filespec)) Then
msg = filespec & " exists."
Else
msg = filespec & " doesn't exist."
End If
WScript.Echo(msg)

Link to comment
Share on other sites

That sorta works. It stops at the first space and the reason i know it does is because if the setup is there i run it, and if it isn't there it still trys to run it.. so whether the file is there or not, that way always returns true

You shouldn't need any quotes. Try this:

Dim filespec, msg
filespec = "E:\Program Files\Internet Explorer\iexplore.exe"
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(filespec)) Then
msg = filespec & " exists."
Else
msg = filespec & " doesn't exist."
End If
WScript.Echo(msg)

Link to comment
Share on other sites

This worked fine in Vista, and on XP/2003 with WSH 5.7 installed:

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("E:\Program Files\Internet Explorer\iexplore.exe") Then

' // I tested C:\ and E:\
' If objFSO.FileExists("C:\Program Files\Internet Explorer\iexplore.exe") Then


Wscript.Echo "File exists."
Else
Wscript.Echo "File does not exist."
End If

Link to comment
Share on other sites

Try this script it will list the contents of any sub folder and there files. I have tested this on XP SP3

and used IE as the folder. See if it runs on your computer

Save As List.vbs

Dim Act :Set Act = CreateObject("Wscript.Shell")
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim Dtop, ColF, ObjF, StrF, Ts
Dtop = Act.SpecialFolders("Desktop") & "\ListFolder.txt"
'Dim Path : Path = Act.ExpandEnvironmentStrings("%ProgramFiles%\Installshield Installation Information")
Dim Path : Path = Act.ExpandEnvironmentStrings("%ProgramFiles%\Internet Explorer")
If Fso.FolderExists(Path) Then
Set Ts = Fso.CreateTextFile(Dtop)
Ts.WriteLine "Confirm :" & Path
ShowSubFolders Fso.GetFolder(Path)
Ts.WriteLine ObjF
Ts.Close
Act.Run(Chr(34) & Dtop & Chr(34)),1,True
ColF = MsgBox("Would You Like To Keep This File?" & vbCrLf & _
"Yes To Keep No To Delete The File", 4132,"Keep Or Delete")
If ColF = 7 Then Fso.DeleteFile(Dtop),True
Else
WScript.Echo "Missing :" & Path
End If

Function ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
ObjF = ObjF & "--------------------------" & vbCrLf & _
Subfolder.Path & vbCrLf & "--------------------------" & vbCrLf
Set StrF = Fso.GetFolder(Subfolder.Path)
For Each ColF In StrF.Files
ObjF = ObjF & " " & Chr(187) & " " & ColF.Name & vbCrLf
Next
ObjF = ObjF & vbCrLf
ShowSubFolders Subfolder
Next
End Function

Link to comment
Share on other sites

  • 2 weeks later...

You can cheat. This is not best practice but may solve your problem.

obj.FileExists("C:\Program Files\Installshield Installation Information\{guid}\setup.exe")

could also be called from the old dos 8 character name format by:

obj.fileexists("c:\progra~1\instal~1\{guid}\setup.exe")

You can use the first 6 characters and then ~1, if it is the first instance of those 6 characters etc. Go to a command prompt and try:

cd \

cd progra~1

cd install~1

or

Install~2

Alternatively you could use an path variable such as %programfiles% which calls the first bit for you.

Link to comment
Share on other sites

  • 3 years later...

I know many hate replies to old post, but I am doing so because a web search brought me here and there was never a resolution.

I had a similar problem while using the method inside of a do/loop and found that putting the check inside of a function outside the loop and then having the function return a true/false value worked.

Again, this is more for people who find this in a web search and not the original poster.

Small snippet.

CheckForStopFile = FALSE

IF CheckForStopFile THEN EXIT DO

FUNCTION CheckForStopFile

CheckForStopFile = FALSE

SET objFSO = CreateObject("Scripting.FileSystemObject")

IF objFSO.FileExists(PrControlFile) THEN

CheckForStopFile = TRUE

SET objFSO = CreateObject("Scripting.FileSystemObject")

SET DeleteStop = objFSO.GetFile(PrControlFile)

DeleteStop.Delete

END IF

END FUNCTION

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...