Search code examples
for-loopbatch-filecmdcommand-line

How to find strings in a variable?


I'm working on a batch file to extract computer information from systeminfo command.

The first version of my script. (It's a French version of Windows 11 Enterprise.)

for /f "tokens=5,* delims= " %%a in ('systeminfo ^| find ^"Nom du syst^"') do set osname=%%a %%b
for /f "tokens=4,* delims= " %%a in ('systeminfo ^| find ^"Date d'installation^"') do set osinstall=%%a %%b
for /f "tokens=6,* delims= " %%a in ('systeminfo ^| find ^"Heure de d^"') do set lastboot=%%a %%b
for /f "tokens=4" %%a in ('systeminfo ^| find ^"physique totale^"') do set totalmem=%%a
for /f "tokens=4" %%a in ('systeminfo ^| find ^"physique disponible^"') do set freemem=%%a

It works well, but it is slow because each line launches systeminfo again.

Then I have found that one:

for /f "tokens=*" %%a in ('systeminfo ^| findstr /B /C:"Nom du syst" /C:"Date d'installation" /C:"Heure de d" /C:"physique totale" /C:"physique disponible"') do @echo %%a

It also works well, but the layout of the result does not fit my needs.
I would like to have each information in separate variables to make a custom layout.

Then I've tested:

systeminfo >systeminfo.txt

With:

for /f "tokens=5,* delims= " %%a in ('find ^"Nom du syst^" systeminfo.txt') do set osname=%%a %%b
for /f "tokens=4,* delims= " %%a in ('find ^"installation^" systeminfo.txt') do set osinstall=%%a %%b
for /f "tokens=6,* delims= " %%a in ('find ^"Heure de démarrage^" systeminfo.txt') do set lastboot=%%a %%b
for /f "tokens=4" %%a in ('find ^"physique totale^" systeminfo.txt') do set totalmem=%%a
for /f "tokens=4" %%a in ('find ^"physique disponible^" systeminfo.txt') do set freemem=%%a

It also worked well.

Next I wanted to avoid the file systeminfo.txt, but did not find a solution.

Is it possible to put the systeminfo result in a variable, and then use the For /F command to search strings in that variable?

The first part would be something like this:

for /f "tokens=*" %%a in ('systeminfo ^| findstr /B /C:"Nom du syst" /C:"Date d'installation" /C:"Heure de d" /C:"physique totale" /C:"physique disponible"') do set informations=%%a`

Then something like this:

for /f "tokens=5,* delims= " %%a in ('find ^"Nom du syst^" %informations%') do set osname=%%a %%b
for /f "tokens=4,* delims= " %%a in ('find ^"installation^" %informations%') do set osinstall=%%a %%b
for /f "tokens=6,* delims= " %%a in ('find ^"Heure de démarrage^" %informations%') do set lastboot=%%a %%b
for /f "tokens=4" %%a in ('find ^"physique totale^" %informations%') do set totalmem=%%a
for /f "tokens=4" %%a in ('find ^"physique disponible^" %informations%') do set freemem=%%a

I've tried to put systeminfo in a variable, then used for /f command, but I have a message telling me the file was not found.


Edit 01

That's the result of:

echo off
cls
echo.
systeminfo | findstr /B /C:"Nom du syst" /C:"Date d'installation" /C:"Heure de d" /C:"physique totale" /C:"physique disponible"
echo.
pause
Nom du système d'exploitation:              Microsoft Windows 11 Entreprise
Date d'installation originale:              12/08/2024, 15:04:54
Heure de démarrage du système:              28/01/2025, 07:30:08

Appuyez sur une touche pour continuer...

And that's the output I have using the batch with systeminfo.txt with that script:

echo off
cls
chcp 65001>nul
mode con:cols=120 lines=44
reg.exe ADD "HKCU\Software\Sysinternals\PsExec" /v EulaAccepted /t REG_DWORD /d 1 /f
reg.exe ADD "HKCU\Software\Sysinternals\PsGetsid" /v EulaAccepted /t REG_DWORD /d 1 /f
reg.exe ADD "HKCU\Software\Sysinternals\PsInfo" /v EulaAccepted /t REG_DWORD /d 1 /f
reg.exe ADD "HKCU\Software\Sysinternals\PsLoggedon" /v EulaAccepted /t REG_DWORD /d 1 /f
cls


echo.
systeminfo >systeminfo.txt
cls
for /f "tokens=5,* delims= " %%a in ('find ^"Nom du syst^" systeminfo.txt') do set osname=%%a %%b
for /f "tokens=4,* delims= " %%a in ('find ^"installation^" systeminfo.txt') do set osinstall=%%a %%b
for /f "tokens=6,* delims= " %%a in ('find ^"Heure de démarrage^" systeminfo.txt') do set lastboot=%%a %%b
cls
for /f "tokens=3" %%a in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" /V "ComputerName"') do set psname=%%a
for /f "tokens=3" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "CurrentVersion"') do set veros=%%a
for /f "tokens=3" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "DisplayVersion"') do set dispver=%%a
for /f "tokens=3" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "CurrentBuild"') do set build=%%a
for /f "usebackq delims== skip=1" %%a in (`.\PsGetsid64 \\%psname%`) do set computerid=%%a
for /f "tokens=4" %%a in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" /v "NV Domain"') do set domaine=%%a
for /f "tokens=3" %%a in ('reg query "HKEY_CURRENT_USER\Volatile Environment" /V "LOGONSERVER"') do set srvauth=%%a
cls
for /f "tokens=3* delims= " %%a in ('reg query "HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\BIOS" /v "SystemManufacturer"') do set mannme=%%a %%b
for /f "tokens=3* delims= " %%a in ('reg query "HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\BIOS" /v "BaseBoardProduct"') do set mbmodel=%%a
for /f "tokens=3* delims= " %%a in ('reg query "HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\BIOS" /v "BaseBoardManufacturer"') do set mbmanufacturer=%%a %%b
for /f "tokens=3* delims= " %%a in ('reg query "HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0" /v "ProcessorNameString"') do set cpu=%%a %%b
for /f "usebackq tokens=3,*" %%a in (`.\PsInfo64 \\%psname% ^| find ^"Processor speed^"`) do set cpuspeed=%%a %%b
cls
echo.
for /f "tokens=4" %%a in ('find ^"physique totale^" systeminfo.txt') do set totalmem=%%a
for /f "tokens=4" %%a in ('find ^"physique disponible^" systeminfo.txt') do set freemem=%%a
cls
del systeminfo.txt
cls
echo.
echo Configuration du poste :
echo.
echo.
echo Nom de l'ordinateur : %psname%
echo.
echo Version de Windows : %osname% (%veros%) - %dispver% - build %build%
echo.
echo Date d'installation : %osinstall%
echo.
echo Heure du dernier démarrage : %lastboot%
echo.
echo.
echo Computer ID : %computerid%
echo.
echo Domaine : %domaine%
echo.
echo Serveur d'authentification : %srvauth%
echo.
echo.
echo Marque de l'ordinateur : %mannme%
echo.
echo Carte mere : %mbmodel% - %mbmanufacturer%
echo.
echo Processeur : %cpu% %cpuspeed%
echo.
psinfo64 -d "Volume Type"
echo.
pause
Configuration du poste :


Nom de l'ordinateur :  ………………………

Version de Windows : Microsoft Windows 11 Entreprise (6.3) - 23H2 - build 22631

Date d'installation : 12/08/2024, 15:04:54

Heure du dernier démarrage : 28/01/2025, 07:30:08


Comnputer ID : …………………………………………………………………………………………………

Domaine : ……………………………………

Serveur d'authentification : ………………


Marque de l'ordinateur : LENOVO

Carte mere : 1057 - LENOVO

Processeur : 12th Gen Intel(R) Core(TM) i7-12700 2.1 GHz


PSInfo v1.78 - Local and remote system information viewer
Copyright (C) 2001-2016 Mark Russinovich
Sysinternals - www.sysinternals.com

System information for \\PS-0082604:

Volume Type       Format     Label                      Size       Free  Free
    C: Fixed      NTFS       Windows               150.00 GB   35.66 GB  23.8%
    D: Fixed      NTFS       DATA                  326.33 GB  231.10 GB  70.S%
    E: Fixed      NTFS       BIG DATA              931.51 GB  646.54 GB  69.4%
    F: Fixed      NTFS       SSD480                446.52 GB  354.67 GB  79.4%

Appuyez sur une touche pour continuer...

There are lot more information. Most of them are found in the registry, but some of them are found with Sysinternals software and other with systeminfo only.


Edit 02

@Aacini

Here is your script which I've tested. (I've added chcp 65001>nul for the accents we have in French, and modified the first token from 1,2 to 1,*.

@echo off
chcp 65001>nul
setlocal EnableDelayedExpansion

for /F "tokens=1,* delims=:" %%a in ('systeminfo 2^>NUL') do (
   if "%%b" neq "" (
      set "var=%%a"
      for /F "tokens=*" %%v in ("%%b") do set "!var: =_!=%%v"
   )
)
cls
echo.
echo Nom de l'hôte : %Nom_de_l_'hôte%
echo.
echo Nom du système d'exploitation : %Nom_du_système_d'exploitation%
echo.
echo Version du système : %Version_du_système%
echo.
pause

That's the output of that code:

Nom de l'hôte :

Nom du système d'exploitation :

Version du système : 10.0.22631 N/A build 22631

Appuyez sur une touche pour continuer...

As you see, only Version du système gives a result.


Edit 03

Echo variable - UTF-8 - no chcp

"Nom_du_système_d'exploitation=Microsoft Windows 11 Entreprise"
"Version_du_système=10.0.22631 N/A build 22631"

Output - UTF-8 - no chcp

NOM :
VERSION :

Appuyez sur une touche pour continuer...

And

Echo variable - UTF-8 - chcp 65001

"Nom_du_système_dʼexploitation=Microsoft Windows 11 Entreprise"
"Version_du_système=10.0.22631 N/A build 22631"

Output - UTF-8 - chcp 65001

NOM :
VERSION : 10.0.22631 N/A build 22631

Appuyez sur une touche pour continuer...

Solution

  • I would like to present you a very simple solution:

    @echo off
    setlocal EnableDelayedExpansion
    
    for /F "tokens=1,2 delims=:" %%a in ('systeminfo 2^>NUL') do (
       if "%%b" neq "" (
          set "var=%%a"
          for /F "tokens=*" %%v in ("%%b") do set "!var: =_!=%%v"
       )
    )
    

    This code takes all the information provided by sysinfo command and store they in same named variables!!! (with the spaces changed by underscores)

    This means that after that code, you can show the value of any variable you want. For example:

    echo Nom du syst=%Nom_du_syst%
    echo Date d'installation=%Date_d'installation%
    

    Or extract/manipulate such an information in any way you wish. For example:

    for /f "tokens=2,* delims= " %%a in ("%Nom_du_syst%") do set osname=%%a  %%b
    

    That is it!