I'm trying to figure out how to add a file in each .rar file in a folder.
For example I have:
And I want the 'readme.txt' file added to each of them.
Or if that isn't possible I can just extract all the .rar files turning them into folder then use this batch code to compress them?
How do I include the readme.txt file?
@ECHO OFF
cd C:\Users\userss\Desktop\compressing
SET PATH=C:;C:\Program Files\WinRAR;C:\Windows\system32;C:\Windows;C:\Win dows\System32\Wbem;%PATH%
FOR /f "delims=" %%d IN ('DIR /B') DO WinRAR a -m0 -ep "C:\Users\userss\Desktop\destination\%%~nxd.rar" "%%~fd"
EXIT
I suggest following:
@echo off
for /F "delims=" %%I in ('dir /B "%USERPROFILE%\Desktop\compressing\*.rar"') do "%ProgramFiles%\WinRAR\Rar.exe" a -ep -idq -y -m0 "%USERPROFILE%\Desktop\compressing\%%I" "%USERPROFILE%\Desktop\compressing\readme.txt"
Some might think this could be done also without command DIR using:
@echo off
for %%I in ("%USERPROFILE%\Desktop\compressing\*.rar") do "%ProgramFiles%\WinRAR\Rar.exe" a -ep -idq -y -m0 "%%~I" "%USERPROFILE%\Desktop\compressing\readme.txt"
But this does not work on FAT32 drives because command FOR processes each RAR archive after modification once more resulting in an endless loop.
The NTFS file system driver returns the list of files always sorted alphabetically while FAT32 file system driver returns the files list as stored in file allocation table (FAT) whereby sort order in FAT changes with every file modification in a directory.
The following loop would avoid an endless loop on FAT32 drives:
@echo off
for %%I in ("%USERPROFILE%\Desktop\compressing\*.rar") do "%ProgramFiles%\WinRAR\Rar.exe" u -ep -idq -y -m0 "%%~I" "%USERPROFILE%\Desktop\compressing\readme.txt"
The difference is using RAR command u
instead of a
. This results in adding the single file once to each *.rar file. When FOR calls Rar.exe
a second time for an already updated archive, RAR detects that the file is already in the archive and does not modify the RAR archive once more. So after each *.rar file is processed twice by Rar.exe
(first adding the file, second not changing anything), the FOR loop ends.
However, using command DIR avoids processing each rar file more than once on NTFS and FAT32 drives.
It is necessary to use GUI WinRAR.exe
instead of console Rar.exe
for adding readme.txt
to ZIP archives because the console version supports only RAR archives as explained at top of text file Rar.txt
in program files folder of WinRAR being the manual for the console version.
@echo off
for /F "delims=" %%I in ('dir /B "%USERPROFILE%\Desktop\compressing\*.zip"') do "%ProgramFiles%\WinRAR\WinRar.exe" a -ep -ibck -y -m0 "%USERPROFILE%\Desktop\compressing\%%I" "%USERPROFILE%\Desktop\compressing\readme.txt"
The switch -idq
is replaced by switch -ibck
for running WinRAR in background which means minimized to system tray. For details on commands and switches of GUI version see in help of WinRAR on tab Contents the chapter Command line mode. The list of supported switches of GUI version is slightly different to list of supported switches of console version.