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(".")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With