Why doesn't WScript.Shell.ExpandEnvironmentStrings work with %CD%? - vbscript

In the command line you can output the current directory using echo %CD% like this this:
The Windows Scripting Host provides the ExpandEnvironmentalStrings method which can be used like this:
Dim objWshShell : Set objWshShell = CreateObject("Wscript.Shell")
MsgBox objWshShell.ExpandEnvironmentStrings("%WINDIR%")
However, it doesn't work with %CD%. It just returns the same value, %CD%:
Dim objWshShell : Set objWshShell = CreateObject("Wscript.Shell")
MsgBox objWshShell.ExpandEnvironmentStrings("%CD%")
Why doesn't this work? I know there are other ways to get the current directory; this is just a curiousity.

The variable %CD% is a CMD-builtin automatic variable, not an environment variable like %PATH% or %USERNAME%. It can only be used within CMD, e.g.
cmd /c echo %CD%
Same goes for the variables %TIME%, %DATE%, and %ERRORLEVEL%.
If you want the current working directory in VBScript you need to use the CurrentDirectory property of the WshShell object
Set sh = CreateObject("WScript.Shell")
WScript.Echo sh.CurrentDirectory
or expand the path of the directory .:
Set fso = CreateObject("Scripting.FileSystemObject")
WScript.Echo fso.GetAbsolutePathName(".")

Related

How do I use spaces with my objShell.CurrentDirectory code?

How can I fix this code so it works correctly? I think it has something to do with the spaces in "Start Menu". The problem comes when I run the file in another drive. For example my P: Drive
Set objShell = WScript.CreateObject("WScript.Shell")
File = "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup"
objShell.CurrentDirectory = File
The environment variables can't be just notated in the path string. You have to use the Shell object method below (ExpandEnv...) to obtain the Environment variable.
This should do what I assume you're trying to do to-date.
Set objShell = WScript.CreateObject("WScript.Shell"): appdata = objShell.ExpandEnvironmentStrings("%appdata%")
File = appdata & "\Microsoft\Windows\Start Menu\Programs\Startup"
objShell.CurrentDirectory = File
Wscript.Echo objShell.CurrentDirectory

VBS - Getting program files folder path?

I am tring to get program files folder in vbs. Tried this without luck;
SET wsc = CreateObject("WScript.Shell")
SET fso = WScript.CreateObject("Scripting.FileSystemObject")
targetpath = wsc.SpecialFolders("ProgramFiles") & "\Google\Chrome\Application\chrome.exe"
It just get the C:\ dir. What is the correct way to do it ?
This TechNet article shows the list of SpecialFolders. Program Files is not among them. This is a limitation of the Windows Script Host. In the same way that the following shows a blank popup
SET wsc = CreateObject("WScript.Shell")
msgbox wsc.SpecialFolders("Awesome")
So instead you have at least 2 options.
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H26&)
Set objFolderItem = objFolder.Self
msgbox objFolderItem.Path
&H26& - Program Files
&H2A& - Program Files (x86)
The other option that I would offer is to use Environment variables like JosefZ suggests.
targetpath = wsc.ExpandEnvironmentStrings("%ProgramFiles%") & "..."
targetpath = wsc.ExpandEnvironmentStrings("%ProgramFiles(x86)%") & "..."

windows shell - how to detect the current location where script file is placed?

I am trying the windows shell file which will be inserted in the folder where it will analyze folders content.
Now i would like to know how can i detect which is the current path ? i.e. location where the vbs file is placed using FileSystemObject?
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFS = CreateObject("Scripting.FileSystemObject")
WScript.Echo objFS.GetParentFolderName(WScript.ScriptFullName)
You can get that from WScript.ScriptFullName. Just remove the filename from the end (the bit after the last backslash). I normally use JScript for scripts, but IIRC VBScript has an InStrRev function that will help you find the last backslash. Or: Create a File object for the WScript.ScriptFullName path and then use its ParentFolder property. Something like (untested):
Dim objFSO
Dim objFile
Dim objFolder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(WScript.ScriptFullName)
Set objFolder = objFile.ParentFolder
To get the full path only without the extension I use Replace(WScript.ScriptFullName, WScript.ScriptName, "") to just result in a filepath

Changing the current directory of a FileSystemObject

When using a FileSystemObject you can reference the directory the script was run from by using the path ".". Is it possible to change what the FileSystemObject thinks is the current directory so you can use the "." path syntax for other directories?
Example:
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.GetFolder(".") ' Returns the current directory
Set f2 = fso.GetFolder(".\backup") ' Returns the "backup" directory under the current directory
As a simplified example, is there a method to call on fso so that the fso.GetFolder(".") call returns the backup directory instead?
You can change the current working folder for your script using the WshShell.CurrentDirectory property:
Dim oShell : Set oShell = CreateObject("WScript.Shell")
oShell.CurrentDirectory = ".\backup"
And here's a Hey, Scripting Guy! article on the subject: How Can I Change the Working Folder of a Script?
Not in general.
But why not find the current folder and store it:
Set fso = CreateObject("Scripting.FileSystemObject")
currentPath = fso.GetAbsolutePathName(".")

Launch programs whose path contains spaces

I need to launch programs in my local system using VBScript. But I am having trouble with the syntax. This is what I am using right now -
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("iexplore")
Set objShell = Nothing
The above code successfully launches IE. I want to launch other browsers. But, if I use -
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("c:\Program Files\Mozilla Firefox\firefox.exe")
Set objShell = Nothing
it is throwing an error saying that the file or path was not found. I am not sure how the parameter inside the Run() function is taken - should I give the path to an EXE or should I give some DOS commands?!
Try:-
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("""c:\Program Files\Mozilla Firefox\firefox.exe""")
Set objShell = Nothing
Note the extra ""s in the string. Since the path to the exe contains spaces it needs to be contained with in quotes. (In this case simply using "firefox.exe" would work).
Also bear in mind that many programs exist in the c:\Program Files (x86) folder on 64 bit versions of Windows.
You van use Exec
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Exec("c:\Program Files\Mozilla Firefox\firefox.exe")
Set objShell = Nothing
It's working with
Set WSHELL = CreateObject("Wscript.Shell")
WSHELL.Exec("Application_Path")
But what should be the parameter in case we want to enter the application name only
e.g in case of Internet Explorer
WSHELL.Run("iexplore")
set shell=CreateObject("Shell.Application")
' shell.ShellExecute "application", "arguments", "path", "verb", window
shell.ShellExecute "slipery.bat",,"C:\Users\anthony\Desktop\dvx", "runas", 1
set shell=nothing
find an .exe file for the application you want to run
example iexplore.exe and firefox.exe and remove .exe
and use it in objShell.Run("firefox")
I hope this helps.
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("firefox")
Set objShell = Nothing
Please try this
Copy the folder, firefox.exe is in and place in the c:\ only. The script is having a hard time climbing your file tree. I found that when I placed the *.exe file in the c:\ it eliminated the error message " file not found."

Resources