Sample of batch script

Because MultiPar consists of PAR clients and GUI, it is possible to use a PAR client on Command Prompt. Read a manual of command line for the details of command and option. It's available by batch file (or command script).

Below are some samples of batch file for Windows 7. Copy & paste the whole lines into a text file. Modify the options for PAR2 files, and set the path of `par2j.exe`. Write absolute path like;
`SET par2_path="C:\something directory\MultiPar\par2j.exe"`
Read manuals (`Command_***.txt`) to know the detail of options. Read Windows OS 's help for Command Prompt's usage.

Save a sample script to a file like `batch.bat` or `batch.cmd`. There are some ways to specify a file or folder.

1. Type everytime by keyboard on Command Prompt

   If you use the batch file at Command Prompt, change directory to the batch file at first. Next, type `batch.bat "path of the file or folder"`. If there is no space in the path, no need to cover by `""`. The path may be absolute path or relative path from the batch file. Normally absolute path would be safe.

   If you want to specify multiple files or folders, type `batch.bat "path of the first file or folder" "path of second file or folder" "path of third file or folder"`. You may add some pathes after a script filename.

2. Drag & Drop a file or folder by mouse

   On Windows Explorer, drag a file or folder and drop it on your batch file. If you put the batch file on Desktop, it will be easy to Drag & Drop.

   If you want to specify multiple files or folders, select them at once on Windows Explorer, and Drag & Drop the group on the batch file. While dragging, number of items may be shown.

3. Select SendTo at Windows Explorer's right click menu

   At first, create Short-cut icon of your batch file. Name it to be something easy to understand like "Create PAR2". Then, put the Short-cut icon in your `SendTo` folder. You can open the `SendTo` folder by typing `shell:sendto` on path-box of Windows Explorer.

   After you select a file or folder on Windows Explorer, click mouse's right button. Select "SendTo" item on the right-click menu. As your created Short-cut item will exist in the SendTo sub-menu, select the Short-cut icon.

   If you want to specify multiple files or folders, select them at once on Windows Explorer. While mouse cursor over there, click mouse's right button. Later action is same as single item.

If you want to confirm the result of scripting, add "`PAUSE`" at the end of batch file. If you want to see how each command line is parsed, remove "`@ECHO OFF`" at the top of batch file, or add "`ECHO`" in front of the questionable line. These may help you understanding how batch script works.

If you want to switch flow by the result, you may use a special environment variable "`ERRORLEVEL`". "`if ERRORLEVEL number task~`" means that the task will start when ERRORLEVEL is greater than or equal to the number. "`if not ERRORLEVEL number task~`" means that the task will start when ERRORLEVEL is less than the number. It's possible to refer the value by using "`%ERRORLEVEL%`". The code is such like "`set err = %ERRORLEVEL%`". When you want to refer ERRORLEVEL in "for ~ do ~" loop, you must set a local variable as "`setlocal enabledelayedexpansion`" and use "`!ERRORLEVEL!`".

### Index

Create individual PAR2 set in each selected folder (with GUI) |
Create individual PAR2 set in each subfolder under a selected folder (with GUI) |
Create one PAR2 file per each file in a selected folder |
Create individual PAR2 set per each RAR archive in a selected folder |
Create individual PAR2 set per each set of 4 files from many selected files |
Create PAR2 recovery data and append it to each original ZIP / 7-Zip archive file |
Verify and/or Repair individual PAR2 set in each selected folder with GUI |
Create, Verify or Repair individual PAR2 set in each subfolder under a selected folder with GUI |
Add MD5 checksum to each file independently in a selected folder (NTFS only) |
Verify each file independently by MD5 checksum in a selected folder (NTFS only) |
Play sound for notification |
Batch processing after MultiPar GUI 's Creation |
Batch processing after MultiPar GUI 's Verification / Repair |

### Create individual PAR2 set in each selected folder (with GUI)

Command | "path of batch file" "path of folder1" "path of folder2" "path of folder3"...
Input | specify some folders
Action | This sample creates individual PAR2 set in each selected folder. For example, when you supply 7 folders, total 7 PAR2 set are made. It is possible to show GUI for setting, too.

@ECHO OFF SETLOCAL SET par2_path="path of par2j.exe" :GetFilePath REM check input path IF "%~1"=="" GOTO End IF NOT EXIST "%~1" GOTO NextFile REM run PAR2 client ECHO create for %1 %par2_path% c /sm2048 /rr20 /rd1 /rf3 "%~1\%~n1.par2" * IF ERRORLEVEL 1 GOTO End :NextFile SHIFT GOTO GetFilePath :End ENDLOCAL

If you want to use MultiPar GUI for setting, you may call `MultiPar.exe` instead of `par2j.exe`. When a GUI is closed, next GUI is opened with next folder. You may set an option in "Automated tasks" to close GUI automatically after creation.

@ECHO OFF SETLOCAL SET multipar_path="path of MultiPar.exe" :GetFilePath REM check input path IF "%~1"=="" GOTO End IF NOT EXIST "%~1" GOTO NextFile REM open MultiPar GUI ECHO create for %1 %multipar_path% /create %1 :NextFile SHIFT GOTO GetFilePath :End ENDLOCAL

If you don't want to wait the finish of each creation, you may use "`START`" command in the script. Because all MultiPar GUIs will open at once for each folder, you can start each creation anytime.

@ECHO OFF SETLOCAL SET multipar_path="path of MultiPar.exe" :GetFilePath REM check input path IF "%~1"=="" GOTO End IF NOT EXIST "%~1" GOTO NextFile REM open MultiPar GUI ECHO create for %1 START "" %multipar_path% /create %1 :NextFile SHIFT GOTO GetFilePath :End ENDLOCAL

### Create individual PAR2 set in each subfolder under a selected folder (with GUI)

Command | "path of batch file" "path of a folder"
Input | specify a folder
Action | This sample creates individual PAR2 set in each subfolder under the selected folder. For example, when there are 7 subfolders, total 7 PAR2 set are made.

@ECHO OFF SETLOCAL REM check input path IF "%~1"=="" GOTO End IF NOT EXIST "%~1" ( ECHO The path does not exist. GOTO End ) REM set options for PAR2 client SET par2_path="path of par2j.exe" REM recursive search of subfolders PUSHD %1 FOR /D /R %%G IN (*.*) DO CALL :ProcEach "%%G" POPD GOTO End REM run PAR2 client :ProcEach ECHO create for %1 %par2_path% c /fo /sm2048 /rr20 /rd1 /rf3 "%~1\%~n1.par2" * GOTO :EOF :End ENDLOCAL

Don't remove an option `/fo` in the command-line. By this option, PAR2 file contains files only in each subfolder.

If you want to use MultiPar GUI for setting, you may call `MultiPar.exe` instead of `par2j.exe`. When a GUI is closed, next GUI is opened with next folder. You may set an option in "Automated tasks" to close GUI automatically after creation. You may check "Always use folder name for base filename" option in "Client behavior", then each PAR2 set is named by the folder.

@ECHO OFF SETLOCAL REM check input path IF "%~1"=="" GOTO End IF NOT EXIST "%~1" ( ECHO The path does not exist. GOTO End ) SET multipar_path="path of MultiPar.exe" REM recursive search of subfolders PUSHD %1 ECHO "%1" FOR /D /R %%G IN (*.*) DO CALL :ProcEach "%%G" POPD GOTO End REM open MultiPar GUI :ProcEach ECHO create for %1 %multipar_path% /create %1 GOTO :EOF :End ENDLOCAL

### Create one PAR2 file per each file in a selected folder

Command | "path of batch file" "path of a folder"
Input | specify a folder
Action | This sample creates one PAR2 file per each file in the selected folder. If there is a subfolder in a folder, files in the subfolder are searched, too. Instead of creating PAR2 files for all files in a subfolder like previous sample, this creates one PAR2 file per each file in a subfolder. For example, when there are 7 files in the folder, total 7 PAR2 files are made.

@ECHO OFF SETLOCAL REM check input path IF "%~1"=="" GOTO End IF NOT EXIST "%~1" ( ECHO The path does not exist. GOTO End ) REM set options for PAR2 client SET par2_path="path of par2j.exe" REM recursive search of files PUSHD %1 FOR /R %%G IN (*.*) DO CALL :ProcEach "%%G" POPD GOTO End REM run PAR2 client :ProcEach ECHO create for %1 %par2_path% c /fo /sm2048 /rr10 /ri /in /lr32767 "%~1.par2" %1 GOTO :EOF :End ENDLOCAL

Don't remove an option `/in` in the command-line. By this option, index file isn't created.

### Create individual PAR2 set per each RAR archive in a selected folder

Command | "path of batch file" "path of a folder"
Input | specify a folder
Action | This sample creates individual PAR2 set per each RAR archive file(s) in the selected folder. If RAR archive is single file, PAR2 files are created for it. If RAR archive is splitted into multiple files, PAR2 files are created for them. When you don't select any folder, it will search RAR files in current directory (where the script exists normally).

@ECHO OFF SETLOCAL enabledelayedexpansion REM check input path IF "%~1"=="" GOTO Start IF NOT EXIST "%~1" ( ECHO The path does not exist. GOTO End ) PUSHD %1 :Start REM set paramaters for PAR2 client SET par2_path="path of par2j.exe" SET REDUNDANCY_PERCENT=15 SET PAR2_FILES=3 REM search file of each single RAR archive FOR %%F in (*.rar) do ( REM check ".part" in splitted RAR files ECHO "%%~nF" | find ".part" >NUL if ERRORLEVEL==1 ( REM when filename doesn't include ".part", create PAR2 file for the RAR file. ECHO Creating parity archives for %%F... %par2_path% c /rr%REDUNDANCY_PERCENT% /rf%PAR2_FILES% /rd1 "%%~nF.par2" "%%F" ECHO Parity archives created for %%F. ECHO. ) ) REM search the first file of each splitted RAR archive FOR %%F in ("*.part1.rar" "*.part01.rar" "*.part001.rar") do ( call :SUB_CREATE "%%~nF" ) POPD ECHO Parity archive creation complete. PAUSE GOTO End REM sub-routine to create PAR files for multiple input files :SUB_CREATE ECHO Creating parity archives for multiple %~n1... %par2_path% c /rr%REDUNDANCY_PERCENT% /rf%PAR2_FILES% /rd1 "%~n1.par2" "%~n1.part*.rar" ECHO Parity archives created for multiple %~n1. ECHO. GOTO :EOF :End ENDLOCAL

The script calls `Find.exe` and for certain Windows versions, you may have to add `C:\Windows\System32` to Windows System Path if not already present.

### Create individual PAR2 set per each set of 4 files from many selected files

Command | "path of batch file" "path of file1" "path of file2" "path of file3" "path of file4" "path of file5"...
Input | specify many files
Action | This sample creates individual PAR2 set per each set of 4 files from many selected files. For example, when you supply 11 files, total 3 PAR2 set are made. (11 = 4 + 4 + 3)

@ECHO OFF SETLOCAL REM set options for PAR2 client SET par2_path="path of par2j.exe" SET /a N=1 :GetFilePath REM check input path IF "%~1"=="" GOTO End IF NOT EXIST "%~1" GOTO End REM run PAR2 client ECHO create for "%~d1%~p1par_set%N%" %par2_path% c /sm2048 /rr20 /rd1 /rf3 "%~d1%~p1par_set%N%.par2" %1 %2 %3 %4 SET /a N=N+1 SHIFT SHIFT SHIFT SHIFT GOTO GetFilePath :End ENDLOCAL

You may modify this sample to accept more files up 9. Be careful about the number of source files and "`SHIFT`" per each step.

### Create PAR2 recovery data and append it to each original ZIP / 7-Zip archive file

Command | "path of batch file" "path of archive file1" "path of archive file2" "path of archive file3"...
Input | specify some ZIP / 7-Zip archive files
Action | This sample creates PAR2 recovery data and append it to each original ZIP / 7-Zip archive file. Though ZIP / 7-Zip archive don't have a feature of recovery record, parchive can add the extra feature. For example, when you supply 7 archive files, total 7 archive files with recovery record are made.

@ECHO OFF SETLOCAL REM set options for PAR2 client SET par2_path="path of par2j.exe" :GetFilePath REM check input path IF "%~1"=="" GOTO End IF NOT EXIST "%~1" GOTO End REM check format IF /i "%~x1" NEQ ".zip" ( IF /i "%~x1" NEQ ".7z" ( GOTO NextFile ) ) REM ignore already appended file %par2_path% l %1 IF NOT ERRORLEVEL 1 GOTO NextFile REM run PAR2 client ECHO append to %1 %par2_path% c /fo /sm2048 /rr10 /ri /in /lr32767 /lp4 "%~1.par2" %1 REM join 2 files COPY /b "%~1"+"%~1.vol_1.par2" /v "%~1" DEL "%~1.vol_1.par2" :NextFile SHIFT GOTO GetFilePath :End ENDLOCAL

Don't remove options `/ri` and `/in` in the command-line. By this option, index file isn't created and filename has fixed volume number. When joinning original archive and recovery data, it is important to put original archive at first, then append recovery data at the last. From the nature of ZIP / 7-Zip archive, appended PAR2 recovery data has no effect to extract or decompress. If you want to keep PAR2 file separately, comment out the line of "`DEL`" command.

To verify the archive, just Drag&Drop it onto MultiPar short-cut icon. (or right-click SendTo or ShellExtension works, too.) On the MultiPar GUI, the archive with PAR2 recovery data is shown as "Appended" status, when the original part of archive is complete. If you repair the archive, the archive returns to the original form. (appended PAR2 recovery data is removed.) When you want to keep the PAR2 recovery data, you need to un-check an option "Delete damaged files after Repair". Then the recovery data is renamed by adding "`.1`" after filename.

### Verify and/or Repair individual PAR2 set in each selected folder with GUI

Command | "path of batch file" "path of folder1" "path of folder2" "path of folder3"...
Input | specify some folders
Action | This sample calls MultiPar GUI for individual PAR2 set in each selected folder. For example, when you supply 7 folders, GUI are opened 7 times one by one. It is possible to repair by the GUI. When a GUI is closed, next GUI is opened with next PAR2 set.

@ECHO OFF SETLOCAL SET multipar_path="path of MultiPar.exe" :GetFilePath REM check input path IF "%~1"=="" GOTO End IF NOT EXIST "%~1" GOTO NextFile REM open MultiPar GUI ECHO verify for %1 %multipar_path% /verify "%~1\%~n1.par2" :NextFile SHIFT GOTO GetFilePath :End ENDLOCAL

The key is setting the PAR filename to be based on the folder name. If you set prefix/suffix to filename at creating time, you need to set same one at verifying time.

### Create, Verify or Repair individual PAR2 set in each subfolder under a selected folder with GUI

Command | "path of batch file" "path of a folder"
Input | specify a folder
Action | This sample calls MultiPar GUI for individual PAR2 set in each subfolder under the selected folder. When PAR2 files don't exist, it creates PAR2 set for the subfolder. When PAR2 files exist, it verifys files in the subfolder. For example, when there are 7 subfolders, total 7 PAR2 set are made. Next time you specify the folder, MultiPar verifies the 7 subfolders one by one.

@ECHO OFF SETLOCAL REM check input path IF "%~1"=="" GOTO End IF NOT EXIST "%~1" ( ECHO The path does not exist. GOTO End ) SET multipar_path="path of MultiPar.exe" REM recursive search of subfolders PUSHD %1 ECHO "%1" FOR /D /R %%G IN (*.*) DO CALL :ProcEach "%%G" POPD GOTO End REM open MultiPar GUI :ProcEach IF EXIST "%~1\%~n1.par2" ( ECHO verify for %1 %multipar_path% /verify "%~1\%~n1.par2" ) else ( ECHO create for %1 %multipar_path% /create %1 ) GOTO :EOF :End ENDLOCAL

The key is setting the PAR filename to be based on the folder name. You must check "Always use folder name for base filename" option in "Client behavior", then each PAR2 set is named by the folder.

When a GUI is closed, next GUI is opened with next folder. You may set an option in "Automated tasks" to close GUI automatically after creation. If you want to skip complete files at verification, you may set option to close GUI automatically after verification, too.

### Add MD5 checksum to each file independently in a selected folder (NTFS only)

Command | "path of batch file" "path of a folder"
Input | specify a folder
Action | This sample adds MD5 checksum to each file independently in a selected folder. If there is a subfolder in a folder, files in the subfolder are searched, too. This uses "Alternate Data Stream" and works over NTFS only. Checksum data is saved in an alternate stream "`original filename:digest.md5`", but it's invisible by Windows Explorer.

@ECHO OFF SETLOCAL REM check input path IF "%~1"=="" GOTO End IF NOT EXIST "%~1" ( ECHO The path does not exist. GOTO End ) REM set options for SFV/MD5 client SET client_path="path of sfv_md5.exe" REM recursive search of files PUSHD %1 FOR /R %%G IN (*.*) DO CALL :ProcEach "%%G" POPD ECHO Finished to add checksums into alternate stream. PAUSE GOTO End REM run SFV/MD5 client :ProcEach ECHO create for %1 %client_path% c "%~1:digest.md5" %1 IF ERRORLEVEL 1 ( ECHO Stop at this point. PAUSE EXIT ) GOTO :EOF :End ENDLOCAL

There are some cautions. Some binary/text editor seems to remove alternate data stream automatically at over-writing. If you modify a file, you will need to re-add checksum to the new file again. Adding checksum will update files' modification time, even though original file data isn't changed. A user needs to use a special tool to see/delete/copy the stream, because those streams are untouchable by Windows Explorer. While an alternate stream is kept at copy/move between NTFS drives, it will be removed on other file system. For example, you cannot backup data in alternate stream on CD/DVD.

### Verify each file independently by MD5 checksum in a selected folder (NTFS only)

Command | "path of batch file" "path of a folder"
Input | specify a folder
Action | This sample verify each file independently by MD5 checksum in a selected folder. If there is a subfolder in a folder, files in the subfolder are searched, too. This uses "Alternate Data Stream" and works over NTFS only. Checksum data must have been saved in an alternate stream "`original filename:digest.md5`".

@ECHO OFF SETLOCAL REM check input path IF "%~1"=="" GOTO End IF NOT EXIST "%~1" ( ECHO The path does not exist. GOTO End ) REM set options for SFV/MD5 client SET client_path="path of sfv_md5.exe" REM recursive search of files PUSHD %1 FOR /R %%G IN (*.*) DO CALL :ProcEach "%%G" POPD ECHO Finished to verify files by checksums in alternate stream. PAUSE GOTO End REM run SFV/MD5 client :ProcEach ECHO verify for %1 %client_path% v "%~1:digest.md5" IF ERRORLEVEL 4 ( ECHO This file is broken. PAUSE GOTO :EOF ) IF ERRORLEVEL 1 ( ECHO Stop at this point. PAUSE EXIT ) GOTO :EOF :End ENDLOCAL

### Play sound for notification

This sample plays sound by Media Player. This is good to notify the end of process to user, while running script. This can play any file format which is supported by Media Player.

@ECHO OFF SETLOCAL REM set path of sound file SET SNDPATH=%systemroot%\Media\ringout.wav REM set playing time by mil seconds SET HTA_MAX_TIME=5000 REM call HTA application (JavaScript and Media Player plag-in) mshta.exe "about:playing... <OBJECT CLASSID='CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95' WIDTH=100 HEIGHT=100><param name='src' value='%SNDPATH%'><param name='PlayCount' value='1'><param name='autostart' value='true'></OBJECT><script>window.resizeTo(10,10);setTimeout(function(){window.close()},%HTA_MAX_TIME%);</script>" ENDLOCAL

### Batch processing after MultiPar GUI 's Creation

MultiPar GUI can start script after creation. The script file must be `auto_c.cmd` in the user's `save` folder. A user may write any script in this file. The script gets two parameters from GUI as below;
`%1 = Path of a recovery file
%2 = Path of base directory of source files`

This sample script moves created PAR2 recovery files into another folder.

@ECHO OFF TITLE After creation SETLOCAL REM List up parameters ECHO Path of this batch file = ECHO %0 ECHO. ECHO Path of a recovery file = ECHO %1 ECHO. ECHO Path of base directory of source files = ECHO %2 ECHO. REM check the extension for .par2 IF /i "%~x1" NEQ ".par2" ( ECHO The recovery file format isn't PAR2. GOTO End ) REM insert * between filename and extension SET search_path="%~d1%~p1%~n1*%~x1" REM move all created recovery files into another folder SET destination_folder="C:/Sample/" ECHO Moving %search_path% move %search_path% %destination_folder% ECHO Above files were gone to %destination_folder% :End ENDLOCAL

### Batch processing after MultiPar GUI 's Verification / Repair

MultiPar GUI can start script after verification or repair. The script file must be `auto_v.cmd` in the user's `save` folder. A user may write any script in this file. The script gets three parameters from GUI as below;
`%1 = Path of a recovery file
%2 = Path of base directory of source files
%3 = Exit code of the used client (refer the command-line manual)`

This sample script opens ZIP or 7-Zip archive only when it is complete. To use this script, you need to create some recovery files for an archive at first. At that time, the key is setting the base filename to be same as the source file like;
source file's name = `something.zip`
recovery files' name = `something.zip.par2, something.zip.vol0+4.par2, something.zip.vol4+8.par2`
Thus, when you open the index file "`something.zip.par2`", the filename without last extension becomes the name of source file.

@ECHO OFF TITLE After verification SETLOCAL REM List up parameters ECHO Path of this batch file = ECHO %0 ECHO. ECHO Path of a recovery file = ECHO %1 ECHO. ECHO Path of base directory of source files = ECHO %2 ECHO. ECHO Status of source files = %3 ECHO. REM check the source file is complete IF NOT %3==0 ( IF NOT %3==16 ( ECHO The file is broken. GOTO End ) ) REM remove last extension from path SET new_path="%~d1%~p1%~n1" ECHO Target file = %new_path% IF EXIST %new_path% (CALL :OpenFile %new_path%) ELSE ECHO The file does not exist. GOTO End :OpenFile REM check the format and open the supported file IF /i "%~x1" EQU ".zip" ( ECHO Open the ZIP archive %1 GOTO :EOF ) IF /i "%~x1" EQU ".7z" ( ECHO Open the 7-Zip archive %1 GOTO :EOF ) ECHO The file format isn't supported. GOTO :EOF :End ENDLOCAL

This sample script moves log file (`MultiPar.ini`) to PAR2 file's directory. If there is an old log file already, it appends new log after existing one. To use this script, you need to enable log. MultiPar Options -> [Client behavior] -> Check "Log output of clients".

@ECHO OFF TITLE Move log file SETLOCAL SET old_path="%~d0%~p0MultiPar.log" SET new_path="%~1.log" ECHO Path of original log file = ECHO %old_path% ECHO Path of moved log file = ECHO %new_path% ECHO. ECHO Copying %old_path% to %new_path%. COPY /B %new_path% + %old_path% %new_path% IF %ERRORLEVEL% == 0 ( REM If copy ok, delete original log file. DEL %old_path% ) ELSE ( REM If copy fail, move log file. ECHO Moving %old_path% to %new_path%. MOVE %old_path% %new_path% ) ENDLOCAL