Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 File1.zip (which includes File1+TextDoc1), File2.zip (Which includes File2+TextDoc1), File3.zip (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.zip" @"File1.txt"

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

etc.

Zip or Rar is fine.

share|improve this question

2 Answers 2

up vote 0 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 TextDoc1.zip 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 File4.zip, résumé.zip, and flower.zip, rather than File4.txt.zip, résumé.doc.zip, and flower.jpg.zip.  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

:loop
start /wait "C:\Program Files\WinRAR\WinRAR.exe" a -esh -m3 -mt2 -r -t "File%counter%.zip" "File%counter%"
start /wait "C:\Program Files\WinRAR\WinRAR.exe" a -esh -m3 -mt2 -r -t "File%counter%.zip" "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 File1.zip, File2.zip and File3.zip, 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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