vbscript – Why doesnt WScript.Shell.ExpandEnvironmentStrings work with %CD%?

vbscript – Why doesnt WScript.Shell.ExpandEnvironmentStrings work with %CD%?

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(.)

vbscript – Why doesnt WScript.Shell.ExpandEnvironmentStrings work with %CD%?

Leave a Reply

Your email address will not be published. Required fields are marked *