-2

I want to write a batch script that add readme.txt file to some directories and then compress the whole directory.

Example :

@ECHO OFF
for /D %%f in ("C:\directory_with_files_you_want_to_compress\ *") do copy "C:\directory_with_readme.txt\readme.txt" "%%f\"
cd C:\directory_with_files_you_want_to_compress
SET PATH=C:;C:\Program Files (x86)\WinRAR;C:\Windows\system32;C:\Windows;C:\Win dows\System32\Wbem;%PATH%
FOR /f "delims=" %%d IN ('DIR /B') DO WinRAR a -m0 -ep -df -v100m -x*.rar "C:\where_you_want_to_save_new_rar_files\%%~nxd.ra r" "%%~fd"
EXIT

But it doesn't add readme.txt to the RAR files.

Mofi
  • 46,139
  • 17
  • 80
  • 143

1 Answers1

0

That is very easy to achieve as console version Rar.exe as well as GUI version WinRAR.exe support multiple files or directories to be specified on command line to add into same archive file.

@echo off
for /D %%D in ("C:\directory_with_files_you_want_to_compress\*") do "%ProgramFiles(x86)%\WinRAR\Rar.exe" a -cfg- -ep -idq -m0 -x*.rar -v100m "C:\where_you_want_to_save_new_rar_files\%%~nxD.rar" "%%~fD\" "C:\directory_with_readme.txt\readme.txt" && rd /Q /S "%%~fD" || echo/ && pause

Same as above easier to read:

@echo off
for /D %%D in ("C:\directory_with_files_you_want_to_compress\*") do (
    "%ProgramFiles(x86)%\WinRAR\Rar.exe" a -cfg- -ep -idq -m0 -x*.rar -v100m -y "C:\where_you_want_to_save_new_rar_files\%%~nxD.rar" "%%~fD\" "C:\directory_with_readme.txt\Readme.txt"
    if not errorlevel 1 (
        rd /Q /S "%%~fD"
    ) else (
        echo/
        pause
    )
)

The batch code uses console version Rar.exe instead of GUI version WinRAR.exe as there is absolutely no need for using GUI version for this archive files creation task.

The switch -df to delete all compressed files by Rar is removed because of readme.txt should not be automatically deleted by Rar after processing the first subdirectory. This file must be added also to the other archive files to create for the other subdirectories. Instead the command RD is used to delete the already successfully processed subdirectory. On an error like the RAR archive to create exists already and is write-protected an empty line is output with echo/ and processing is halted with pause.

The switch -idq is added to run Rar in quiet mode which results in getting output only errors.

The switches -m0 and -v100m results in just storing instead of really compressing all files in each subdirectory into a multi-volume RAR archive with 100 MB per volume. So this batch file is obviously used to pack already compressed archive, audio, image or video files into multi-volume archives.

The switch -y is added to automatically possible prompts with yes.

All switches are documented in text file Rar.txt in WinRAR program files folder.

I added after %%~fD a backslash in case of somebody else wants to use this code with switch -ep1 instead of -ep and with adding the switch -r to recursively archive each subdirectory without having subdirectory name added to the archive too. The used switches archive only the files in each subdirectory, but not their subdirectories although the processed subdirectory is completely removed after creation of multi-volume RAR archive.

See also the answers on the questions:

Mofi
  • 46,139
  • 17
  • 80
  • 143