Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a text file that I want to be archived with each file in a particular folder.

For example, the folder has: File1,File2,File3,TextDoc1`

I want (which includes File1+TextDoc1), (Which includes File2+TextDoc1), (which includes File3+TextDoc1)

Is there a batch command that I can run (as a .bat) to create these archives instead of making each one individually? I've been using listfiles, but I still need to create each .txt file with the filenames in it.

This is what I've been using:

"C:\Program Files\WinRAR\WinRAR.exe" a -esh -m3 -mt2 -r -t "" @"File1.txt"

"C:\Program Files\WinRAR\WinRAR.exe" a -esh -m3 -mt2 -r -t "" @"File2.txt"


Zip or Rar is fine.

share|improve this question
up vote 1 down vote accepted

@echo off for %%f in (*) do (       if not %%f==TextDoc1 (            (your command to create%%~nf.zipcontaining%%fandTextDoc1)       ) )

for %%f in (*) means for each file in the current directory — DO the command to create the ZIP file (the details of which are irrelevant) — except when you’re considering the TextDoc1 file itself.  Skip that, or else you’ll create a file that contains two copies of TextDoc1 (or maybe just one, depending on how WinRAR works).

The one tricky thing is the %%~nf, which is the filename portion of %%f (with the extension removed).  I used this because, while you gave File1, File2, and File3 as example file names, I guess that you also have File4.txt, résumé.doc, flower.jpg, etc.  I’m making the wild guess that you want the ZIP files to be called, résumé.zip, and, rather than, résumé, and  Of course that means that if there are schedule.mpp and schedule.xls, we may have a problem.

share|improve this answer
Thank you for the help and clarification. I am very new to using commands, so this code was a great base for my system. – Luke Dec 5 '12 at 23:13

Here's a batch you can try:

@echo off

rem Set highest number
set /a highcount=3

rem Initialize counter to 1
set /a counter=1

start /wait "C:\Program Files\WinRAR\WinRAR.exe" a -esh -m3 -mt2 -r -t "" "File%counter%"
start /wait "C:\Program Files\WinRAR\WinRAR.exe" a -esh -m3 -mt2 -r -t "" "TextDoc1"

rem Increment the counter by 1
set /a counter+=1

rem If the counter is less than or equal to "highcount", then loop and do the next file.
if %counter% LEQ %highcount% goto loop

This should create 3 zip files named, and, each with a matching "File#" file and the (same) "TextDoc1" in them.

share|improve this answer
Thank you for the quick response! Sorry, the Files in the folder are not necessarily Numbered sequentially. It would be more appropriate to say FileABC, FileDEF, FileGHI and TextDoc1. Thank you for your time/effort! – Luke Dec 5 '12 at 22:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .