374

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

I would path to be equal to c:\path\to\my\file

How could I achieve that ?

Daniel Earwicker
  • 108,589
  • 35
  • 194
  • 274
Misha Moroshko
  • 148,413
  • 200
  • 467
  • 700
  • 3
    possible duplicate of [Command line .cmd/.bat script, how to get directory of running script](http://stackoverflow.com/questions/130112/command-line-cmd-bat-script-how-to-get-directory-of-running-script) – Kevin Gosse Dec 24 '12 at 13:27
  • possible duplicate of [How to pass command line parameters to a batch file?](http://stackoverflow.com/questions/26551/how-to-pass-command-line-parameters-to-a-batch-file) – AStopher May 27 '15 at 15:48
  • 1
    BTW: %0 does not contain the full path if the bat is called with a relative command line. So "%~dpf0" would be more reliable for this case. – eckes Jan 14 '17 at 17:59

8 Answers8

625

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

jurl
  • 1,686
  • 1
  • 11
  • 15
Dean Harding
  • 67,567
  • 11
  • 132
  • 174
  • 8
    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 Sep 30 '10 at 03:56
  • 1
    Nice ! But, `%~dp0` contains the `\` at the end. Do you have an idea how to remove it ? – Misha Moroshko Sep 30 '10 at 03:56
  • 1
    @Misha: I assume you mean how to remove the '\' on the end. I've updated my answer with details. – Dean Harding Sep 30 '10 at 04:06
  • DeanHarding you should update the answer with the suggestion made by @Cameron, using the double quotation marks. :) – Rafael Oliveira Sep 26 '13 at 20:52
  • 2
    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. – Martin Pain Feb 19 '15 at 11:04
  • In Windows 8.1, the use of %~dp0 fails. It can also report the working directory of the calling program and not accurately the actual file location of the script itself. Not sure if this is related to the current security manager, but the calling program was a Runtime.exec Java call from our webserver. – The Coordinator Apr 10 '15 at 20:20
  • As a variation: `PUSHD %~dp0; set mypath=%CD%; popd` works also if the script was called with a relative path like `..\bin\myscript.cmd` – hvb Feb 02 '16 at 09:45
  • 2
    It's "unfortunate" that those can't be combined, because the world definitely needs more `%~dp0:~0,-1$` in it. Still--very nice answer. – Kyle Strand Sep 21 '16 at 05:04
  • Any way to make this method survive symbolic links? – forresthopkinsa Sep 19 '17 at 00:38
  • @StingyJack - here is updated link: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/call#batch-parameters – jurl Dec 03 '18 at 09:33
  • 1
    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 Dec 20 '19 at 01:40
  • 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 Jun 25 '20 at 17:04
  • **ATTENTION!** %~dp0 will break if you change the directory with `cd /d` to another drive! – Smit Johnth Jan 16 '21 at 17:09
14

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

pushd %~dp0
set script_dir=%CD%
popd
dss539
  • 6,410
  • 2
  • 31
  • 61
Arnaud
  • 285
  • 2
  • 3
  • 6
    Ok, so why not just use `%~dp0` directly? – jpaugh Mar 28 '16 at 19:04
  • 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 – Michael Mrozek Feb 06 '17 at 19:52
  • 13
    `%~dp0` can't contain a relative path, `d` stands for drive and `p` for path, how a drive could be relative? – jeb Mar 31 '17 at 15:30
  • 7
    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 May 25 '18 at 14:25
12

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

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

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

Adir D
  • 480
  • 3
  • 13
0

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.

Hayz
  • 98
  • 4
-4

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

Just run:

echo %cd%
Azametzin
  • 4,342
  • 12
  • 22
  • 38
  • 1
    `%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 Mar 31 '20 at 13:27
-8

I am working on a Windows 7 machine and I have ended up using the lines below to get the absolute folder path for my bash script.

I got to this solution after looking at http://www.linuxjournal.com/content/bash-parameter-expansion.

#Get the full aboslute filename.
filename=$0
#Remove everything after \. An extra \ seems to be necessary to escape something...
folder="${filename%\\*}"
#Echo...
echo $filename
echo $folder
Andy Korneyev
  • 25,238
  • 15
  • 65
  • 65
Jonas
  • 7
-8

That would be the %CD% variable.

@echo off
echo %CD%

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

Ruel
  • 14,301
  • 6
  • 34
  • 49
  • 36
    %cd% returns the directory the script was run from, not the directory the script is in. – Misha Moroshko Sep 30 '10 at 03:55
  • 4
    it only works if your script doesn't modify the the working directory. Try `CD C:\Temp ECHO %CD%` (`` is newline...) – Dean Harding Sep 30 '10 at 04:12
  • 6
    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 Sep 30 '10 at 04:40
  • 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 Mar 13 '14 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 Feb 05 '15 at 13:45
  • @Clifford I don't think this should be dependent on whether your version of Windows is 32 bit or 64 bit; something else is probably awry, and note that this *definitely* should not work if the working directory is not the directory containing the script. – Kyle Strand Sep 21 '16 at 05:10
  • @Clifford ....possibly you are not trying to solve the same issue as in the original question...? – Kyle Strand Sep 21 '16 at 05:11
  • @KyleStrand : This answer did do what I want but had been downvoted - I was merely pointing out that it is not entirely without value. I specified WIn7 32bit not because my experience was unique to that platform, but merely to indicate that on that platform *at least* it worked - albeit perhaps in some restricted sense - but better then not working at all as was the case with the other solutions. I have no idea other solutions failed and no longer really care; 18 months on however it is hardly worth resurrecting. – Clifford Sep 26 '16 at 20:37