How to archive each folder in a directory separately using WinRAR?
I am trying to use WinRAR to compress all my different folders individually.
Example of folder content before
c:\projects\test
c:\projects\country
c:\projects\db
and aft开发者_Python百科er running the batch file
c:\backup\test.rar
c:\backup\country.rar
c:\backup\db.rar
I am trying the following command in a batch file. But it compresses all the folders in the projects folder being into the backup archive:
for /f "delims==" %%D in ('DIR C:\projects /A /B /S') do (
"C:\Program Files\WinRAR\WinRAR.EXE" m -r "c:\backup\projects.rar" "%%D"
)
c:\backup\projects.rar
contains all the files which I want in separate archives.
How to modify the 3 lines in batch file to get the desired archives?
I think you need to change a couple things.
- Change
/A
to/AD
to get just the directories. - Remove the
/S
so you will only get the top-level directories inC:\Projects
. - Inside your
FOR
loop, change the"c:\backup\projects.rar"
toC:\Backup\%%D.rar"
WARNING: This code is untested.
FOR /F "DELIMS==" %%D in ('DIR C:\projects /AD /B') DO (
"C:\Program Files\WinRAR\WinRAR.EXE" m -r "C:\Backup\%%D.rar" "%%D"
)
Here is a batch file for more general usage of this common task because the folder with the subfolders to archive can be specified as parameter on running the batch file. There can be used also case-insensitive the option /N
on running the batch file to suppress the execution of command PAUSE on an error occurred which could be useful on running the batch file from within a command prompt window or on calling it from another batch file.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "BackupFolder=C:\Backup"
set "FolderToArchive=C:\projects"
rem The first or the sescond argument can be optionally /N
rem to suppress the execution of command pause on an error.
set "PauseCmd=pause"
if /I "%~2" == "/N" set "PauseCmd="
if /I "%~1" == "/N" set "PauseCmd=" & shift /1
if exist "%ProgramFiles%\WinRAR\Rar.exe" goto CheckFolder
echo/
echo Error: RAR executable "%ProgramFiles%\WinRAR\Rar.exe" does not exist.
echo/
%PauseCmd%
exit /B 22
rem Folder to archive can be optionally specified as parameter.
:CheckFolder
if not "%~1" == "" set "FolderToArchive=%~1"
rem Check the existence of the folder to archive.
if exist "%FolderToArchive%\*" goto CheckDestination
echo/
echo Error: Folder "%FolderToArchive%" does not exist.
echo/
%PauseCmd%
exit /B 20
rem Check existence of backup folder and create this folder
rem if not already existing with verification on success.
:CheckDestination
if exist "%BackupFolder%\*" goto CreateArchives
md "%BackupFolder%"
if not errorlevel 1 goto CreateArchives
echo/
echo Error: Folder "%BackupFolder%" could not be created.
echo/
%PauseCmd%
exit /B 21
rem Archive each subfolder in specified or default folder to archive
rem as separate archive with name of folder as archive file name and
rem with current date and an automatically incremented number with at
rem least two digits appended to the archive file name to be able to
rem create multiple archives on different days and even on same day.
rem Parent directory path of each subfolder is removed from archive.
rem The name of the subfolder itself is added to each archive. This
rem can be changed by replacing "%%D" with "%%D\" or "%%D\*". Then
rem the files and subfolders of the compressed folder would be added
rem to archive without the name of the compressed folder.
rem Best compression is used on creating a solid archive with 4 MB
rem dictionary size. All messages are suppressed except error messages.
rem The last modification time of the created archive file is set to
rem date and time of newest file inside the archive.
:CreateArchives
set "RarError=0"
(for /D %%I in ("%FolderToArchive%\*") do (
echo Archiving %%I ...
"%ProgramFiles%\WinRAR\Rar.exe" a -ag_YYYY-MM-DD_NN -cfg- -ep1 -idq -m5 -md4m -r -s -tl -y "%BackupFolder%\%%~nI.rar" "%%I"
if errorlevel 1 call :GetRarError
)) & goto CheckRarError
:GetRarError
echo/
echo/
if %ERRORLEVEL% GTR %RarError% set "RarError=%ERRORLEVEL%"
goto :EOF
rem Wait for a key press if an error occurred on creating an archive
rem file and the option to suppress the pause on error is not used.
:CheckRarError
if not defined PauseCmd goto EndBatch
if not %RarError% == 0 %PauseCmd%
rem Exit with highest exit code of any RAR execution.
:EndBatch
exit /B %RarError%
For the details on the used switches on Rar command line open text file Rar.txt
in program files folder of WinRAR which is the manual for the console version Rar.exe
and read the explanations for those switches.
Note: Command a (add to archive) is used in batch code above instead of m (move to archive).
The manual for using WinRAR.exe
from within a batch file can be found in help of WinRAR on tab Contents under list item Command line mode.
There are some differences on list of switches between console and GUI version of WinRAR. For example WinRAR.exe
supports also creating ZIP archives which Rar.exe
does not support. Therefore WinRAR.exe
supports the switch -af<type>
which console version does not. The switch -idq
(quiet mode) of console version is switch -ibck
(run in background) for GUI version.
For understanding the used 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.
call /?
echo /?
exit /?
for /?
goto /?
if /?
md /?
pause /?
rem /?
set /?
setlocal /?
shift /?
Note: Such an archiving can be also done with WinRAR by selecting in WinRAR the folders to archive, clicking on Add icon in toolbar, inserting C:\Backup\
on Archive name and enabling option Put each file to separate archive on tab Files. The other options used in the batch file above defined via switches can be found on the tabs General, Backup and Time.
精彩评论