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

WindowsBatch File

Windows Problem Overview


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 ?

Windows Solutions


Solution 1 - Windows

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

Solution 2 - Windows

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

pushd %~dp0
set script_dir=%CD%
popd

Solution 3 - Windows

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

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

Solution 4 - Windows

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.

Solution 5 - Windows

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

Solution 6 - Windows

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

Just run:

echo %cd%

Solution 7 - Windows

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

Solution 8 - Windows

That would be the %CD% variable.

@echo off
echo %CD%

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMisha MoroshkoView Question on Stackoverflow
Solution 1 - WindowsDean HardingView Answer on Stackoverflow
Solution 2 - WindowsArnaudView Answer on Stackoverflow
Solution 3 - WindowsduanView Answer on Stackoverflow
Solution 4 - WindowsHayzView Answer on Stackoverflow
Solution 5 - WindowsAdir DView Answer on Stackoverflow
Solution 6 - WindowsUddalak BiswasView Answer on Stackoverflow
Solution 7 - WindowsJonasView Answer on Stackoverflow
Solution 8 - WindowsRuelView Answer on Stackoverflow