-
-
Save Ravlissimo/c76c881262608150325b823220c60112 to your computer and use it in GitHub Desktop.
[Batch] Extract Variable Set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
setlocal EnableExtensions EnableDelayedExpansion | |
:: Setup | |
set "ResultsFolder=results" | |
set "ExportFile=output.txt" | |
:: Verify that the Results folder exists | |
if not exist "%ResultsFolder%\*" md "%ResultsFolder%" | |
set "OutputFile=%ResultsFolder%\%ExportFile%" | |
:: Set the Working Directory | |
cd data | |
:: Empty the results file. | |
<nul set /p "=">%OutputFile% | |
:: Loop through the directory /a:d and its subdirectories /s. | |
for /f "delims=" %%F in ('dir /a:d /b /s') do if exist "%%~fF" ( | |
call :Reset | |
rem Loop through the file contents and parse each line. | |
for /f "usebackq delims=" %%L in ("%%~fF") do ( | |
for /f "tokens=1,2,*" %%X in ("%%L") do ( | |
rem Keep track of the variables. | |
if "%%Y"=="!namenum!" set "name=%%Z" & set /a "count+=1" | |
if "%%Y"=="!hostnum!" set "host=%%Z" & set /a "count+=2" | |
if "%%Y"=="!yearnum!" set "year=%%Z" & set /a "count+=4" | |
if "%%Y"=="!monthnum!" set "month=%%Z" & set /a "count+=8" | |
if "%%Y"=="!daynum!" set "day=%%Z" & set /a "count+=16" | |
if "%%Y"=="!hournum!" set "hour=%%Z" & set /a "count+=32" | |
if "%%Y"=="!minutenum!" set "minute=%%Z" & set /a "count+=64" | |
if "%%Y"=="!secondnum!" set "second=%%Z" & set /a "count+=128" | |
if /i "%%X"=="@name" set "namenum=%%Y" | |
if /i "%%X"=="@host" set "hostnum=%%Y" | |
if /i "%%X"=="@year" set "yearnum=%%Y" | |
if /i "%%X"=="@month" set "monthnum=%%Y" | |
if /i "%%X"=="@day" set "daynum=%%Y" | |
if /i "%%X"=="@hour" set "hournum=%%Y" | |
if /i "%%X"=="@minute" set "minutenum=%%Y" | |
if /i "%%X"=="@second" set "secondnum=%%Y" | |
rem When a full set is reached print it out. | |
if "!count!" equ "255" ( | |
echo %%~nF !name! !host! !year! !month! !day! !hour! !minute! !second!>>%OutputFile% | |
call :Reset | |
) | |
if "!count!" gtr "255" ( | |
echo %%~nF Error Incomplete Data Set>>%OutputFile% | |
) | |
) | |
) | |
) | |
goto :eof | |
:: Reset all of the variables for the next set. | |
:Reset | |
set "name=" | |
set "namenum=" | |
set "host=" | |
set "hostnum=" | |
set "year=" | |
set "yearnum=" | |
set "day=" | |
set "daynum=" | |
set "month=" | |
set "monthnum=" | |
set "hour=" | |
set "hournum=" | |
set "minute=" | |
set "minutenum=" | |
set "second=" | |
set "secondnum=" | |
set "count=0" | |
goto :eof | |
:End | |
endlocal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment