ChatGPT解决这个技术问题 Extra ChatGPT

How to get the path of the batch script in Windows?

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 ?

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.

j
jurl

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


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…
d
dss539

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

pushd %~dp0
set script_dir=%CD%
popd

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.
d
duan

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

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

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.
A
Adir D

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


H
Hayz

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.


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
A
Azametzin

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

Just run:

echo %cd%

%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.
R
Ruel

That would be the %CD% variable.

@echo off
echo %CD%

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


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