Could someone explain this batch code to me?

Asked

Viewed 107 times

-3

Could someone explain this code to me?

I know what this is about, but I can’t apply it.

for /f "tokens=2 delims== " %%A in ('wmic baseboard get serialnumber /value ^| find /i "SerialNumber="') do set serialnumber=%%A
if /i not "%serialnumber%" == "PF0TPMUN" goto :exit

1 answer

1

The for /f is a command for use in loopings, where it will allow to obtain and manipulate the information that returns from its execution, whether by invoking a program, an internal/external command of the system, another for looping, another script, finally, one or more commands, and their respective outputs will be submitted to the treatments that the author of the code has developed, and that will summarize in actions that result in something useful for him in some way.

For /f can also be used to process the contents of files (readable, printable, understand "text"), and as per the purpose of the user, the for /f will open, read, write, and develop actions with the data(s) of the file(s) and/or the commands outputs that are passed within the loop actions, either in a previous filtering action on the output contents, or in the manipulations that return from that execution, using options such as using quotes with usebackq, skipping one or more lines at the exit, with skip, define which/which ones to pick with tokens, and either na/no a1st token, and/or o2nd, and/or the 3rd, and/or all (*) of the remaining occurrences, in addition to also defining or/not, an appropriate delimiter (or multiples if applicable) with delims, and also what character you will use to define/limit the end of a line, with eol.

But now that you have a brief notion of what a command involves for /f, you can try to see in this your command for /f, that will execute thewmic.exe already passing some arguments to get/return the serial number of the computer’s motherboard, besides having already defined that of this output, it will use only the text/strings that comes in the second token, assuming as bounders (multiples), the equals sign and the space.

Note that in this execution, the strings resulting from this command wmic + for, and, in fact, they are doing a direct pre-selection/(filtering) of the output of the execution, and manipulating the strings as defined in the arguments of the wmic /value with the options of for /f tokens=2 delims== ", to then send/direct what is left to use within the loop by the following commands, such as the set, that vei set a stored variable the strings present in the card serial, and the if that will test a condition and compare what returned with a string "PF0TPMUN", if the condition is true in the comparison of equality of the string "known", the bat follows the actions immediately after that loop, but if they are not equal, goes to the label :exit and execute the commands you have "queued".

On the use of multiplicity in the delimiter, this is dispensable to obtain the serial, understanding that the execution wmic you won’t need the argument /value, which end up forcing the use of the options of for, but, in doing the removal of this argument, you can also remove the for /f "tokens=2 delims== ", and use only the skip^=1 in his for, that will skip the first line at the exit, and with the absence of a defined tokens/delims, the "tokens=1 delims= " by default will be used.

Note the wmic without /value, and the for /f just using skip^=1:

  • And see the output with additional line on account of Unicode’s output wmic, and is visible in the first execution, but removed in the second with the replacement of find "SerialNumber=" pelo simples uso do **findstr .** // siga o ^< nul` in the first command, and lose sight of it in the second execution:

On the additional line on behalf of Unicode in the output of wmic

inserir a descrição da imagem aqui


Note: 1 In the case of running on a netbook/laptop/desktop branded/manufactured computer, it is common for the serial number of the motherboard to be the same as the serial number of the bios.

Note: 2 I would use wmic bios get serialnumber direct, then move on to a hash-like conversion, and that string on md5, or Shada vida, is what I would use for comparison, oh yes, the editing of the code would not make it "easy" to change the manipulation of "validator, and this using the certUtil.exe that already comes with Windows.


    O comando wmic baseboard get serialnumber /value
    Saida do comando: SerialNumber=PE03A187      -> = // Meu Computador
    Delimitadores 2 : "delims== " =igual+espaço  -> = // Delims==' ' 
    Saida com for /F: tokens 1    =tokens 2      -> = // Tokens=2 
                      ignorado    =ocorrência    -> = // PE03A187

Note: 3 There is no second sign of equals, which comes after first is also the second token!

  • For help and where to find the commands used:
     if | Comando: Interno do cmd.exe
        | Caminho: C:\WINDOWS\System32\CMD.exe
        |   Ajuda: C:\Windows\System32\cmd.exe /k if /?
        
    set | Comando: Interno do cmd.exe 
        | Caminho: C:\WINDOWS\System32\CMD.exe
        |   Ajuda: C:\Windows\System32\cmd.exe /k set /? 

    for | Comando: Interno do cmd.exe
        | Caminho: C:\WINDOWS\System32\CMD.exe
        |   Ajuda: C:\Windows\System32\cmd.exe /k for /? 
            
   find | Comando: Interno do cmd.exe
        | Caminho: C:\WINDOWS\System32\CMD.exe
        |   Ajuda: C:\Windows\System32\cmd.exe /k find /?
            
   goto | Comando: Interno do cmd.exe 
  ref.: | Caminho: C:\WINDOWS\System32\CMD.exe
  :exit |   Ajuda: C:\Windows\System32\cmd.exe /k goto /?
            
   wmic | Comando: Externo do Windows
        | Caminho: C:\Windows\System32\wbem\WMIC.exe
        |   Ajuda: C:\Windows\System32\wbem\WMIC.exe /? 

findstr | Comando: Externo do Windows
        | Caminho: C:\Windows\System32\findstr.exe
        |   Ajuda: C:\Windows\System32\cmd.exe /k findstr /?

        rem :: Os comandos listados aqui funcionam para linda de comando e
               também no iniciar / executar do Windows, case quera copiar
               as saídas para ler de outra forma(editor de text) adicione
               | clip e cole onde desejar: 
                 Ex.:   for /? | clip  
  • A suggested edit/change in code for your bat:

The ^< nul set^ is an all-line outlet, where the output will be treated as text escaping any non-alphanumeric character that may come from a "Ching Ling" style board, and with the findstr . to find any character, helping to deal with the line that juto from Unicode that has been bringing an additional at the end of the string. Already the skip^=1 is to skip/skip the first line at the exit of the wmic, and one other thing, at your command set, seeks to use quotes set AbreAspasVariavel=ValorFecheAspas == set "SerialNumber=PE03A187"

for /f skip^=1 %%i in ('^<con: wmic.exe baseboard get serialnumber^|findstr .
')do ^< nul set^ "SerialNumber=%%~i" & if not "%%~A" == "PF0TPMUN" goto=:exit

Browser other questions tagged

You are not signed in. Login or sign up in order to post.