WinRar Command to Compress(Zip) Every File in Folder with Another File

2

1

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, and TextDoc1.

I want to create File1.zip (which includes File1 and TextDoc1), File2.zip (which includes File2 and TextDoc1), and File3.zip (which includes File3 and 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.

Luke

Posted 2012-12-05T19:09:22.643

Reputation: 23

Answers

1

@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.

Scott

Posted 2012-12-05T19:09:22.643

Reputation: 17 653

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 – 2012-12-05T23:13:51.537

0

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.

Ƭᴇcʜιᴇ007

Posted 2012-12-05T19:09:22.643

Reputation: 103 763

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 – 2012-12-05T22:32:22.157