Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Do you know any smart script to rename files for web format?
(replace all non ASCII characters, spaces, transliterate unicode chars, change case etc.)

eg.

my ójf ćżpd - ąąv - hźóż HŹŃÓKŁFU.jpg

to

my_ojf_czpd-aav_-_hzoz_HZNOKLFU.jpg

I've been playing with rename command, but there is always some new character which my regex does not support. I'm sure there is already a good tool for this task.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Can you use Python? This little script:

import urllib
import unicodedata
print urllib.quote_plus(unicodedata.normalize(NFKD', u'my ójf ćżpd - ąąv - hźóż ŹŃÓKŁFU.jpg').encode('ascii','ignore')).replace('+','_')

Produces your requested output of:

 my_ojf_czpd_-_aav_-_hzoz_HZNOKFU.jpg

This creates an output that is all ASCII and escapes ASCII characters not allowable in URLs. If that does what you are after it shouldn't take much to turn it into the script you need.

share|improve this answer
    
I have ever used Python before :) Looks promising, but how to recursive iterate the files in the directory? –  takeshin Jul 9 '10 at 20:23
    
import os os.listdir(.) returns a list containing all the files in the directory –  Tom O'Connor Jul 9 '10 at 20:31
    
mayankjohri.wordpress.com/2008/07/02/… This works. Instead of appending the files to a list, you'll run the translation code above and then perform the rename from old to new –  bk. Jul 9 '10 at 20:37

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.