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.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 theWshShell
objector expand the path of the directory
.
: