Batch file : How to get current directory
Here’s a question from a blog reader.
“I need to write a batch script file which can traverse to different directories and do some operations on those directories. Once done, I need to come back to the original directory where the batch script started and do some more stuff. I need to get the initial starting directory and save it in a variable. My question is what’s the simple way to get the the directory from batch script. ”
Below is the answer for this question.
There is a very simple way to get the directory from a batch script file. CD environment variable stores the current directory of a command window session. Just run the command ‘echo %CD%’ and check it yourself.
Another way to do what the reader wanted would be to use the pushd and popd commands to traverse directories like a stack.
outputs blank empty line
If you traverse the flag of the 7th decimal variable. The channel of corresponding flag will stay static in the field of the command to enable what you need
How to create a batch file that can organise files into specific folders that have been downloaded from the internet.
Example; if I was to download a number of different file types such as a pdf, mp4,mp3, or an app, they would normally download into the download file by default. I would like to take the many different file types in the download folder and organise them into specific folders within my libraries by batching them. Can this be done?
You can download all the files into the default folder. Next, create a subfolder for each of the file type you have. Next, run the command below.
Windows shell command to get the full path to the current directory?
Is there a Windows command line command that I can use to get the full path to the current working directory?
Also, how can I store this path inside a variable used in a batch file?
14 Answers 14
Use cd with no arguments if you’re using the shell directly, or %cd% if you want to use it in a batch file (it behaves like an environment variable).
You can set a batch/environment variable as follows:
sample screenshot from a Windows 7 x64 cmd.exe.
Update: if you do a SET var = %cd% instead of SET var=%cd% , below is what happens. Thanks to jeb.
Quote the Windows help for the set command ( set /? ):
Note the %CD% — expands to the current directory string. part.
This has always worked for me:
For Windows we can use
command is there.
For Windows, cd by itself will show you the current working directory.
For UNIX and workalike systems, pwd will perform the same task. You can also use the $PWD shell variable under some shells. I am not sure if Windows supports getting the current working directory via a shell variable or not.
On Windows:
CHDIR Displays the name of or changes the current directory.
In Linux:
PWD Displays the name of current directory.
Based on the follow up question (store the data in a variable) in the comments to the chdir post I’m betting he wants to store the current path to restore it after changeing directories.
The original user should look at «pushd», which changes directory and pushes the current one onto a stack that can be restored with a «popd». On any modern Windows cmd shell that is the way to go when making batch files.
If you really need to grab the current path then modern cmd shells also have a %CD% variable that you can easily stuff away in another variable for reference.
Get current batchfile directory
Firstly, I saw this topic but I couldn’t understand that.
Question :
There is a batch file in D:\path\to\file.bat with following content :
It must be D:\path\to
What am I doing wrong?
4 Answers 4
System read-only variable %CD% keeps the path of the caller of the batch, not the batch file location.
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 ).
You can refer to other files in the same folder as the batch script by using this syntax:
This can even be used in a subroutine, Echo %0 will give the call label but, echo «%
nx0″ will give you the filename of the batch script.
When the %0 variable is expanded, the result is enclosed in quotation marks.
dp0 will return path to batch location. echo %
f0 will return path to the batch with filename. – Stoleg Jun 12 ’13 at 12:03
dp0 ? – Ivailo Bardarov May 5 ’17 at 10:06
dp0 as first line of batch file and worked – mkb Oct 25 ’17 at 4:03
dp0″ rather than %
dp0 . Quotes will guarantee the path is taken as one token, even if there are spaces, and also protects against poison characters like & . – dbenham Dec 20 ’19 at 3:53
Within your .bat file:
You can now use the variable %mypath% to reference the file path to the .bat file. To verify the path is correct:
For example, a file called DIR.bat with the following contents
run from the directory g:\test\bat will echo that path in the DOS command window.
Here’s what I use at the top of all my batch files. I just copy/paste from my template folder.
Setting current batch file’s path to %batdir% allows you to call it in subsequent stmts in current batch file, regardless of where this batch file changes to. Using PUSHD allows you to use POPD to quickly set this batch file’s path to original %batdir%. Remember, if using %batdir%ExtraDir or %batdir%\ExtraDir (depending on which version used above, ending backslash or not) you will need to enclose the entire string in double quotes if path has spaces (i.e. «%batdir%ExtraDir»). You can always use PUSHD %
dp0. [https: // ss64.com/ nt/ syntax-args .html] has more on (%
Note that using (::) at beginning of a line makes it a comment line. More importantly, using :: allows you to include redirectors, pipes, special chars (i.e. | etc) in that comment.
Of course, Powershell does this and lots more.
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?
9 Answers 9
From within your batch file:
dp0 refers to the full path to the batch file’s directory (static)
%
f0 both refer to the full path to the batch directory and file name (static).
dp0 will always give the full path to the executing batch file. – dbenham Jun 12 ’13 at 11:19
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 robvanderwoude.com/parameters.php for more details. – deadlydog Jul 11 ’13 at 20:08
dp0 is the working directory not the batch files directory, Found this out the hard way. – trampster Jan 29 ’18 at 22:44
dp0 gives the batch file directory with trailing slash. – icc97 Feb 27 ’18 at 9:10
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 .
In a batch file, %cd% is the most commonly used command for the current directory, although you can set your own variable:
So say you were wanting to open Myprog.exe. If it was in the same folder, you would use the command:
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:
dp0 is more consistent. – icc97 Feb 27 ’18 at 9:14
Say you were opening a file in your current directory. The command would be:
I hope I answered your question.
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:
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 .
Just my 2 cents. The following command fails if called from batch file (Windows 7) placed on pendrive:
But this does the job:
dp0 – Ammar Mohammad Aug 3 ’19 at 12:49
%__CD__% , %CD% , %=C:%
There’s also another dynamic variable %__CD__% which points to the current directory but alike %CD% it has a backslash at the end. This can be useful if you want to append files to the current directory.
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.
Get current folder name by a DOS command?
Is it possible to get the current folder name (not current directory path) by using a DOS command? If so, how?
The closest I got was this but it doesn’t do it:
note: the above attempt was me attempting to tokenize the string and get the last token set as the CURR variable.
n* are supported in MS-DOS. (Windows’ cmd.exe is not DOS, it’s a native Windows program.) – user1686 Jul 7 ’10 at 12:05
10 Answers 10
Shortest way I have found:
or within a .bat script:
or in .bat with Get value in variable.
nx means file name and extension only
If you want to know the current location of the batch file (and if your Windows isn’t a very ancient release), type for /? in a ‘DOS box’ window. Scroll down. Read.
You’ll find out, that you can now read (from within the batch file) these variables:
This works for many cases. Assume, the batchfile is called mytest.bat . You may call it in different ways:
- ..\..\to\mytest.bat . (relative path)
- d:\path\to\mytest.bat . (full path)
- \\fileserver\sharename\mytest.bat . (path on remote share)
. and you’ll always get the right value in your variables.
dp0″ solves my problem also and its more elegant. – djangofan Aug 10 ’10 at 21:32
dpnx0 , isn’t? – luc Nov 4 ’16 at 8:49
I personally liked Toms answer, until it struggled with dots in dir names. This gave me a hint:
Tom’s answer is good, but if you have a directory name with a period in it (i.e. wxwidgets-2.9.4) you’ll only get the full name. So this would output wxwidgets-2.9 instead because the .4 has been treated as an extension (Yes, even though it’s a directory name!).
To get the full output name you have to add on the extension to the end:
and in batch file mode:
Or of course, set a variable in the batch file instead:
An other way is:
it works with «.» and spaces in pathname
What does it do?
put the whole filename (driveletter-path-filename-extension) into MyPath Var
remove filename and extension from MyPath var
It also works with UNC Paths. If you need the Backslash on the end of the Path. Remove the \ after MyPath in the second set command, eg.
dp0 – Amit Naidu Jun 16 ’13 at 11:12
You can get the current dir into a variable. One-liner:
Be careful of the backslash of the end of path, it has not to be backslash of the end.
My answer in this thread does it in 3 simple lines:
This works for me from a batch file. It returns the current working directory name.
Almost all of the above code options will lead to an error if you call the bat file from the outside by other script or program. For example, this code: for %%I in (.) do set CurrDirName=%%
nxI, when called from Notepad++, will output Notepad++, but I don’t need a parent Notepad++ folder name surely! It is a bad way. In addition, for the same reason, do not use %cd% and any other options. Use only the input parameters of this, maybe remotely located script. Otherwise, you will get the result for the calling, but not called script and what is written in its command line, but not what must be written in the command line of the called code. Use this code for avoid errors: