445

I know that %0 contains the full path of the batch script, e.g. c:\path\to\my\file\abc.bat

I want path to be equal to c:\path\to\my\file.

How can I achieve that?

4

9 Answers 9

736

%~dp0 will be the directory. Here's some documentation on all of the path modifiers. Fun stuff :-)

To remove the final backslash, you can use the :n,m substring syntax, like so:

SET mypath=%~dp0
echo %mypath:~0,-1%

I don't believe there's a way to combine the %0 syntax with the :~n,m syntax, unfortunately.

Sign up to request clarification or add additional context in comments.

14 Comments

Excellent... I've been using %~0\.. -- knew there had to be a better way! Also, you will probably want to enclose %~dp0 in double quotation marks ("") in case there's spaces in the directory name, etc.
The example in the answer works fine without quotation marks even when there is a space in the path. (e.g. SET msg=hello world works fine). However, when using %mypath% elsewhere you want to be careful to use it in quotes, although they're not needed for cd either.
It's "unfortunate" that those can't be combined, because the world definitely needs more %~dp0:~0,-1$ in it. Still--very nice answer.
instead of removing the trailing backslash you just need to add a . to the end. It'll work exactly the same as the current dir SET mypath=%~dp0.
If the trailing backslash disturbs, simply append a dot .; do not remove the \ as you do, because this will result in an unintentional relative path when the script is located in the root directory of a drive, since D:\ (absolute) and D: (relative) may point to different locations, depending on the current working directory, but D:\. is still absolute…
|
16

%~dp0 may be a relative path. To convert it to a full path, try something like this:

pushd %~dp0
set script_dir=%CD%
popd

4 Comments

Ok, so why not just use %~dp0 directly?
I imagine this was posted to address the problem mentioned in the accepted answer's comments -- %~dp0 can be relative, which may or may not be a problem depending on use case
%~dp0 can't contain a relative path, d stands for drive and p for path, how a drive could be relative?
In which world? I just tested this answer on Windows Server 2012 r2 and it turns out %~dp0 will be an absolute path even when the script was run as a relative path. Thanks to jeb's comment, I was not fooled by this answer. Why do people just make up stuff and go and start spreading their wild imagination to others. I have this colleague who does this, but I blamed his (young) age. I wish my down-vote would count.
15

You can use following script to get the path without trailing "\"

for %%i in ("%~dp0.") do SET "mypath=%%~fi"

3 Comments

This doesn't remove the filename from the path though (e.g. abc.txt in OP's example).
@dcp Er, it does, though.
@Kyle Strand - Yeah, I just tried it again and now it is working fine. I'm not sure what happened when I tried it originally, maybe I made a mistake somewhere in the script. Sorry for the confusion, and thanks for pointing it out.
15

%~dp0 - return the path from where script executed

But, important to know also below one:

%CD% - return the current path in runtime, for example if you get into other folders using "cd folder1", and then "cd folder2", it will return the full path until folder2 and not the original path where script located

Comments

3

You can use %~dp0, d means the drive only, p means the path only, 0 is the argument for the full filename of the batch file.

For example if the file path was C:\Users\Oliver\Desktop\example.bat then the argument would equal C:\Users\Oliver\Desktop\, also you can use the command set cpath=%~dp0 && set cpath=%cpath:~0,-1% and use the %cpath% variable to remove the trailing slash.

3 Comments

I can't see any more informations, than in the 9 years old answer
Extra information is "d means the drive only, …" etc. Thank you, @Hayz.
You can't remove the trailing backslash like that in a single line unless you use delayed expansion
2

Like mentioned above, %~dp0 will return the absolute drive and path of the current script. IMPORTANT NOTE: the implementation of %~dp0 is critically broken and will not work if the bat script is invoked via a path that is enclosed in quotes.

Microsoft is refusing to fix this bug due to cmd.exe being a legacy product. See https://github.com/microsoft/terminal/issues/15212 for details.

Comments

0

One solution if you know the file name:

set fullpath=%~f0
set dirpath=%fullpath:NAME_OF_UR_FILE.EXT=%

You may be able to use vars like $n0 with this, but this was good enough for my use case

Comments

-8

%cd% will give you the path of the directory from where the script is running.

Just run:

echo %cd%

1 Comment

%CD% is the current working folder, not the folder, where the batch file is stored. They can be the same location, but often they are not.
-12

That would be the %CD% variable.

@echo off
echo %CD%

%CD% returns the current directory the batch script is in.

8 Comments

%cd% returns the directory the script was run from, not the directory the script is in.
it only works if your script doesn't modify the the working directory. Try CD C:\Temp <CR> ECHO %CD% (<CR> is newline...)
Also, if you right-click on the script and select "Run as Administrator", the starting current directory is C:\Windows\System32 regardless of where the script is located.
Although it's not a direct answer to OP's question, this flavour of the functionality is exactly what I was looking for when I found this question. Thanks!
None of the other solutions posted appear to work for me on Win7 32bit cmd.exe, this is useful to me at least.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.