1

I want to cd to USB without specifying it because it changes its letter on every other computer.

screenshot

2

I'm going to assume you are asking how to use "cd" using a volume label rather than a drive letter.

The trick is to query WMI to get the drive letter from a given label, save the result to a variable, and the pushd that variable path.

@ECHO OFF
FOR /F "tokens=1 skip=1" %%A IN ('WMIC logicaldisk where "volumename="%1"" get name') DO (
pushd %%A
goto fileend
)
:fileend

After the first result returned from WMIC, the loop will exit (Although, only one path should be returned anyways).

To use the script, save it as something like "cdlabel.cmd" anywhere in the PATH like System32, or in a easy-to-reach location. Then call it with the volume name as an argument like the following: "cdlabel GamingDrive"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.