How to archive each folder in a directory separately using WinRAR?

和自甴很熟 提交于 2019-12-06 09:50:24

I think you need to change a couple things.

  1. Change /A to /AD to get just the directories.
  2. Remove the /S so you will only get the top-level directories in C:\Projects.
  3. Inside your FOR loop, change the "c:\backup\projects.rar" to C:\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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!