Sunday 16 January 2011

How to create par2 files recursively in a batch

I like to protect my movie files on hard drive by creating par2 files. Par2 files protect my files in case they get damaged and can be used as a way to ensure their integrity.

Because par2cmdline didn't work on my system, I decided to use Multipar command line tool par2j download here

I often have many movie files in many directories,creating the par files is time consuming and hard work. This could be done in a batch and just leave it running.

c:\video
...........c:\video\movie_directory_1
.....................movie_file_1.mkv
.....................info_file_1.nfo
...........c:\video\movie_directory_2
.....................movie_file_2.mkv
.....................info_file_2.nfo
... and so on

I also wanted to make the batch low priority, so it would be possible to do other things on the PC at the same time.

1. So first you need to install Multipar. Basically just download the latest version and extract to a directory called c:\Multipar

2. Create another directory called c:\Scripts

3. Create a file called c:\Scripts\par2batch.cmd code :


@ECHO OFF

REM this command script requires Windows 2000/XP

SETLOCAL

REM check input path

IF NOT EXIST "%1" (
ECHO The path does not exist.
GOTO :EOF
)

REM set options for PAR2 client

SET par2_path=c:\Multipar\par2j.exe
rem SET par2_blocks=800
SET par2_redundancy=10

REM recursive search of sub folders

PUSHD %1
FOR /D /R %%G IN (*.*) DO CALL :ProcessFolder "%%G" "%%~NG"
POPD

PAUSE
GOTO :EOF

REM create PAR2 files in each sub folders

:ProcessFolder
PUSHD %1
%par2_path% c -rr%par2_redundancy% -fo -rd2 -lr100 "%~2.par2" *
POPD

GOTO :EOF

ENDLOCAL


4. Next create a file called c:\Scripts\par2batchlow.cmd code:

@echo off
start /low /b parbatch.cmd %1


5. Add the paths c:\Multipar and c:\Scripts to your windows path, in Vista, Start Button, right click Computer, select Properties, select Advanced System Settings, then on the System Properties Window, press the Environment Variables Button, select the "path" environment variable, click Edit












































(NB System Properties is also accessible via control panel as "System")

6. Now you can open a Command Prompt window (in Start Menu under accessories).

par2batchlow c:\directoryname

This will create par2 files in each of the subdirectories for all files that are in each sub directory.

The code presented here is modified from code that I found at the QuickPar forum , by the author of MultiPar Yutaka Sawada

I generally have used QuickPar for a number of years (download here), which is an amazing tool. However the par2 command line version I could get to work with large files, and many of the files that I'm working with are 4GB, 8GB and even 16GB for video files, so I decided to use par2j which is newer and currently being worked on.



Hope this is useful for someone, good luck


9 comments:

  1. Hi, thanks for your tute, it helped me :)

    Just a quick heads up regarding a small error in step 4: the line should read par2batch.cmd (you missed the 2)

    Thanks!

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi, thank you for this script.

    Following your example, if in c:\video there are files, no parity archive will be created.

    If you want to take into account also c:\video directory you need to modify the script as following:

    REM recursive search of sub folders

    PUSHD "%1"
    FOR /D /R %%G IN (*.*) DO CALL :ProcessFolder "%%G" "%%~NG"
    POPD

    REM PAUSE

    REM Last roundtrip for input directory itself
    CALL :ProcessFolder "%%1" "%%~N1"

    REM PAUSE
    GOTO :EOF

    REM create PAR2 files in each sub folders

    ReplyDelete
  4. Hi,
    I improved your script so it can be launched periodically and it understands if there are new changes in a directory (so PAR2 files need to be regenerated) or not (and it does nothing).

    @ECHO OFF

    REM this command script requires Windows 2000/XP

    SETLOCAL

    REM check input path

    IF NOT EXIST "%1" (
    ECHO The path does not exist.
    GOTO :EOF
    )

    REM set options for PAR2 client

    SET par2_path=d:\Apps\MultiPar\par2j.exe
    rem SET par2_blocks=800
    SET par2_redundancy=10

    REM recursive search of sub folders

    PUSHD "%1"
    FOR /D /R %%G IN (*.*) DO CALL :ProcessFolder "%%G" "%%~NG"
    POPD

    REM PAUSE

    REM Last roundtrip for input directory itself
    CALL :ProcessFolder "%%1" "%%~N1"

    REM PAUSE
    GOTO :EOF

    REM create PAR2 files in each sub folders

    :ProcessFolder
    PUSHD "%1"
    ECHO(
    ECHO Processing %1

    REM chdir
    SET FileFound=
    FOR /F "delims=" %%E IN ('DIR . /B /A:-D') DO (
    SET FileFound=%%E
    GOTO :ExitFileFound
    )

    :ExitFileFound
    REM ECHO FileFound %FileFound%
    IF "%FileFound%"=="" (
    ECHO Directory %1 doesn't contain files
    GOTO :ExitFor
    )

    REM chdir
    FOR /F "delims=" %%H IN ('DIR . /L /B /O:-D') DO (
    REM ECHO "%%H" "%%~XH"
    IF "%%~XH"==".par2" (
    ECHO No new changes in %1.
    ) ELSE (

    ECHO Deleting old PAR2 files in %1
    REM chdir
    DEL /F /Q *.par2
    REM ECHO "%par2_path%" c -rr%par2_redundancy% -fo -rd2 -lr100 "%~2.par2" *
    "%par2_path%" c -rr%par2_redundancy% -fo -rd2 -lr100 "%~2.par2" *
    )
    GOTO :ExitFor
    )
    :ExitFor
    POPD

    GOTO :EOF

    ENDLOCAL

    -

    ReplyDelete
  5. Hi,

    In case a directory name contains a dot (.), there's no reason to create a PAR2 file with a partial name; so it should be modified as:

    FOR /D /R %%G IN (*.*) DO CALL :ProcessFolder "%%G" "%%~NXG"

    ReplyDelete
  6. I'm usin Danilos version of this script but how do i change it so that it uses filename for par2 files instead of foldername?

    ReplyDelete
  7. Hi,
    Let me understand.
    Do you want to create a PAR file for each file in a directory?

    ReplyDelete
  8. Hi!

    No.. I want them par2 files to be named after the filename not the folder:

    example:

    i have a folder called : vacation pictures from Denmark

    in that folder are files called: vacation2014.rar

    when i use your script the par2 files are named: vacation in denmark.par2

    i want them to be named vacation2014.par2

    so instead of using foldername to name the par2 files it should use the filename

    /Kenneth

    ReplyDelete
  9. I've been looking for a way to automate my PARing of a few thousand folders... What I'm TRYING to do is define an algorithm to select different %s based on # of files... a 24-file TV series folder can have 10% PAR2 files, so if 1 or 2 episodes are deleted, they can be recovered. But a 2-file folder, needs just over 1 file (50%+) (or, really, just over whatever 50% of the total folder size is). Currently, I would have to do make a manual change for every single folder (thousands of them). I'm surprised there isn't (so far as I've searched) any commercial software to do this; it's really just an extension of what's been written above... Anyone know of any software that does this?

    ReplyDelete