Help - Search - Members - Calendar
Full Version: VBScript Fileexists not working
MSFN Forums > Coding, Scripting and Servers > Programming (C++, Delphi, VB, etc.)

   
Google Internet Forums Unattended CD/DVD Guide
wkboarder348
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.
gunsmokingman
Change
QUOTE
CODE
obj.FileExists("C:\Program Files\Installshield Installation Information\{guid}\setup.exe")

To this
QUOTE
CODE
obj.FileExists(Chr(34) & "C:\Program Files\Installshield Installation Information\{guid}\setup.exe"  & Chr(34))
wkboarder348
I tried that and it still didn't work, it fails every time that way


QUOTE (gunsmokingman @ Sep 3 2008, 06:42 PM) *
Change
QUOTE
CODE
obj.FileExists("C:\Program Files\Installshield Installation Information\{guid}\setup.exe")

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



seisyll
You shouldn't need any quotes. Try this:

CODE
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)
wkboarder348
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

QUOTE (seisyll @ Sep 4 2008, 09:37 AM) *
You shouldn't need any quotes. Try this:

CODE
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)

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

CODE
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
gunsmokingman
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

QUOTE
CODE
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
cowstaker
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.
jcarle
Personally, I think there's nothing wrong with the FileExists method, there's probably something wrong with the rest of the script.
Google Internet Forums Unattended CD/DVD Guide
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.