2

I've been struggling to write a script that will find the drive index number from other properties of the drive. The script is as follows:

@echo off
REM batch file to load Veracrypt
Set "driveIndex="
for /f "skip=1 tokens=1 delims= " %%a in ('wmic diskdrive where "model ='WD Elements 1078 USB Device'" get index') do SET driveIndex=%%a & goto reportLetter

:reportLetter
if not defined driveIndex (
echo Error Occured!
pause
exit
) else (
echo \Device\Harddisk%driveIndex:~0%\Partition3
pause
exit
)

However, the output of the script is \Device\Harddisk1 \Partition3. I tried for a long long time but could get the script to give the following output: \Device\Harddisk1\Partition3.

Could anyone tell me how to correct the code to get the required output?

2
  • You may want to take a look at ss64.com/nt/syntax-replace.html Commented May 11, 2015 at 2:15
  • The problem is that wmic is outputting an additional blank line. Commented May 11, 2015 at 3:10

3 Answers 3

3

Try this

DO SET "driveIndex=%%a"

Your line

... do set driveIndex=%%a & goto ...

is interpreted as set driveIndex=%%a<space>& goto ..., this is, where the additional space in \Device\Harddisk1 \Partition3 comes from.

Of course you could write:

... do set driveIndex=%%a& goto ... 

but the better syntax is:

... do set "driveIndex=%%a" & goto ...

which eliminates any unintended spaces.

Note1: set is very picky with spaces. set var = hello creates a variable named var<space> with the value <space>hello<space>.

Note2:

set var="value" sets the value to "value"

set "var=value"sets the value to value. This syntax gives you full (and visible) control to the variable name and it's value.

Sign up to request clarification or add additional context in comments.

3 Comments

best answer until now, but you really should explain why this works better to make your answer a good one.
I thought more of an explanation for the quotes with the setcommand to avoid the space at the end of the variable - which is the problem in the question.
It worked! I don't know why those quotations worked, and I intend to find out, but it worked.
2
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET DRIVE_INDEX=

FOR /F "usebackq skip=1" %%i IN (`wmic diskdrive where "model = 'HGST HTS725050A7E630 ATA Device'" get index`) DO (
    IF "!DRIVE_INDEX!" EQU "" (SET DRIVE_INDEX=%%i)
)

ECHO DRIVE_INDEX is set to %DRIVE_INDEX%

2 Comments

Unfortunately, this is returning a blank %drive_index%
I revised the answer, please try it again. Thanks.
1

I believe that the problem is that WMIC output is Unicode.

I'd try

for /f "skip=1 tokens=1 delims= " %%a in ('wmic diskdrive where "model ='WD Elements 1078 USB Device'" get index^|more') do SET driveIndex=%%a & goto reportLetter

where the ^|more converts to ANSI. The caret escapes the pipe to tell cmd that the pipe is part of the command to be executed.

1 Comment

|more does not change anything for me. (WIN8.1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.