0
votes

I'm running a WMIC lookup against a series of remote client machines, pulling in Model and serial number.

For /F "tokens=*" %%b in ('wmic /node:%device% computersystem get Model /value^|find "Model"') do Set model=%%b

FOR /F "tokens=*" %%c in ('wmic /node:%device% bios GET serialnumber /value^|find "SerialNumber=" ') do set Serialnumber=%%c

The problem I have is that (for example) %serialnumber% is set to:SerialNumber=CNU8424GP3

I don't want the 'Serialnumber=', I just want the Serial number itself.

The only way I've found of stripping this is:

set SerialNumber=!serialnumber:SerialNumber=!

but this leaves an equals sign at the beginning of the line. So the final output is =CNUBFZXXY, what I would like to do is to remove the leading =, and I haven't been able to.

Any Suggestions?

5

5 Answers

2
votes

Add delims== to your for statements and change tokens=* to tokens=1-2. The values you want will then be in the second for loop variables:

For /F "delims== tokens=1-2" %%b in ('wmic /node:%device% computersystem get Model /value^|find "Model"') do set model=%%c
For /F "delims== tokens=1-2" %%c in ('wmic /node:%device% bios GET serialnumber /value^|find "SerialNumber=" ') do set Serialnumber=%%d
2
votes

Either split up the result using a for-statement with "delims==" (already answered), or trim up to and including the equal-sign – your example code was very close already:

set SerialNumber=!SerialNumber:SerialNumber=!
set SerialNumber=!SerialNumber:~1!
1
votes

The snippet below will allow you to remove the first and last character. I've played a trick here and added a matching '=' at the end of the string (NOTE: I could have used any character). I couldn't find a way to get up to the last character, so the trick is to just add on any character at the end of the variable and then strip it with the 1,-1 which means skip the first charcter and the negative value for the right side means 'counting back from the right'.

set serialnumber=%serialnumber%=
set serialnumber=%serialnumber:1-1%

Hope this helps people looking to hack DOS batch scripts in the future. A good short description of string manipulation can be found here: DOS String Manipulation

0
votes

It is possible to retrieve specific characters from a string variable.

Syntax %variable:~num_chars_to_skip% %variable:~num_chars_to_skip,num_chars_to_keep%

This can include negative numbers:

  %variable:~num_chars_to_skip, -num_chars_to_skip%
  %variable:~-num_chars_to_skip,num_chars_to_keep%

Example

 SET _test=012345
 SET _result=%_test:~1,6%
 ECHO %_result%          =12345
0
votes

Just do this instead it's much easier. USE QUOTATION MARKS AROUND BOTH VARIABLE AND STRING on the IF statement

IF "%VARIABLE%"=="equalsign=123456" goto Z

:Z

SET VARIABLE=%VARIABLE:~10%

echo %VARIABLE%

The output should be 123456. No equal sign.

That should do it. Seriously, that was really making me go nuts trying to figure that out. Someone else recently helped me with that. Never thought it would be so easy lol.