Search code examples
windowsbatch-filearchive

Can somebody explain this batch code for extracting specific file from archive


The below code extracts files from within the archive in another location, can somebody explain what exactly is going on in the program.

@echo off

if "%1" == "" GOTO error 
if "%2" == "" GOTO error

FOR /F "tokens=1,2 delims=^" %%G IN (%1) DO call 7za.exe e %%G  %%H  -o%2  -y 

:error 
@echo usage : jobextract.bat (inputFile.txt) (o/p dir) 

Solution

  • Here is the explanation:

    @echo off - turns command echoing off. See echo /?

    if "%1" == "" GOTO error - if the first parameter passed into the script is blank, goto (and skip everything in between) a label named error
    if "%2" == "" GOTO error - if the second parameter passed into the script is blank, goto (and skip everything in between) a label named error

    FOR /F "tokens=1,2 delims=^" %%G - for the next two items (tokens) that are seperated by a ^ (caret), starting with a variable named %%G
    IN (%1) - in the following input (parameter 1)
    DO call - run the following
    7za.exe e %%G %%H -o%2 -y - running 7za.exe1 with e (extract) %%G (first token from the for /f) %%H (second token from the for /f) -o%2 (output to the directory that was put in as a second parameter in the script) -y (yes to overwrite)

    :error - this is the error label
    @echo usage : jobextract.bat (inputFile.txt) (o/p dir) print the following to the screen: usage : jobextract.bat (inputFile.txt) (o/p dir)

    Here's more info on for loops: LINK and/or for /?
    Here's info on goto and labels: LINK and/or goto /?