0

Im writing a batch script to run on a flash drive. I need to validate the volume serial number of flash drive inside the code, therefore nobody should be able to run it from a different location.

Does anyone know how to validate serial number inside a batch file?

Example:

IF %VOL%==ABCD GOTO A ELSE EXIT
Joey
  • 344,408
  • 85
  • 689
  • 683
yrk
  • 67
  • 3
  • 10

2 Answers2

2

Although there are several different ways to achieve the same thing, the original DOS/Windows command intended to manage volumes and serial numbers is VOL:

@echo off
for /F "skip=1 tokens=5" %%a in ('vol %~D0') do set Serial=%%a
if %Serial% equ ABCD-EF01 do (
    echo Valid serial number!
)
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • currently i am using MS dos 6.22 , unfortunately these all solution couldn't help me to reach my requirement. please advice me to find out another way with correct syntax for proper execution on MS Dos 6.22 – yrk Jun 13 '12 at 08:11
  • @Aacini, +1: nice. Forgot about `vol`. – Eitan T Jun 13 '12 at 08:13
1

You can probably use WMIC and do something like:

wmic logicaldisk where drivetype=3 get volumeserialnumber

See parameter types in the link mentioned.

Edit:

@echo off
for /F "skip=1 delims=" %%j in ("wmic logicaldisk where deviceid = 'C:' get volumeserialnumber") do (
  set SERIAL=%%j
  goto :DONE
)
:DONE
echo  SERIAL=%SERIAL%

if "%SERIAL%"=="BLAH" (
   echo "Bluh"
)
David Brabant
  • 41,623
  • 16
  • 83
  • 111
  • @yrk - Change the where clause to look at the drive letter where the batch is executing from: `where deviceid = '%~d0'`. Also make sure to remove the hyphen from the right side of the IF comparison. DIR hyphenates the serial number, WMIC does not. – dbenham Jun 12 '12 at 12:02