Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account



Photo

VBScript Fileexists not working

- - - - -

  • Please log in to reply
9 replies to this topic

#1
wkboarder348

wkboarder348
  • Member
  • 3 posts
  • Joined 03-September 08
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.


How to remove advertisement from MSFN

#2
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,419 posts
  • Joined 02-August 03
  • OS:none specified
  • Country: Country Flag
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))




GunSmokingMan



#3
wkboarder348

wkboarder348
  • Member
  • 3 posts
  • Joined 03-September 08
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))



#4
seisyll

seisyll
  • Member
  • 1 posts
  • Joined 03-September 08
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)


#5
wkboarder348

wkboarder348
  • Member
  • 3 posts
  • Joined 03-September 08
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)



#6
cluberti

cluberti

    Gustatus similis pullus

  • Supervisor
  • 11,252 posts
  • Joined 09-September 01
  • OS:Windows 8.1 x64
  • Country: Country Flag
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

MCTS Windows Internals, MCITP Server 2008 EA, MCTS MDT/BDD, MCSE/MCSA Server 2003, Server 2012, Windows 8
--------------------
Please read the rules before posting!
Please consider donating to MSFN to keep it up and running!

#7
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,419 posts
  • Joined 02-August 03
  • OS:none specified
  • Country: Country Flag
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




GunSmokingMan



#8
cowstaker

cowstaker
  • Member
  • 1 posts
  • Joined 18-September 08
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.

#9
jcarle

jcarle

    MSFN Master

  • Developer
  • 2,579 posts
  • Joined 14-August 04
Personally, I think there's nothing wrong with the FileExists method, there's probably something wrong with the rest of the script.
Posted Image
Posted Image

#10
anthonyfrost

anthonyfrost
  • Member
  • 1 posts
  • Joined 11-April 12
  • OS:none specified
  • Country: Country Flag
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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users