Once upon a time Simon Liddington wrote:
> 
> 
> Can anyone think of an easy way of changing the case of the names of a
> directory tree of files?

This was something I often wanted to do and it seemed like a good
makework Python project while I was learning the language so, I append
a Python script which does more than you want, but it *does* do what
you want - and it will recurse, which I don't think any other solution
posted so far does...

-- 
|Deryk Barker, Computer Science Dept. | Music does not have to be understood|
|Camosun College, Victoria, BC, Canada| It has to be listened to.           |
|email: [EMAIL PROTECTED]         |                                     |
|phone: +1 250 370 4452               |         Hermann Scherchen.          |

-------------------------------------------------------------------------------
#!/usr/local/bin/python
#
# Upper/lower case file names
#
import getopt
import glob
import os
import sys
import string

recursing = None                        # default is not to recurse
files     = 1                           # and to process files
directories = None                      # but not directories
verify = None                           # don't normally say what we're doing
        
                                        # Now a dictionary mapping command 
                                        # names to functions
commands = {'icn': lambda x: string.upper (x[0]) + string.lower(x[1:]),
            'lcn': string.lower,
            'ucn': string.upper}

stdopts = 'fdarv'                       # options:
                                        # -f    files only
                                        # -d    directories only
                                        # -a    all (files and dirs)
                                        # -r    recurse
                                        # -v    verify

#
# Try to change a single name
#
    
def changeOneName (name, dir, file):
  newname = os.path.join (dir, function (file))
  if verify:
    print "Renaming: ", name, " => ", newname
  try:
    os.rename (name, newname)
  except:
    sys.stderr.write (sys.argv[0] + ": cannot access " + name + "\n")

#
# Change case of all names. Test for inclusion of files and/or directories
# Recurse into subdirectories if necessary.
#
def changeName (name):                  # change case of name
  dir, file = os.path.split (name)
  if os.path.isdir (name):              # a directory
    if directories:                     # change its name?
      changeOneName (name, dir, file)
    if recursing:                       # recursing
      names = glob.glob (name + "/*")   # build list of names
      for child in names:
        changeName (child)
  else:                                 # not a directory
    if files:
      changeOneName (name, dir, file)

#
# Main code
#

if __name__ == '__main__':
  try:
    function = commands[sys.argv[0][-3:]]
  except:
    sys.stderr.write ("Usage icn|lcn|ucn [-adlrv] names\n")
    sys.exit (1)

  opts, args = getopt.getopt (sys.argv[1:], stdopts)

  for opt, value in opts:
    if opt == '-a':
      files = directories = 1
    elif opt == '-d':
      files = None
      directories = 1
    elif opt == '-f':
      files = 1
      directories = None
    elif opt == '-r':
      recursing = 1
    elif opt == '-v':
      verify = 1

  if not args:
    sys.stderr.write ("Usage icn|lcn|ucn [-adlrv] names\n")
    sys.exit (1)

  for name in args:
     changeName (name)




-- 
  PLEASE read the Red Hat FAQ, Tips, Errata and the MAILING LIST ARCHIVES!
http://www.redhat.com/RedHat-FAQ /RedHat-Errata /RedHat-Tips /mailing-lists
         To unsubscribe: mail [EMAIL PROTECTED] with 
                       "unsubscribe" as the Subject.

Reply via email to