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 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)

share|improve this question
add comment

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

share|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
add comment

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");
share|improve this answer
    
It does, but here's the trick; zipfile doesn't support RAR archives. –  Chris R Mar 9 '10 at 16:07
2  
True, but there's a rarfile package which does: github.com/markokr/rarfile –  Tim Gilbert Apr 28 '13 at 19:07
add comment

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.

share|improve this answer
add comment

I've been looking into this too, and I suspect the easiest thing to do is probably write a python script to do it, especially because you'll be dealing with both RAR and ZIP files.

It looks like there's not a particularly clean way to rename a file in a zip archive without unzipping all the files, renaming the ones you want to rename, and then zipping them back up, which is not really ideal. However, I did stumble upon a OS/X utility called zipnote which does purport to do this:

zipnote man page

It doesn't look terribly easy to use, but it might be worth looking into, and assuming it doesn't actually unzip the files itself it should be fairly easy to automate. Meanwhile, the command-line OS/X rar utility does support renaming files inside of RAR archives via rar rn foobar.rar oldname newname.

share|improve this answer
add comment

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.