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.
C:\Users\windmdline>echo The current directory is %CD% The current directory is C:\Users\wincmdline
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
Aww, Dude You have to Input some value after this command as –
echo hello i am someone
like this if you put, it will display the text – hello i am someone.
Hope you understand.
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.
%~dp0 is the variable you want.
It outputs the drive and path of the batch file.
EX: C:\mydir\myfile.bat becomes C:\mydir\
Here’s a MS reference, it works in thing other than Batch; like VBS SCCM, PS, shortcuts too.
Works like a charm, thanks :)
Thanks, this post has been very useful to me
Is there also a way to get the path to the batch that’s currently running? What I really want to do is make sure the batch I’m running is running in its own directory in case I forget to update the “start in” I have in my task scheduler for this… otherwise if I move or copy the directory, it will either not start what it’s supposed to or (in the case of copying) potentially worse.
Why use a strange, impossible to remember thing like “%~dp0” when you can just use %cd% instead? Duh.
It’s very easy to remember – the last character is zero, not O. In particular %0 is the name of the currently running program (i.e., the BAT file). Respectively %1, %2, etc. are the first, second, and so on command-line arguments (if any) supplied to it.
Of the rest of the letters, “d” stands for “drive” and “p” stands for “path”. (You can also use the letter “f”, which stands for the file name.) So, %~dp0 means “the drive and directory of the currently running BAT file”.
Now, why use this and not %CD%? Because they are two different things. “%~dp0” gives you the directory where the running BAT file resides, no matter where you are when running it. “%CD%” gives you the directory where you are when running the BAT file. This might be different from the directory where the file resides, if you aren’t running it from the current directory.
So, if you run the BAT file C:\FOO\BAR.BAT while you’re in the directory D:\SNAFU, “%~dp0” will give you “C:\FOO”, while “%CD%” will give you “D:\SNAFU”.
DustyG’s answer is the correct one:
%CD% returns the current directory; this may be fine to use for simple batch files, but the current directory can and often does change in a batch file, and a batch file could be called from another directory.
%~dp0 returns the directory where the batch file exists
Consider the following batch file stored at c:\users\myusername\documents\test.bat
:: start batch file
:: end batch file
Batch file output:
Note that the echo %CD% command does not show the folder that the batch file exists, it shows the current directory, while the echo %~dp0 always shows the folder containing the batch file – useful if you are needing to call another batch file (or other program or file) based on a relative location from the initial batch file.
Also consider if the user was in the command prompt located at: c:\dir1 and they called the batch file c:\dir2\test.bat The %CD% returns c:\dir1 which is the current directory, not c:\dir2 which is where the batch file is stored.
While SRINI’s answer matches the title of the article, it does not answer the original blog reader’s question, unless the first line of your batch file was to be something like:
set startdir=%CD% to save the initial directory into a variable to use later in the script.
n.b. another difference to be aware of is that the result of %CD% has no slash at the end while %~dp0 does.
An excellent resource covering this is: https://ss64.com/nt/syntax-args.html