Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the current directory in a batch file?

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?

like image 403
Aaron de Windt Avatar asked Dec 12 '10 00:12

Aaron de Windt


People also ask

What is current directory in CMD?

The CWD (Current Working Directory) is a path (of a directory) inside the file system, where the shell is currently working. The current working directory is essential for resolving relative paths. Cd is a generic command found in the Command Interpreter of most operating systems.

How do I find current directory?

To determine the exact location of your current directory within the file system, go to a shell prompt and type the command pwd. This tells you that you are in the user sam's directory, which is in the /home directory. The command pwd stands for print working directory.

What is DIR in batch file?

Display a list of files and subfolders.

How do I find the location of a batch file?

You can get the name of the batch script itself as typed by the user with %0 (e.g. scripts\mybatch. bat ). Parameter extensions can be applied to this so %~dp0 will return the Drive and Path to the batch script (e.g. W:\scripts\ ) and %~f0 will return the full pathname (e.g. W:\scripts\mybatch. cmd ).


2 Answers

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?

like image 184
JRL Avatar answered Oct 16 '22 23:10

JRL


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.

like image 33
GolezTrol Avatar answered Oct 17 '22 00:10

GolezTrol