Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 445
Misha Moroshko Avatar asked Sep 30 '10 03:09

Misha Moroshko


People also ask

How do I get the path of a batch file?

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.

How do I find the path in CMD?

Go to the destination folder and click on the path (highlights in blue). type cmd. Command prompt opens with the path set to your current folder.

What is path in batch file?

The PATH is an environment variable containing a list of directories, separated by semi-colons (;). It is used by the command processor to find the programs or executables it needs. The PATH variable can be set in two ways: SET PATH=c:\bat;c:\dos. or: PATH c:\bat;c:\dos.

Where is the batch file of Windows?

Open Start. Search for Command Prompt, right-click the top result, and select the Run as administrator option. Type the following command to run a Windows 10 batch file and press Enter: C:\PATH\TO\FOLDER\BATCH-NAME. bat.


1 Answers

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

like image 137
Dean Harding Avatar answered Sep 19 '22 17:09

Dean Harding