I need to set up an archiving task on Windows 7 using Windows command line and WinRAR (or some other similar program). I want to create a script that does this, but I have no prior experience.

I want to be able to:

  • recreate folders substructure down to a specified level

  • have the contents of each sub-folder at the last level be archived in one archive bearing the name of that sub-folder

  • password protect the archive

  • split the archive into chunks of a specified size

  • have the ability to include only files with the A(rchive) attribute

  • clear the A(rchive) attribute after archiving

  • have the possibility to exclude certain files and folders, by specifying them in a text file.

Any help is much appreciated.

I am also ready to learn new things, so pointing me to some tutorials that would help me accomplish this task is also very much appreciated.


Consider the following example: assume I want to archive the contents of a folder called DOCS and recreate its structure down to Level 2 ('DOCS' is considered to be Level 0). Assume that the structure of sub-folders, down to the second level is the following:







I want to obtain the following structured back-up (on a different partition or drive):







  • 1
    Have you looked at the WinRAR command-line options and identified those that might help you? Can you also explain your second requirement in detail, preferably with a small example? – Karan May 25 '13 at 14:25
  • @Karan: I edited the op and gave a minimal example. I looked over WinRAR command-line options, but I can't recreate the folder structured as mentioned (all the other aspects can be achieved within WinRAR, but I don't know how to go through the folder structure automatically). – digital-Ink May 25 '13 at 14:55
  • Hmm, what you want to do (separately archive the contents of each 2nd level dir) might be possible with a fairly complex batch script. Do you absolutely need this to be done via batch though? IMO something like this might be much easier to accomplish in PowerShell instead of breaking your head over a batch-based solution. – Karan May 25 '13 at 15:07
  • @Karan: Any kind of solution is welcomed. – digital-Ink May 25 '13 at 15:17
  • 1
    I'm not a PS expert, but I edited the tags so hopefully those keeping a watch on the PS tag will be notified of this question and respond. – Karan May 25 '13 at 15:27

This works for step 1 and 2; this creates a rar file named "Level2Name.rar" for each of the level 2 folders in a mirrored folder tree structure:

If you want to include the folder tree structure inside the rar file ("Docs/Lv1/Lv2/Files") just remove -ep from the winrar command.

set savelocation=D:\Backups
set path=%path%;C:\Program Files\WinRAR
for /F "eol= usebackq delims=" %%A IN (`dir "DOCS" /A:D /B`) do call :FL1 "%%A"
goto :eof

pushd DOCS\%1
set level1="%~nx1"
for /F "eol= usebackq delims=" %%A IN (`dir * /A:D /B`) do call :FL2 "%%A"
goto :eof

set level2="%~nx1"
if not exist "%savelocation%\DOCS\%level1%\%level2%" mkdir "%savelocation%\DOCS\%level1%\%level2%"
winrar a -ibck -inul -ep "%savelocation%\DOCS\%level1%\%level2%\%level2%.rar" "DOCS\%level1%\%level2%"
goto :eof

  • +1 Haven't tested this myself, but looks good! – Karan May 25 '13 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.