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?
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.
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.
Display a list of files and subfolders.
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 ).
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?
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With