442

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

729

%~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.

14
  • 10
    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.
    – Cameron
    Commented Sep 30, 2010 at 3:56
  • 3
    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. Commented Feb 19, 2015 at 11:04
  • 5
    It's "unfortunate" that those can't be combined, because the world definitely needs more %~dp0:~0,-1$ in it. Still--very nice answer. Commented Sep 21, 2016 at 5:04
  • 3
    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.
    – phuclv
    Commented Dec 20, 2019 at 1:40
  • 4
    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…
    – aschipfl
    Commented Jun 25, 2020 at 17:04
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
  • 6
    Ok, so why not just use %~dp0 directly?
    – jpaugh
    Commented Mar 28, 2016 at 19:04
  • 1
    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 Commented Feb 6, 2017 at 19:52
  • 23
    %~dp0 can't contain a relative path, d stands for drive and p for path, how a drive could be relative?
    – jeb
    Commented Mar 31, 2017 at 15:30
  • 14
    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.
    – bitoolean
    Commented May 25, 2018 at 14:25
15

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

for %%i in ("%~dp0.") do SET "mypath=%%~fi"
3
  • This doesn't remove the filename from the path though (e.g. abc.txt in OP's example).
    – dcp
    Commented Aug 3, 2016 at 13:11
  • 5
    @dcp Er, it does, though. Commented Sep 21, 2016 at 5:09
  • 1
    @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.
    – dcp
    Commented Sep 21, 2016 at 12:26
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

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
  • 3
    I can't see any more informations, than in the 9 years old answer
    – jeb
    Commented Feb 22, 2019 at 6:53
  • 1
    Extra information is "d means the drive only, …" etc. Thank you, @Hayz. Commented Aug 6, 2019 at 22:00
  • 1
    You can't remove the trailing backslash like that in a single line unless you use delayed expansion
    – aschipfl
    Commented Jun 26, 2020 at 10:43
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.

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

-8

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

Just run:

echo %cd%
1
  • 6
    %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.
    – Stephan
    Commented Mar 31, 2020 at 13:27
-12

That would be the %CD% variable.

@echo off
echo %CD%

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

8
  • 42
    %cd% returns the directory the script was run from, not the directory the script is in. Commented Sep 30, 2010 at 3:55
  • 4
    it only works if your script doesn't modify the the working directory. Try CD C:\Temp <CR> ECHO %CD% (<CR> is newline...) Commented Sep 30, 2010 at 4:12
  • 7
    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.
    – Cameron
    Commented Sep 30, 2010 at 4:40
  • 1
    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!
    – Zoltán
    Commented Mar 13, 2014 at 16:07
  • None of the other solutions posted appear to work for me on Win7 32bit cmd.exe, this is useful to me at least.
    – Clifford
    Commented Feb 5, 2015 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.