I have a set of archive files -- both zip and rar formats -- inside of which I need to rename some files. Specifically, I want to do something like this:

for each archive file in a directory
    for each file in the archive
        if the file name matches the regular expression /(.* - [0-9]{2})([0-9]{2} - .)*/
            rename the file as \1-\2

The trick isn't so much in the generation of the new name; I can do that with either bash or sed or anything else. It's the set of commands to manipulate the files in the archives using rar/unrar or unzip/zip

(If it makes a difference, I'm re-formatting some CBR/CBZ files to get the double-page spreads to come up in the right order in SimpleComic -- it interprets page 0203 as page 203, which makes the story a bit hard to follow)

link|improve this question

37% accept rate
feedback

3 Answers

up vote 1 down vote accepted

I think the easiest way you'll find to do this is to unpack each archive, do the rename operation, then repack it.

On Linux, I could use a FUSE filesystem to have transparently decompressed archives, which I could operate on like regular folders, using a batch script to do all the renaming. I don't know if that particular FUSE filesystem exists for Mac.

link|improve this answer
Which fuse FS is that? – Chris R Jan 28 '10 at 23:07
Here's a list of candidates: sourceforge.net/apps/mediawiki/fuse/… I haven't actually used any myself. I just said that I could. – Ryan Thompson Jan 30 '10 at 23:18
feedback

I look ate you example and I see PYTHON CODE :)

Really I think this should be the way to go. I know this isn't stackoverflow.... but this is the only way I can see your problem solved.

Example code to extract all files:

import zipfile

a = zipfile.ZipFile("C:\\x.zip", 'r')
for i in a.namelist():         #Extract every file from it
     b = open("C:\\"+i, 'wb')
     b.write(a.read(i))
     #HERE YOU INSERT WHAT YOU WANT TO DO TO EACH FILE (b is the last file)
     b.close()
a.close()

Hope this helps

This page teaches how to instal an unrar for the command line for MAC.

So you can do a script to unrar and a script to rar your files and call then from python, and make python change the names.

To execute an external script use:

import os;
os.system("path/scritname");
link|improve this answer
It does, but here's the trick; zipfile doesn't support RAR archives. – Chris R Mar 9 '10 at 16:07
feedback

rar has a command line switch to rename files in the archive:

rar rn archive.rar filename newfilename

however the zip utility you're using does not. You'll need to extract, rename, and recompress as Ryan stated. There are other zip utilities for Mac which probably have this capability. I know 7-zip has supported renaming in archives since 2.30 beta 25, although I haven't checked if they support it from the command line. 7-zip is fairly extensive when it comes to command line options, so I'd assume it can.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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