633

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?

MervS
  • 5,724
  • 3
  • 23
  • 37
Aaron de Windt
  • 16,794
  • 13
  • 47
  • 62

9 Answers9

1239

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: What does %~dp0 mean, and how does it work?

Community
  • 1
  • 1
JRL
  • 76,767
  • 18
  • 98
  • 146
  • 87
    Actually, it looks like %~dp0 gives the full path to the *directory* that the executing batch file is in. %~dpnx0 (which is equivalent to %~f0) gives the full path to the batch file. See http://www.robvanderwoude.com/parameters.php for more details. – deadlydog Jul 11 '13 at 20:08
  • 6
    See also good detailed answers of a similar question: [Get list of passed arguments in Windows batch script (.bat)](http://stackoverflow.com/q/357315/938111) – oHo Sep 23 '13 at 15:37
  • 3
    Unfortunately when run as a process from .net %~dp0 is the working directory not the batch files directory, Found this out the hard way. – trampster Jan 29 '18 at 22:44
  • 4
    As a more explicit example, when right-clicking on a `.cmd` file and running as administrator, `%cd%` gives `C:\WINDOWS\system32` and `%~dp0` gives the batch file directory with trailing slash. – icc97 Feb 27 '18 at 09:10
  • What about one batch STARTing or CALLing another? Do either of those affect `%~dp0`, `%~dpnx0`, or `%~f0`? I would assume these values are grabbed when the command window/cmd process starts, so START would change it and CALL would not, but we all know what happens when one *assumes*, especially with Windows. – Twisted on STRIKE at1687989253 May 22 '21 at 15:42
  • also is there a delayed expansion compatible version of these variables? I prefer to use delayed expansion for anything that supports it, but neither `!~dp0` nor `!~dp0!` seem to be valid – Twisted on STRIKE at1687989253 May 22 '21 at 17:08
  • `%~dp0` always ending with a backslash is not really static because of a bug of `cmd.exe`. For full details see [What is the reason for batch file path referenced with %~dp0 sometimes changes on changing directory?](https://stackoverflow.com/questions/12141482) – Mofi Jun 27 '21 at 08:06
  • 1
    @TwistedCode `%~dp0` is referencing drive and path of __argument 0__ of the batch file arguments. It is __NOT__ an *environment variable* reference. It is a __batch file argument__ reference. `%~dp0` expands to full path of __currently processed__ batch file. A batch file `C:\Temp\Devlopement & Test()!\Main.bat` with `%~dp0` expanding to ``C:\Temp\Devlopement & Test()!\`` on execution can `call` the batch file `C:\Temp\SubBatch.cmd`, for example with `call "%~dp0..\SubBatch.cmd"`, containing also `%~dp0` expanding on execution to ``C:\Temp\``. Run `call /?` in a cmd window for help. – Mofi Jun 27 '21 at 08:15
29

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.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
15

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"
Tom
  • 83
  • 2
  • 12
J. Bond
  • 466
  • 4
  • 7
  • 2
    As noted in the top answer here, `%cd%` is variable, so running the batch file from Windows Explorer as admin will give `C:\WINDOWS\system32` which is almost certainly not what you want. `%~dp0` is more consistent. – icc97 Feb 27 '18 at 09:14
10

%__CD__% , %CD% , %=C:% , %~dp0

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.

And with %~dp0 argument you can get the directory where the script itself is located (unless the shift command was used)

npocmaka
  • 55,367
  • 18
  • 148
  • 187
8

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

 start %cd%\filename.filetype

I hope I answered your question.

Pablo Claus
  • 5,886
  • 3
  • 29
  • 38
  • When calling e.g. via `.bat` file this easily resolves to `C:\Windows\filename.filepath` instead of using the location where the batchfile is located. Under these circumstances `%cd%` resolves to the location of CMD, not the location of the file. – Koenigsberg Mar 21 '23 at 17:55
5

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%  
g00glen00b
  • 41,995
  • 13
  • 95
  • 133
anuj0901
  • 573
  • 6
  • 8
4

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.

icyrock.com
  • 27,952
  • 4
  • 66
  • 85
1

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\"
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • This is the switch that I was looking for to determine the parent directory path of my bat file without that batch file name: `%~dp0` – Ammar Mohammad Aug 03 '19 at 12:49
0

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]

robert
  • 31
  • 9