wkboarder348 Posted September 3, 2008 Posted September 3, 2008 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 Posted September 4, 2008 Posted September 4, 2008 Changeobj.FileExists("C:\Program Files\Installshield Installation Information\{guid}\setup.exe")To thisobj.FileExists(Chr(34) & "C:\Program Files\Installshield Installation Information\{guid}\setup.exe" & Chr(34))
wkboarder348 Posted September 4, 2008 Author Posted September 4, 2008 I tried that and it still didn't work, it fails every time that wayChangeobj.FileExists("C:\Program Files\Installshield Installation Information\{guid}\setup.exe")To thisobj.FileExists(Chr(34) & "C:\Program Files\Installshield Installation Information\{guid}\setup.exe" & Chr(34))
seisyll Posted September 4, 2008 Posted September 4, 2008 You shouldn't need any quotes. Try this:Dim filespec, msgfilespec = "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 IfWScript.Echo(msg)
wkboarder348 Posted September 4, 2008 Author Posted September 4, 2008 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 trueYou shouldn't need any quotes. Try this:Dim filespec, msgfilespec = "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 IfWScript.Echo(msg)
cluberti Posted September 4, 2008 Posted September 4, 2008 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
gunsmokingman Posted September 5, 2008 Posted September 5, 2008 Try this script it will list the contents of any sub folder and there files. I have tested this on XP SP3and used IE as the folder. See if it runs on your computerSave As List.vbsDim Act :Set Act = CreateObject("Wscript.Shell")Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")Dim Dtop, ColF, ObjF, StrF, TsDtop = 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 Posted September 19, 2008 Posted September 19, 2008 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~1cd install~1or Install~2Alternatively you could use an path variable such as %programfiles% which calls the first bit for you.
jcarle Posted September 19, 2008 Posted September 19, 2008 Personally, I think there's nothing wrong with the FileExists method, there's probably something wrong with the rest of the script.
anthonyfrost Posted April 11, 2012 Posted April 11, 2012 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 = FALSEIF CheckForStopFile THEN EXIT DOFUNCTION CheckForStopFileCheckForStopFile = FALSESET objFSO = CreateObject("Scripting.FileSystemObject")IF objFSO.FileExists(PrControlFile) THEN CheckForStopFile = TRUESET objFSO = CreateObject("Scripting.FileSystemObject")SET DeleteStop = objFSO.GetFile(PrControlFile)DeleteStop.DeleteEND IFEND FUNCTION
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now