Well, that isn't so sweet. I want to open a certain file from %TMP%
directory with VBScript by using
WScript.CreateObject("WScript.Shell").Run(WScript.CreateObject("Scripting.FileSystemObject").GetSpecialFolder(2)+"\OpenMe.txt")
It works fine on windows 7, but not on XP, because there is a space in the temp folder path.
Some tutorials suggest to use quotes (""
), but I don't know how.
Simply concatenate double quotes to the beginning and end of the path string. Since VBScript uses double quotes for string literals you need to specify them either via their ASCII code (Chr(34)
) or by putting a two subsequent double quotes inside a string literal (""""
). Doubling escapes the double quote.
WScript.CreateObject("WScript.Shell").Run(Chr(34)+WScript.CreateObject("Scripting.FileSystemObject").GetSpecialFolder(2)+"\OpenMe.txt"+Chr(34))
On a more general note, you should use the concatenation operator (&
) instead of the addition operator (+
). Even though the latter will work in most situations, there are some edge cases where it'll produce an error. Also, avoiding nested method calls, building paths via the BuildPath
method, and perhaps defining a quoting function will make for cleaner code that is better to understand and to maintain.
Function qq(s)
qq = Chr(34) & s & Chr(34)
End Function
Set fso = CreateObject("Scripting.FileSystemObject")
Set sh = CreateObject("WScript.Shell")
tempfolder = fso.GetSpecialFolder(2)
path = fso.BuildPath(tempfolder, "OpenMe.txt")
sh.Run qq(path)