1.

Solve : Comparing variables?

Answer»

I have written the following MS Dos Batch File
@echo off
setlocal enabledelayedexpansion
set xserial=DCD24116T8
set xserial=%xserial:~0,10%
echo %xserial%

for /F "skip=1 delims=" %%j in ('WMIC bios get serialnumber') do (
set serial=%%j
echo !serial!
if NOT !serial==%xserial% goto DONE
)
endlocal
echo They are the same
goto END

:DONE
echo ========
echo %xserial%
echo !serial!
echo They are not the same
:END

When this Batch File is run in Command WINDOW the variables are echoed as the same but the if statement equates them to not being equal. What am I doing wrong??FWIW this command doesn't return a value on all machines.

d:\abc>wmic bios get serialnumber
SerialNumber



d:\abc>


Wmic usually ends the output with a single CR and as it stands your variable will be wrong. It also pads the string with trailing spaces in many cases, unless you use a CSV output.

This should help with setting the variable
Code: [Select]if not defined serial set serial=%%j
and then use echo "!serial!" and see if it has trailing spaces inside the double quotes.

This particular WMIC command produces three lines of output, a header, the actual serial number and a blank line. By using the skip parameter in the for loop you are positioning the file at the second record. So far so good, but the if NOT !serial==%xserial% goto DONE resolves to false. (ie. The serial numbers are equal). Consequently the for loops executes one final time with the third (blank) line where the serial numbers are not equal.

You might FIND it EASIER to do all the logic COMPARES within the for loop and quit after processing the single record with the serial number.

Code: [Select]@echo off
setlocal enabledelayedexpansion
set xserial=DCD24116T8

for /f "skip=1" %%i in ('wmic bios get serialnumber') do (
set serial=%%i
if %xserial% EQU !serial! echo %xserial% and !serial! are equal
if %xserial% NEQ !serial! echo %xserial% and !serial! are not equal
goto :eof
)

Thankyou - Sidewinder, that works for me!. The point has been taken about the "wmic bios get serialnumber" sometimes not returning a value - ie to supplied by oem provider. What unique identifier can be used to id a particular pc that will be more reliable. Will the Network Card be the answer and how would you read and isolate that variable into this batch file?Code: [Select]wmic nic get macaddress|find ":"
You might get more than one



Discussion

No Comment Found