简体   繁体   中英

How to determine the current drive letter in autostart in Windows?

Under Windows 7 I created a custom autostart entry for an usb device. This entry starts a batch file on my harddisk, not on the usb-drive. My harddisk has always the same drive letter. How do I tell the batch file, which drive letter is being assigned to the usb device or how do I get this in the batch file itself?

I found that it is not %~d0

I'm surprised %~d0 does not work, yet your batch file runs.

If you can assign a unique volume label to your USB device, then you can try the following:

for /f %%D in ('wmic volume get driveLetter^, label ^| find "yourLabel"') do set drive=%%D

EDIT after reading comment - OK, %~d0 should not work because it will simply give the drive letter where your bat file resides - your hard drive.

I still think WMIC is the utility you will need. If you are looking for a specific device, then I think my original answer might work. Another possibility is to substitute serialNumber for label in the wmic command. Plug your device in, identify which drive letter it is manualy, and then type wmic volume get driveLetter, serialNumber from the command line to identify what serial number you are looking for. Then you can use the following in your batch file

for /f %%D in ('wmic volume get driveLetter^, serialNumber ^| find "yourSerialNumber"') do set drive=%%D

Another possibility might be to put your batch file on the usb-device. If you can get auto-start to launch the batch file from your device, then you can use %~d0 to get the assigned drive letter.

cd /d %~dp0\ 

it equals cd /dd:\\currentpath\\

%0 is your batch file path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM