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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With