Search code examples
batch-filefindstr

Batch file using systeminfo with findstr result in one liner


I'm trying to create a one liner output for this code:

@echo off &SETLOCAL
del Result.txt
echo %date% %time% > Result.txt
FOR /F "tokens=*" %%L IN (hostname.txt) DO (
    systeminfo /s %%L|findstr /c:"System Boot Time" /c:"Host Name" >> Result.txt

)

but it's on 2 liners. Any way of getting a one liner result?

I would like all output on 1 line for each loop

Results should be like:

Wed 01/22/2014  9:32:31.08
Host Name: Test1 System Boot Time: 1/17/2014, 6:41:07 PM
Host Name: Test2 System Boot Time: 1/17/2014, 6:41:57 PM

Solution

  • @echo off &SETLOCAL EnableDelayedExpansion
    set "out="
    FOR /F "tokens=*" %%L IN (hostname.txt) DO (
        for /F "delims=" %%M in ('systeminfo /s %%L^|findstr /c:"System Boot Time" /c:"Host Name"') do set "out=!out! %%M"
    )
    echo %date% %time% - %out% > Result.txt
    

    Previous Batch file create Result.txt file with one line! If you want separated lines for the date and time, replace last line by these ones:

    (
    echo %date% %time%
    echo %out%
    ) > Result.txt