+ Post New Thread
Results 1 to 9 of 9
Scripts Thread, script to capitalize first letter of words, separated with full stop in Coding and Web Development; Just wondering if someone could knock us up a simple script, bash, or perl, or python, or whatever is easiest.. ...
  1. #1

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,303
    Blog Entries
    6
    Thank Post
    1,249
    Thanked 456 Times in 296 Posts
    Rep Power
    156

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

    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

  2. IDG Tech News
  3. #2

    Join Date
    Apr 2007
    Location
    Birmingham
    Posts
    112
    Thank Post
    4
    Thanked 40 Times in 35 Posts
    Rep Power
    19
    Not a script, but this may help - Renamer is quite useful for file renaming.

    You can add a case rule to captialise every word which should do what you require.

  4. #3

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,303
    Blog Entries
    6
    Thank Post
    1,249
    Thanked 456 Times in 296 Posts
    Rep Power
    156
    sorry should have stipulated.. im not running windows.

  5. #4
    Arcath's Avatar
    Join Date
    Feb 2009
    Location
    Lancashire
    Posts
    791
    Thank Post
    66
    Thanked 78 Times in 70 Posts
    Rep Power
    28
    does the script need to look through a given directory? or will you just pass a filename to it?

    is ruby allowed?

  6. #5

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,303
    Blog Entries
    6
    Thank Post
    1,249
    Thanked 456 Times in 296 Posts
    Rep Power
    156
    yeh preferably to traverse through a whole directory tree

    Never used Ruby before, but I can install it no worries ...

  7. #6
    monkeyx's Avatar
    Join Date
    Nov 2006
    Posts
    361
    Thank Post
    8
    Thanked 52 Times in 41 Posts
    Rep Power
    23
    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

    Code:
    #!      /usr/bin/python
    #       Copyright 2008 Thura <thurahlaing06@gmail.com>
    #       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 <or>
        #             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)

  8. Thanks to monkeyx from:

    RabbieBurns (24th September 2009)

  9. #7

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,303
    Blog Entries
    6
    Thank Post
    1,249
    Thanked 456 Times in 296 Posts
    Rep Power
    156
    That works great, cheers.

    Its no big deal about the last 3 chars I guess..

    Thanks again

  10. #8

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,303
    Blog Entries
    6
    Thank Post
    1,249
    Thanked 456 Times in 296 Posts
    Rep Power
    156
    Code:
    #!      /usr/bin/python
    #       Copyright 2008 Thura <thurahlaing06@gmail.com>
    #       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 <or>
        #             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.

  11. #9
    monkeyx's Avatar
    Join Date
    Nov 2006
    Posts
    361
    Thank Post
    8
    Thanked 52 Times in 41 Posts
    Rep Power
    23
    Great News

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 9
    Last Post: 9th April 2009, 07:33 AM
  2. Replies: 0
    Last Post: 16th September 2008, 08:56 AM
  3. Replies: 28
    Last Post: 28th November 2006, 03:32 PM
  4. Certain letter keys stop working
    By EL_S in forum Windows
    Replies: 4
    Last Post: 9th June 2006, 04:37 PM
  5. Words fail me #2
    By KarlGoddard in forum General Chat
    Replies: 14
    Last Post: 1st March 2006, 10:51 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •