Search code examples
windowsbatch-filecmdwinrar

How to show list of missing files and other errors on creating multiple ZIP archives with WinRAR?


I made this batch file to create ZIP files based on a text file:

@echo off

set path="C:\Program Files\WinRAR\";%path%

for /f "tokens=1* delims=;" %%a in (list.txt) do (
    WinRAR a -afzip "%%a" %%b
    pause
    cls
)

The file list.txt looks like this:

file1.zip;fileA.pdf fileB.pdf fileC.pdf
file2.zip;fileA.pdf fileB.pdf fileC.pdf fileD.pdf
file3.zip;fileB.pdf fileD.pdf
file4.zip;fileA.pdf fileC.pdf fileE.pdf
file5.zip;file*.pdf

As you can see:

  • %%a is the name of the ZIP file to create, e.g: file1.zip and
  • %%b is the list of files to add to the ZIP file, e.g: fileA.pdf fileB.pdf fileC.pdf for file1.zip.

It works very well, but I was wondering if it's possible to make show me which files it could not find (and therefore didn't add to the ZIP file) for each ZIP file created.

Any ideas?

EDIT: As stated by @Mofi, Rar cannot be used to create ZIP files. So I changed

    rar a -r -m5 "%%a" %%b

to

    WinRAR a -afzip "%%a" %%b

Solution

  • Console version Rar does not support ZIP format as it can be read in text file %ProgramFiles%\WinRAR\Rar.txt which is the manual for console version. Only GUI version WinRAR supports creating and extracting ZIP archives.

    There is no need to separately test existence of all files to archive on using WinRAR as this commented batch code demonstrates:

    @echo off
    rem Define name of error log file in directory for temporary files with name
    rem of batch file, an underscore, a random number and file extension TMP.
    set "ErrorLog=%TEMP%\%~n0_%RANDOM%.tmp"
    
    rem Delete error log file if already existing from a previous run of this
    rem batch file broke by the user while running and having randomly the
    rem same number.
    if exist "%ErrorLog%" del "%ErrorLog%"
    
    rem For each ZIP file run WinRAR with ignoring default configuration (-cfg-)
    rem using best compression (-m5), running in background (-ibck = minimized
    rem to system tray), writing error messages into a log file (-ilog) and
    rem suppressing displaying the errors in a GUI window (-inul).
    
    rem If WinRAR really output any error into the log file, clear screen, output
    rem on which ZIP file the error(s) occurred, output the logged error messages,
    rem delete the error log file, and hold batch processing until user presses
    rem any key to continue.
    
    for /f "tokens=1* delims=;" %%a in (list.txt) do (
        "%ProgramFiles%\WinRAR\WinRAR.exe" a -afzip -cfg- -m5 -ibck "-ilog%ErrorLog%" -inul -- "%%~a" %%b
        if exist "%ErrorLog%" (
            cls
            echo Errors output by WinRAR on creating %%a:
            echo.
            type "%ErrorLog%"
            del "%ErrorLog%"
            echo.
            echo.
            pause
        )
    )
    
    rem Delete the environment variable for error log file name with path.
    set "ErrorLog="
    

    For even more details on used WinRAR switches

    • start WinRAR,
    • click in menu Help on Help topics,
    • open on tab Contents the item Command line mode,
    • open item Switches and
    • click on a switch (option / parameter) used in above batch code.

    It is important not using -r switch (recursive) as this means for WinRAR that a parameter after archive name can be a file or a directory.

    WinRAR v5.21 does not report directories not existing on compression, just files which could not be opened for read because of not existing at all, or no read permission (NTFS partition) for current user, or read/write lock set by other running process on file, etc. The reason for the file opening error returned by operating system is also output by WinRAR additionally to error message about failed opening of the file.

    WinRAR expects without -r that each parameter after archive file name is the name of a file (with or without path) and therefore outputs an error message if the file could not be opened for reading the data.

    But the parameter can be also name of a directory not existing on using switch -r which is not reported as an error by WinRAR.

    If one or more files in list file contain 1 or more spaces or one of these characters

    &()[]{}^=;!'+,`~
    

    in file name it is necessary to quote this file name in list file.

    Example:

    "file 1.zip";"file A.pdf" fileB.pdf "file C.pdf"
    file 2.zip;"file A.pdf" fileB.pdf "file C.pdf" fileD.pdf
    file3.zip;fileB.pdf fileD.pdf
    file4.zip;"fileA.pdf" fileC.pdf fileE.pdf
    file5.zip;file*.pdf
    file6.zip;file*.pdf readme.txt
    

    The archive file name can be also without quotes because of using "%%~a" on command line calling WinRAR.

    The last 2 lines demonstrate that also file name patterns with 1 or more wildcards can be used. But error message No files to add is output by WinRAR only when ZIP file could not be created because of no file found to add at all. So line 5 produces an error message if there is no file*.pdf, but line 6 produces no error message if at least readme.txt exists and could be added to archive file.

    WinRAR sets also errorlevel on exit according to page List of WinRAR exit codes in help of WinRAR. But evaluating the exit code in the batch file does not make much sense if the error messages are output in any case. The error log file is created by WinRAR only when any error occurred.

    Last but not least it would be also possible to use switch -log to get the names of files added to an archive written into a log file and output those list with type and more in the console window. See help of WinRAR for details on switch -log.

    For understanding the used console commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

    • cls /?
    • del /?
    • echo /?
    • for /?
    • if /?
    • pause /?
    • rem /?
    • set /?
    • type /?