Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the drive letter a batch script is running from?

I have a batch script on a CD. Whenever I try to run it and enter %~d0, it returns the C: drive instead of F:, which is my CD drive.

What is a way to find the drive letter?

like image 661
user2245624 Avatar asked Apr 04 '13 15:04

user2245624


People also ask

How do I know if a batch process is running?

bat. We can easily check whether a process is currently running or not using the tasklist toolkit. Tasklist allows us to check for current processes.

What is CD in batch script?

The cd command, also known as chdir (change directory), is a command-line shell command used to change the current working directory in various operating systems. It can be used in shell scripts and batch files. cd / chdir.

What does the symbol do in batch files?

The interpreter executes each line in turn, starting with the first. The @ symbol at the start of any line prevents the prompt from displaying that command as it is executed. The command ECHO OFF turns off the prompt permanently, or until it is turned on again.


1 Answers

Get the drive letter from the current directory with:

%cd:~0,2%

%~dp0 is pretty useful in a bat: it is the folder in which the executing bat file resides.

Perhaps at the top of your script, do something like:

set _SCRIPT_DRIVE=%~d0
set _SCRIPT_PATH=%~p0

and then echo it out to debug. %~d0 should be giving you what you want, but the other options I mentioned might be helpful in solving the challenge.

like image 130
bubba Avatar answered Sep 28 '22 02:09

bubba