Jump to content

script to capitalize first letter of words, separated with full stop


Recommended Posts

Posted

Just wondering if someone could knock us up a simple script, bash, or perl, or python, or whatever is easiest..

 

basically just looking to try to change

 

this.is.the.filename.ext

 

to

 

This.Is.The.Filename.ext

 

Thanks in advance :)

Posted

Here is a pyhton script that I use where I use the string.title() function to do what you are asking for. It does also captize the extension, but sure you could work out how set the last 3 chars to lowercase :)

 

#!      /usr/bin/python
#       Copyright 2008 Thura 
#       Just a small script to rename files and folders in nautilus recursively
#       Tested under Ubuntu 8.04, should work fine with other Linux dist ...


import os,sys,string
dirs = []
def ren(path):
   oldname = os.path.split(path)[1]
   newname = oldname.title()

   print 'Renaming ' + oldname + ' to ' + newname;
     #Change this line for your preferred filename ...
   #For example, newname = oldname.lower()
   #             newname = "Your Prefix" + oldname 
   #             newname = oldname + "Your suffix"
   newpath = os.path.join(os.path.split(path)[0],newname)
   os.rename(path,newpath)
   if os.path.isdir(newpath):
       for name in os.listdir(newpath):
           ren(os.path.join(newpath,name))
for i in sys.argv[1:]:
   dirs.append(i)
for dir in dirs:
   ren(dir)

Posted

#!      /usr/bin/python
#       Copyright 2008 Thura 
#       Just a small script to rename files and folders in nautilus recursively
#       Tested under Ubuntu 8.04, should work fine with other Linux dist ...


import os,sys,string
dirs = []
def ren(path):
   oldname = os.path.split(path)[1]
   newname = oldname.title()

   temp = newname.split(".")
   temp[-1] = temp[-1].lower()
   newname = ".".join(temp)

   print 'Renaming ' + oldname + ' to ' + newname;
     #Change this line for your preferred filename ...
   #For example, newname = oldname.lower()
   #             newname = "Your Prefix" + oldname 
   #             newname = oldname + "Your suffix"
   newpath = os.path.join(os.path.split(path)[0],newname)
   os.rename(path,newpath)
   if os.path.isdir(newpath):
       for name in os.listdir(newpath):
           ren(os.path.join(newpath,name))
for i in sys.argv[1:]:
   dirs.append(i)
for dir in dirs:
   ren(dir)

 

Modified to lowecase the last word.. which works perfectly..

 

Thanks again.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...