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 after 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 first parameter on running the batch file.
@echo off
setlocal
set "BackupFolder=C:\Backup"
rem Folder to archive can be optionally specified as parameter.
if "%~1" == "" (
set "FolderToArchive=C:\projects"
) else (
set "FolderToArchive=%~1"
)
rem Check existence of the folder to archive.
if not exist "%FolderToArchive%\*" (
echo.
echo Error: Folder %FolderToArchive% does not exist.
echo.
endlocal
pause
goto :EOF
)
rem Check existence of backup folder and create this folder
rem if not already existing with verification on success.
if not exist "%BackupFolder%\*" (
md "%BackupFolder%"
if errorlevel 1 (
echo.
echo Error: Folder %BackupFolder% could not be created.
echo.
endlocal
pause
goto :EOF
)
)
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 2 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 compfessed 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.
set "RarError=0"
for /D %%D in ("%FolderToArchive%\*") do (
echo Archiving %%D ...
"%ProgramFiles%\WinRAR\Rar.exe" a -ag_YYYY-MM-DD_NN -cfg- -ep1 -idq -m5 -md4m -r -s -tl -y "%BackupFolder%\%%~nD.rar" "%%D"
if errorlevel 1 set "RarError=1"
)
rem Wait for a key press if an error occurred on creating an archive file.
if "%RarError%" == "1" (
echo.
pause
)
endlocal
For 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 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. Or 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.
echo /?
endlocal /?
for /?
if /?
md /?
pause /?
rem /?
set /?
setlocal /?
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.
来源:https://stackoverflow.com/questions/2954611/how-to-archive-each-folder-in-a-directory-separately-using-winrar