There is the environment variable CD
which always has the path of current directory. Run in a command prompt window set /?
to get displayed on several pages the help of this command listing at end also the dynamic environment variables like CD
, DATE
or TIME
which can't be seen on running just set
which outputs all currently defined static environment variables.
The current directory can be equal the directory of the started batch file which is usually the case on double clicking on a batch file.
But the current directory can be also any other directory different to directory of the batch file. For example the current directory is %SystemRoot%\System32
on running a batch file as administrator or as scheduled task with system account or %SystemRoot%
on double clicking on a batch file on a network resource accessed using a UNC path.
Let us assume there is C:\folderA\a.bat
with the command lines:
@echo off
echo Running %~nx0
echo/
echo Current directory is: %CD%
echo Directory of %~nx0 is: %~dp0
And there is C:\folderA\folderB\b.bat
with the command lines:
@echo off
echo Running %~nx0
echo/
echo Current directory is: %CD%
echo Directory of %~nx0 is: %~dp0
echo/
call "%~dp0..\a.bat"
echo/
echo Running again %~nx0
echo/
echo Current directory is: %CD%
echo Directory of %~nx0 is: %~dp0
echo/
for /F %%I in ("%~dp0..\") do echo Batch parent directory is: %%~dpI
for /F %%I in ("%CD%\..\") do echo Current parent directory is: %%~dpI
Double clicking on batch file C:\folderA\folderB\b.bat
results in output:
Running b.bat
Current directory is: C:\folderA\folderB
Directory of b.bat is: C:\folderA\folderB\
Running a.bat
Current directory is: C:\folderA\folderB
Directory of a.bat is: C:\folderA\
Running again b.bat
Current directory is: C:\folderA\folderB
Directory of b.bat is: C:\folderA\folderB\
Batch parent directory is: C:\folderA\
Current parent directory is: C:\folderA\
Removing the command call
in line 7 of b.bat
and running again b.bat
results in output:
Running b.bat
Current directory is: C:\folderA\folderB
Directory of b.bat is: C:\folderA\folderB\
Running a.bat
Current directory is: C:\folderA\folderB
Directory of a.bat is: C:\folderA\
There is no return to b.bat
on reaching end of a.bat
without using call
.
Next let us look on output on running b.bat
having again call
on line 7 from C:\Windows\System32
with the command line C:\folderA\folderB\b.bat
:
Running b.bat
Current directory is: C:\Windows\system32
Directory of b.bat is: C:\folderA\folderB\
Running a.bat
Current directory is: C:\Windows\system32
Directory of a.bat is: C:\folderA\
Running again b.bat
Current directory is: C:\Windows\system32
Directory of b.bat is: C:\folderA\folderB\
Batch parent directory is: C:\folderA\
Current parent directory is: C:\Windows\
ATTENTION:
Path of current directory hold by environment variable CD
is without backslash at end with one exception: the current directory is the root directory of a drive. In this case CD
is for example C:\
instead of just C:
.
When working with %~dp0
please take into account:
What is the reason for batch file path referenced with %~dp0 sometimes changes on changing directory?