Wednesday, February 1, 2012

Batch file to recursively pack .fb2 files to files

This batch file uses freeware 7-Zip file archiver for zipping files. You can download it from here. You can also replace 7-Zip with your favorite archiver.

Optionally batch file can delete .fb2 files after they are successfully packed, see comment in the code.

@echo off
setlocal enableextensions enabledelayedexpansion

set zipper="%ProgramFiles%\7-Zip\7z.exe"

set total=0
set packed=0
set skipped=0
set failed=0

for /r %%f in (*.fb2) do (
    set filename=%%~df%%~pf%%~nf
    set fb2=!filename!.fb2
    set fb2zip=!fb2!.zip
    set /a total+=1
    if exist !fb2zip! (
        echo !fb2!: already packed
        set /a skipped+=1
    ) else (
        set success=0
        %zipper% a -tzip !fb2zip! !fb2! > nul
        if 0==%ERRORLEVEL% (
            %zipper% t !fb2zip! !fb2! > nul
            if 0==%ERRORLEVEL% (
                rem Uncomment the following line to delete packed .fb2 files
                rem del !fb2!
                set success=1

        if 1==!success! (
            echo !fb2!: OK
            set /a packed+=1
        ) else (
            echo !fb2!: error %ERRORLEVEL%
            set /a failed+=1

echo -------------------
echo %total% file(s) processed
echo %packed% file(s) packed successfully
echo %skipped% file(s) skipped
echo %failed% file(s) failed

No comments:

Post a Comment