I'm trying to read the comment section of many .rar files with winrar cli and pipe it to a text file.
WinRAR -z
command open the comment section however it can't be piped.
Thank for helpers.
The program files folder of WinRAR contains the console version Rar.exe
and the text file Rar.txt
which is the manual for the console version. So on usage of Rar.exe
it is advisable to double click on text file Rar.txt
to open it in the associated application like Windows Notepad and read the manual from top to bottom.
Rar.txt
contains the explanation for the RAR command cw
:
cw Write archive comment to specified file. Format of output file depends on -sc switch. If output file name is not specified, comment data will be sent to stdout. Examples: 1) rar cw arc comment.txt 2) rar cw -scuc arc unicode.txt 3) rar cw arc
Further there is explained near the bottom the switch -y
which is also useful for this task in case of the file to write the comment to exists already which would cause a prompt by Rar.exe
on not using switch -y
.
And last the switch -idq
for using quiet mode is also useful here to prevent output to console while writing all the comments from the RAR archive files into text files.
So the task can be done by running in a command prompt window:
for %I in ("C:\Path to RAR files\*.rar") do @"%ProgramFiles%\WinRAR\rar.exe" cw -idq "%I" "C:\Output Path\%~nI.txt"
But how to get all comments directly into one text file?
That is a more interesting question because a batch file is really needed to achieve this.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FirstFile=Yes"
set "CommentFile=C:\Output Path\AllComments.txt"
del "%CommentFile%" 2>nul
for %%I in ("C:\Path to RAR files\*.rar") do (
if not defined FirstFile (
>>"%CommentFile%" echo ================================================================================
>>"%CommentFile%" echo/
) else set "FirstFile="
>>"%CommentFile%" echo %%I
>>"%CommentFile%" echo/
"%ProgramFiles%\WinRAR\Rar.exe" cw -idc -y "%%I" >>"%CommentFile%"
)
endlocal
The switch -idc
is used to prevent just output of the copyright notice, but get output by English Rar.exe
the line Comment is not present
with an empty line above and no empty line below in case of a processed *.rar file does not contain a comment. It would be of course also possible to use -idq
to get nothing written into the all comments text file for a *.rar file not containing a comment.
Note: The posted solution for writing all comments into one text file must be adapted or is not possible at all depending on the environment in which the batch file itself is executed for producing the all comments text file on which contains all characters are correct encoded for non-ASCII comments on which the switch -sc
is additionally used or on file names/paths contain non-ASCII characters.
There could be also used the following code to write the comments faster into a single comments file which is either created new on not already existing before starting the FOR loop or with appending the text output at the end of the comment file on already existing because of using the redirection operator >>
left to the comments file name:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FirstFile=Yes"
set "CommentFile=C:\Output Path\AllComments.txt"
(for %%I in ("C:\Path to RAR files\*.rar") do (
if not defined FirstFile (
echo ================================================================================
echo/
) else set "FirstFile="
echo %%I
echo/
"%ProgramFiles%\WinRAR\Rar.exe" cw -idc -y "%%I"
))>>"%CommentFile%"
endlocal
The information Comment is not present
on RAR.exe
processing a RAR archive file with no comment is written also into the comment file.
The usage of >
instead of >>
left to comment file name would result in always creating new the comments file on already existing before running the FOR loop.