I am pretty new to windows batch scripting and am having trouble to search and extract a portion of a string from a text file and display it out. Some samples data are shown below.
The search is based on student ID, for example: STUD777012
Appreciate if you could help out.
Thank you very much.
STUD777012, return code: 0, Analysis detected no errors
STUD777293, return code: 4, Analysis detected warnings
STUD777086, return code: 8, Analysis detected errors
STUD777099, return code: 0, Analysis detected no errors
Compiling STUD777012 to Data Structure
This is prg version 380.10.20
This is StudPrg.exe version 6.24
debug enabled version
StudPrg.exe finished
prg finished with return code: 0
status:
Analysis detected no errors
Compiling STUD777293 to Data Structure
This is prg version 380.10.20
This is StudPrg.exe version 6.24
debug enabled version
StudPrg.exe finished
This is StudPrg.exe version 6.24
debug enabled version
StudPrg.exe finished
prg finished with return code: 4
status:
Analysis detected warnings
Compiling STUD777086 to Data Structure
This is prg version 380.10.20
This is StudPrg.exe version 6.24
debug enabled version
StudPrg.exe finished
This is StudPrg.exe version 6.24
debug enabled version
StudPrg.exe finished
prg finished with return code: 8
status:
Analysis detected errors
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /f "tokens=1,2,6" %%a IN (q25290636.txt) DO (
IF "%%a"=="Compiling" SET "stud=%%b"
IF "%%a"=="prg" (
IF "%%c"=="0" ECHO !stud! return code %%c, Analysis detected no errors
IF "%%c"=="4" ECHO !stud! return code %%c, Analysis detected warnings
IF "%%c"=="8" ECHO !stud! return code %%c, Analysis detected errors
)
)
GOTO :EOF
assuming that it's safe to convert the error-code number to the text (even if it seems superfluous)
I used a file named q25290636.txt
containing your data for my testing.
You give no data for STUD777099, return code: 0, Analysis detected no errors
Revision to isolate one-particular-record.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "target=STUD%~1"
FOR /f "tokens=1,2,6" %%a IN (q25290636.txt) DO (
IF "%%a"=="Compiling" SET "stud=%%b"
IF "%%a"=="prg" (
ECHO !stud!|FINDSTR /L /B "%target%" >NUL
IF NOT ERRORLEVEL 1 (
IF "%%c"=="0" ECHO !stud! return code %%c, Analysis detected no errors
IF "%%c"=="4" ECHO !stud! return code %%c, Analysis detected warnings
IF "%%c"=="8" ECHO !stud! return code %%c, Analysis detected errors
)
)
)
GOTO :EOF
To run for all targets, run with no parameter.
To run for STUD777293
simply run thisbatch 777293
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.