What is the current directory in a batch file?

Batch FileDirectory

Batch File Problem Overview


I want to create a few batch files to automate a program.

My question is when I create the batch file, what is the current directory? Is it the directory where the file is located or is it the same directory that appears in the command prompt, or something else?

Batch File Solutions


Solution 1 - Batch File

From within your batch file:

  • %cd% refers to the current working directory (variable)
  • %~dp0 refers to the full path to the batch file's directory (static)
  • %~dpnx0 and %~f0 both refer to the full path to the batch directory and file name (static).

See also: https://stackoverflow.com/questions/5034076/what-does-dp0-mean-and-how-does-it-work

Solution 2 - Batch File

It usually is the directory from which the batch file is started, but if you start the batch file from a shortcut, a different starting directory could be given. Also, when you'r in cmd, and your current directory is c:\dir3, you can still start the batch file using c:\dir1\dir2\batch.bat in which case, the current directory will be c:\dir3.

Solution 3 - Batch File

In a batch file, %cd% is the most commonly used command for the current directory, although you can set your own variable:

set mypath=%cd%
echo %mypath% (where %mypath% is the current directory that the batch file is sitting in)

So say you were wanting to open Myprog.exe. If it was in the same folder, you would use the command:

start %mypath%\Myprog.exe

That would open Myprog from the current folder.

The other option is to make a directory in C: called AutomatePrograms. Then, you transfer your files to that folder then you can open them using the following command:

start "" "C:\AutomatePrograms\Myprog1.exe"
start "" "C:\AutomatePrograms\Myprog2.exe"
start "" "C:\AutomatePrograms\Myprog3.exe"

Solution 4 - Batch File

Say you were opening a file in your current directory. The command would be:

 start %cd%\filename.filetype

I hope I answered your question.

Solution 5 - Batch File

It is the directory from where you run the command to execute your batch file.

As mentioned in the above answers you can add the below command to your script to verify:

> set current_dir=%cd%
> echo %current_dir%  

Solution 6 - Batch File

It is the directory from where you start the batch file. E.g. if your batch is in c:\dir1\dir2 and you do cd c:\dir3, then run the batch, the current directory will be c:\dir3.

Solution 7 - Batch File

%__CD__% , %CD% , %=C:%

There's also another dynamic variable %__CD__% which points to the current directory but unlike %CD% it has a backslash at the end. This can be useful if you want to append files to the current directory. Also %CD% does not work under disabled extensions environment ,but %__CD__% always works.

With %=C:% %=D:% you can access the last accessed directory for the corresponding drive. If the variable is not defined you haven't accessed the drive on the current cmd session.

And %__APPDIR__% expands to the executable that runs the current script a.k.a. cmd.exe directory.

Solution 8 - Batch File

Just my 2 cents.
The following command fails if called from batch file (Windows 7) placed on a pendrive:

%SystemRoot%\System32\xcopy.exe /e /i "%cd%Ala" "C:\KS\Ala\"

But this does the job:

%SystemRoot%\System32\xcopy.exe /e /i "%~dp0Ala" "C:\KS\Ala\"

Solution 9 - Batch File

Your bat file should be in the directory that the bat file is/was in when you opened it. However if you want to put it into a different directory you can do so with cd [whatever directory]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAaron de WindtView Question on Stackoverflow
Solution 1 - Batch FileJRLView Answer on Stackoverflow
Solution 2 - Batch FileGolezTrolView Answer on Stackoverflow
Solution 3 - Batch FileJ. BondView Answer on Stackoverflow
Solution 4 - Batch FilePing Multiple TimesView Answer on Stackoverflow
Solution 5 - Batch Fileanuj0901View Answer on Stackoverflow
Solution 6 - Batch Fileicyrock.comView Answer on Stackoverflow
Solution 7 - Batch FilenpocmakaView Answer on Stackoverflow
Solution 8 - Batch FileDarek AdamkiewiczView Answer on Stackoverflow
Solution 9 - Batch FilerobertView Answer on Stackoverflow