variablesbatch-filecmd

Store output from multiple batch commands in variables


I am trying to make a batch file run a couple of different commands, then display all the results together at the end.

SETLOCAL ENABLEDELAYEDEXPANSION
SET Counter=1
FOR /F "tokens=* USEBACKQ" %%F IN (`DISM /Online /Cleanup-Image /CheckHealth`) DO (
  SET "VariableA!Counter!=%%F"
  SET /a Counter=!Counter!+1)

SET Counter=1
FOR /F "tokens=* USEBACKQ" %%F IN (`DISM /Online /Cleanup-Image /ScanHealth`) DO (
  SET "VariableB!Counter!=%%F"
  SET /a Counter=!Counter!+1)

SET Counter=1
FOR /F "tokens=* USEBACKQ" %%F IN (`DISM /Online /Cleanup-Image /RestoreHealth`) DO (
  SET "VariableC!Counter!=%%F"
  SET /a Counter=!Counter!+1)

SET Counter=1
FOR /F "tokens=* USEBACKQ" %%F IN (`SFC /scannow`) DO (
  SET "VariableD!Counter!=%%F"
  SET /a Counter=!Counter!+1)

@ECHO %VariableA4%
@ECHO %VariableA5%
@ECHO %VariableB4%
@ECHO %VariableB5%
@ECHO %VariableC4%
@ECHO %VariableC5%
@ECHO %VariableD3%
@ECHO %VariableD4%

ENDLOCAL
pause

I tried the code posted above, but only got a result on the first two variables
@ECHO %VariableA4% & @ECHO %VariableA5% All the next variables did not display anything/ get assigned any value from 2'nd, 3'rd and 4'th command.

The normal output of each command:
DISM /Online /Cleanup-Image /CheckHealth

C:\WINDOWS\system32>DISM /Online /Cleanup-Image /CheckHealth

Deployment Image Servicing and Management tool
Version: 10.0.19041.3636

Image Version: 10.0.19045.4529

No component store corruption detected.
The operation completed successfully.

DISM /Online /Cleanup-Image /ScanHealth

C:\WINDOWS\system32>DISM /Online /Cleanup-Image /ScanHealth

Deployment Image Servicing and Management tool
Version: 10.0.19041.3636

Image Version: 10.0.19045.4529

[==========================100.0%==========================] No component store corruption detected.
The operation completed successfully.

DISM /Online /Cleanup-Image /RestoreHealth

C:\WINDOWS\system32>DISM /Online /Cleanup-Image /RestoreHealth

Deployment Image Servicing and Management tool
Version: 10.0.19041.3636

Image Version: 10.0.19045.4529

[==========================100.0%==========================] The restore operation completed successfully.
The operation completed successfully.

SFC /scannow

C:\WINDOWS\system32>SFC /scannow

Beginning system scan.  This process will take some time.

Beginning verification phase of system scan.
Verification 100% complete.

Windows Resource Protection did not find any integrity violations.

My goal is to make the CMD window display something like this at the very end:

    CheckHealth:
    No component store corruption detected.
    The operation completed successfully.
    (only line 4 and 5 of the normal command output)

    ScanHealth
    No component store corruption detected.
    The operation completed successfully.
    (only line 4 and 5 of the normal command output)

    RestoreHealth
    The restore operation completed successfully.
    The operation completed successfully.
    (only line 4 and 5 of the normal command output)

    SFC /scannow
    Verification 100% complete.
    Windows Resource Protection did not find any integrity violations.
    (only line 3 and 4 of the normal command output)

I appreciate the help anyone could give.


Solution

  • You need the last two lines and you know how many lines there will be (I trust your words here). So you can skip the first n (unneeded) lines. You just have to filter out the progress lines, because you don't know (or don't want to count) how many there are.

    I took the freedom to adapt your counter method a bit.

    @echo off
    setlocal enabledelayedexpansion
    set counter=0
    FOR /F "skip=5 tokens=* USEBACKQ" %%F IN (`dism /online /cleanup-image /checkhealth`) DO (
      SET /a Counter+=1
      SET "Variable!Counter!=%%F"
    )
    FOR /F "skip=5 tokens=* USEBACKQ" %%F IN (`DISM /Online /Cleanup-Image /ScanHealth^|find /v "["`) DO (
      SET /a Counter+=1
      SET "Variable!Counter!=%%F"
    )
      set variable
      set counter
    echo ====== Summary ======
    echo checkhealth:
    echo %variable1%
    echo %variable2%
    echo scanhealth:
    echo %variable3%
    echo %variable4%
    echo etc.
    

    Note: this will fail miserably if the number of lines differ (in case of an error or other unexpected output, or not running with admin privilege (less lines, nothing to process))