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?
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?
%~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.
%~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.
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
%~dp0:~0,-1$
in it. Still--very nice answer.
Commented
Sep 21, 2016 at 5:04
.
to the end. It'll work exactly the same as the current dir SET mypath=%~dp0.
.
; 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…
%~dp0
may be a relative path.
To convert it to a full path, try something like this:
pushd %~dp0
set script_dir=%CD%
popd
%~dp0
can be relative, which may or may not be a problem depending on use case
Commented
Feb 6, 2017 at 19:52
%~dp0
can't contain a relative path, d
stands for drive and p
for path, how a drive could be relative?
%~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.
Commented
May 25, 2018 at 14:25
You can use following script to get the path without trailing "\"
for %%i in ("%~dp0.") do SET "mypath=%%~fi"
%~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
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.
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.
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
%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.
That would be the %CD%
variable.
@echo off
echo %CD%
%CD%
returns the current directory the batch script is in.
CD C:\Temp <CR> ECHO %CD%
(<CR>
is newline...)
Commented
Sep 30, 2010 at 4:12