Windows Batch : query wmic computersystem get model if equals true continue with windows batch -


looking write script query wmic computersystem model , if value true continue batch file if not true echo un-compatible system exiting, have far

    @echo off          /f "skip=1" %%? in ('wmic computersystem model') "(        if %%?' equ test' ('goto start') else (        if %%?' equ test1' ('goto start') else (        if %%?' equ test2' ('goto start') else (        if %%?' equ test3' ('goto start') else (        echo un-compatible system exiting...>nul)))))"  :start  start of script 

im ok scripting never used if statements kinda lost on one.

any welcomed.

@echo off     setlocal enableextensions disabledelayedexpansion      /f "tokens=2 delims==" %%a in (         'wmic computersystem model /value'     ) /f "delims=" %%b in ("%%~a") %%m in (         "model1" "model2" "model3" "model4"     ) if /i "%%~b"=="%%~m" (         set "model=%%~m"         goto start     )      echo un-compatible system     goto :eof  :start     echo start of script model [%model%] 

where for loops are

  • %%a retrieve model
  • %%b remove ending carriage return in value returned (wmic behaviour)
  • %%m iterate on list of allowed models

if of allowed models matches 1 retrieved wmic, code jumps start label, else, inner for loop ends , no match has been found script ends.

this can simplified

>nul (wmic computersystem model |findstr /i /l /c:"model1" /c:"model2" /c:"model3")||(     echo un-compatible system     goto :eof ) echo compatible system 

where conditional execution operation used determine if findstr command fails found of models , cancel execution.

of course, can use cascade of if /else, syntax little different

for /f "tokens=2 delims==" %%a in (     'wmic computersystem model /value' ) /f "delims=" %%b in ("%%~a") (     if /i "%%~b"=="test1" goto start      if /i "%%~b"=="test2" goto start      if /i "%%~b"=="test4" goto start  ) echo un-compatible system goto :eof 

or

for /f "tokens=2 delims==" %%a in (     'wmic computersystem model /value' ) /f "delims=" %%b in ("%%~a") (            if /i "%%~b"=="test1" ( goto start      ) else if /i "%%~b"=="test2" ( goto start      ) else if /i "%%~b"=="test3" ( goto start     ) else (         echo un-compatible system         goto :eof     ) ) 

or

for /f "tokens=2 delims==" %%a in (     'wmic computersystem model /value' ) /f "delims=" %%b in ("%%~a") (     if /i "%%~b"=="test1" (          goto start      ) else if /i "%%~b"=="test2" (          goto start      ) else if /i "%%~b"=="test3" (          goto start     ) else (         echo un-compatible system         goto :eof     ) ) 

or whatever other combination/style better fits code, have take in consideration placement of parenthesis matters. if opening parenthesis needs on same line if command. if closing parenthesis needs in same line else clause (if present). else opening parenthesis needs in same line else clause.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -