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 ?
%~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.
%~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
directly?
%~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?
%~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.
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.
%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...)
Success story sharing
%~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 forcd
either.%~dp0:~0,-1$
in it. Still--very nice answer..
to the end. It'll work exactly the same as the current dirSET 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, sinceD:\
(absolute) andD:
(relative) may point to different locations, depending on the current working directory, butD:\.
is still absolute…