Click to See Complete Forum and Search --> : converting uppercase to lowercase filename


nopri
03-29-2001, 02:12 AM
hello,
i'm very new in shellscript, but i have to convert all file in folder 'A' from uppercase to lowercase, anyone cal help me, i'm using bash.

jemfinch
03-29-2001, 02:17 AM
cd A
for file in *
do
mv $file `echo $file | perl -pe 'tr/a-z/A-Z/'`
done


Jeremy

pwrhouse
03-29-2001, 04:49 AM
I bet it would be easier with python ;)

Ben Briggs
03-30-2001, 12:25 AM
pwrhouse: Damn right, I'm surprised jemfinch didn't include a Python version too :).

I wrote one just the other day... but I'm on my family's computer (as opposed to mine), but I remember it used the string module, and that there was a specific method for converting to lower-case... when I get on my computer tonight, I'll post the code (if someone else doesn't beat me :)).

jemfinch
03-30-2001, 02:12 AM
#!/usr/bin/env python

import os, sys, string

if len(sys.argv) < 2:
print 'Usage: %s <directory>' % sys.argv[1]
sys.exit(-1)

for file in os.listdir(sys.argv[1]):
newfilename = string.upper(file)
os.rename(os.path.join(sys.argv[1], file),
os.path.join(sys.argv[1], newfilename))


It's a little more flexible this way.

Jeremy

Ben Briggs
03-30-2001, 02:14 AM
As promised... nocaps.py (http://briggs.250x.com/python/nocaps.py)

Ben Briggs
03-30-2001, 02:16 AM
I was posting mine when jemfinch answered! jemfinch's is definately more effecient than mine though, as usual :)

jemfinch
03-30-2001, 04:26 PM
I hope Ben doesn't mind if I comment about his code...


count = 0
for arg in sys.argv:
if count < 1:
pass


Instead of this, you should do this:


for arg in sys.argv[1:]


Then you don't have to worry about counting your place in sys.argv.

My wild guess is that you've programmed another language that iterates with numbers instead of iterating over sequences. :)


sys.argv[count] = arg


Honestly, this doesn't do anything :) Note that you don't use this after the statement itself.


count += 1


Just as a note (I don't know if it matters to you or not,) += isn't backwards compatible with 1.5.2, which is still the default at least in debian, and perhaps in other distributions.

One problem with the script as a whole is that it requires full pathnames, but uppercases the whole full pathname. That won't work for paths that include a directory if all the directories in the path aren't uppercase. For example:

./nocaps.py foo/bar.mp3 bar/baz.html

would fail since after the uppercasing, it would be

FOO/BAR.MP3 BAR/BAZ.HTML

and directories FOO and BAR don't exist.

Instead, if you want to maintain your "files on the command line" interface, you can use os.path.basename to get the filename component, uppercase that, and then os.path.join it with the os.path.dirname component of the filename.

Jeremy

P.S. -- s/upper/lower/g, I missed which case your program worked on for a second.

Ben Briggs
03-31-2001, 02:14 AM
jemfinch: I certainly don't mind you providing some constructive critisism on my code... It will help me become a better Python programmer. And yes, I had experience in C/C++ before I tried Python (that's where I got my counting through an array habbit).

I just double checked... '+=' isn't compatable with 1.5.2, but I hope that isn't going to matter much :).

I'll look into you suggestions as to how to improve my script.

This is just FYI, nopri was looking for how to rename files with all caps, to all lowercase letters, not the other way arround :).

nopri: I'm sure you're getting a little annoyed with all the E-mails notifying you that someone has replied to your post, only to find out it's me and jemfinch talking about Python :D.

[ 31 March 2001: Message edited by: Ben Briggs ]