batch file - Windows shell command to get the full path to the current directory?

ID : 10152

viewed : 46

Tags : windowsbatch-filecommand-linecmdwindows

Top 5 Answer for batch file - Windows shell command to get the full path to the current directory?

vote vote

98

Use cd with no arguments if you're using the shell directly, or %cd% if you want to use it in a batch file (it behaves like an environment variable).

vote vote

84

You can set a batch/environment variable as follows:

SET var=%cd% ECHO %var% 

sample screenshot from a Windows 7 x64 cmd.exe.

enter image description here

Update: if you do a SET var = %cd% instead of SET var=%cd% , below is what happens. Thanks to jeb.

enter image description here

Capturing the current directory from a batch file

vote vote

80

Quote the Windows help for the set command (set /?):

 If Command Extensions are enabled, then there are several dynamic environment variables that can be expanded but which don't show up in the list of variables displayed by SET.  These variable values are computed dynamically each time the value of the variable is expanded. If the user explicitly defines a variable with one of these names, then that definition will override the dynamic one described below:  %CD% - expands to the current directory string.  %DATE% - expands to current date using same format as DATE command.  %TIME% - expands to current time using same format as TIME command.  %RANDOM% - expands to a random decimal number between 0 and 32767.  %ERRORLEVEL% - expands to the current ERRORLEVEL value  %CMDEXTVERSION% - expands to the current Command Processor Extensions     version number.  %CMDCMDLINE% - expands to the original command line that invoked the     Command Processor. 

Note the %CD% - expands to the current directory string. part.

vote vote

70

On Unix?

pwd

vote vote

51

This has always worked for me:

SET CurrentDir="%~dp0"  ECHO The current file path this bat file is executing in is the following:  ECHO %CurrentDir%  Pause 

Top 3 video Explaining batch file - Windows shell command to get the full path to the current directory?

Related QUESTION?