Looking for simple renaming program

Posted by: BartDG

Looking for simple renaming program - 30/03/2008 16:36

Hi Guys,

I'm currently ordering my media files somewhat. I personally like to see all my files with every first letter capitalised. Don't ask me why, I just like them that way, it looks good to me.

eg. I like the files to look: "Assault On Precinct 13.avi", and not "assault on precinct 13.avi".

I also have done this for all my MP3 files. But for these kind of files, this was easy since Tag & Rename can very easily do this. Literally only one click. But this only works for MP3 files sadly.

So basically, I'm looking for renaming software which can rename an entire folder like the way I've described. I'm sure this exists, I only have no idea where to look and my Goofle-Fu seems to be incredibly weak today...

Does anyone of you know of a program which can do this?

Cheers!
Posted by: StigOE

Re: Looking for simple renaming program - 30/03/2008 16:47

A quick Google gave me these links (I have no idea how good they are...)
Magic Video Batch Converter
FileName Pro
Batch File Utility
All of them are shareware, though.

Stig
Posted by: oliver

Re: Looking for simple renaming program - 30/03/2008 21:58

I was bored this afternoon, so I wrote something quick and dirty that "should" take care of this for you.

It requires .net 2.x or greater. Open a command line window, and run the exe... Syntax is as follows...

FileRenamer.exe -path "c:\users\oliver\music" -pattern *.mp3 -recursive -update

NOTE: when -update is specified, it will actually rename the files. i'd suggest on the first run you remove the -update and look over the list of files. if you don't want to scan/update subfolders remove the -recursive function.

-pattern is also optional, if you leave it out it will basically just do *.*

the ugly code is also in the zip file, if you have visual studio.net 2005/8 you can compile it yourself. otherwise you'd need the SDK and a command I can't remember...




Posted by: Dignan

Re: Looking for simple renaming program - 31/03/2008 02:31

Please give The Rename a try. I swear by the program and have used it on a very regular basis for about 6 years.
Posted by: mlord

Re: Looking for simple renaming program - 31/03/2008 13:31

I'm sure a real shell wizard could do a lot better, but at least this is bulletproof:
Code:
#!/bin/sh
# capitalize first letter of all items in current directory:
ls -1 | awk '{gsub("\"","\\\""); system("mv \"" $0 "\" \"" toupper(substr($0,1,1)) substr($0,2) "\"")}'

Posted by: mlord

Re: Looking for simple renaming program - 31/03/2008 13:49

Originally Posted By: mlord
I'm sure a real shell wizard could do a lot better, but at least this is bulletproof:
Code:
#!/bin/sh
# capitalize first letter of all items in current directory:
ls -1 | awk '{gsub("\"","\\\""); system("mv \"" $0 "\" \"" toupper(substr($0,1,1)) substr($0,2) "\"")}'

Translated to human-speak, it breaks down as follows:
  • Standard shell script header:
    #!/bin/sh
  • List directory contents, one item per line:
    ls -1
  • Feed the directory listing into an awk program:
    | awk '{ ... }'
  • Insert a backslash in front of any/all double-quotes within file names, so that we can then safely use then inside double-quoted expressions in the program:
    gsub("\"","\\\"");
  • Execute a new command line, using mv to rename things:
    system("mv " OLD " " NEW)
  • OLD and NEW are in this case, double-quoted file names. The OLD name is simply the current line of input to the awk program, known as $0. Double-quoted, this becomes:
    "\"" $0 "\""
  • The NEW name is built from two parts: the first character of the OLD name, substr($0,1,1), and the rest of the OLD name, substr($0,2). Again, double-quoted:
    "\"" toupper(substr($0,1,1)) substr($0,2) "\""
  • Put it all together, and we see this:
    system("mv " "\"" $0 "\"" " " "\"" toupper(substr($0,1,1)) substr($0,2) "\"")
  • We can collapse adjacent double-quoted strings together, to simplify it somewhat:
    system("mv \"" $0 "\" \"" toupper(substr($0,1,1)) substr($0,2) "\"")

And that's the final result. smile
Posted by: BartDG

Re: Looking for simple renaming program - 01/04/2008 08:00

Guys, thank you all so very much for going through so much trouble! smile

Wow! This forum still is the dogs' you-know-what!

Cheers!
Posted by: mlord

Re: Looking for simple renaming program - 01/04/2008 09:56

Originally Posted By: mlord
And that's the final result. smile

Or this version, for folks who find one-liners difficult: smile

Code:
#!/bin/sh
# capitalize first letter of all items in current directory:
ls -1 | awk '
        ## This script gets run once for each line of output from the "ls -1" command.
        {
                space = " "             ## a space character
                dquote = "\""           ## a double-quote character
                backslash = "\\"        ## a backslash character

                ## Each line of input ($0) is a new file name to convert:
                old_name = $0

                ## Insert backslashes in front of any " characters inside the file name,
                ## so that they are preserved when we later invoke system() on it:
                gsub(dquote, backslash dquote, old_name)

                ## Extract the first character of the file name:
                first_char = substr(old_name,1,1)

                ## And the rest of the characters:
                the_rest = substr(old_name,2)

                ## Don't try to rename unless the first character is a lowercase letter:
                if (first_char ~ "[a-z]") {
                        ## Assemble the new file name:
                        new_name = toupper(first_char) the_rest

                        ## Issue a command to rename the file:
                        rename = "/bin/mv"
                        system(rename space dquote old_name dquote space dquote new_name dquote)
                }
        }'
Posted by: peter

Re: Looking for simple renaming program - 01/04/2008 12:33

Quote:
# capitalize first letter of all items in current directory

But that's not quite what Archeon asked for: the idea was to capitalise the first letter of every word in each filename. Which is harder: beyond my sed-fu, and I don't really have any awk-fu. I'd probably just do it in C if I had to.

Peter
Posted by: mlord

Re: Looking for simple renaming program - 01/04/2008 13:09

Originally Posted By: peter
Quote:
# capitalize first letter of all items in current directory

But that's not quite what Archeon asked for: the idea was to capitalise the first letter of every word in each filename. Which is harder

Oh dear, you are correct (as always). smile

Here's a program to do what what was asked:
Code:
#!/bin/sh
# capitalize first letter of all items in current directory:
ls -1 | awk '
        ## This script gets run once for each line of output from the "ls -1" command.
        {
                space = " "             ## a space character
                dquote = "\""           ## a double-quote character
                backslash = "\\"        ## a backslash character

                ## Each line of input ($0) is a new file name to convert:
                old_name = $0

                ## Insert backslashes in front of any " characters inside the file name,
                ## so that they are preserved when we later invoke system() on it:
                gsub(dquote, backslash dquote, old_name)

                ## Copy old_name to new_name, and capitalize first letters of all words:
                in_word = 0
                new_name = ""
                for (i = 1; i <= length(old_name); ++i) {
                        tail = substr(old_name,i)
                        c = substr(tail,1,1)
                        if (tail != ".mp3" && tail != ".avi") {
                                if (c !~ "[a-zA-Z]") {
                                        in_word = 0
                                } else if (in_word == 0) {
                                        in_word = 1
                                        c = toupper(c)
                                }
                        }
                        new_name = new_name c
                }

                ## If new_name differs from old_name, then rename the file:
                if (new_name != old_name) {
                        ## Issue a command to rename the file:
                        rename = "/bin/mv"
                        print  rename space dquote old_name dquote space dquote new_name dquote
                        system(rename space dquote old_name dquote space dquote new_name dquote)
                }
        }'


EDIT: updated to leave the ".avi" or ".mp3" extension as-is.
Posted by: tonyc

Re: Looking for simple renaming program - 01/04/2008 13:10

I'd just uze zsh's "zmv" function:

Code:
autoload -U zmv
zmv -v '(* *)' '${(C)1}'


Or, to do it recursively:

Code:
zmv -v '(**/)(* *)' '$1${(C)2}'


Run zmv with -n option for a dry run without actually renaming the files. For some other tasty examples of what zmv can do, check here towards the middle of the page.
Posted by: wfaulk

Re: Looking for simple renaming program - 01/04/2008 13:11

You realize that he's using Windows, right? And that he'll have to download megabytes worth of Cygwin or something in order to use this script. That may or may not do what he wants. And is virtually unreusable.
Posted by: mlord

Re: Looking for simple renaming program - 01/04/2008 13:13

Originally Posted By: wfaulk
You realize that he's using Windows, right?

No, actually.

But what I'm really doing here, is demonstrating how certain classes of problems can be solved for free, without having to install binary malware from whomever.

And thus demonstrating that sometimes, free software is worth a little effort.

Quote:
And is virtually unreusable.

That's about as misleading as they come. This stuff is incredibly reusable -- I'm re-using a single program called awk to do this random task, just like I re-use it for hundreds of other random tasks.

Cheers
Posted by: mlord

Re: Looking for simple renaming program - 01/04/2008 13:22

Originally Posted By: tonyc
I'd just uze zsh's "zmv" function:
Code:
autoload -U zmv
zmv -v '(* *)' '${(C)1}'

That looks rather clever, except it doesn't work on anything other than very simple, space-separated words in quote-free file names.

But still, very clever!
Posted by: tonyc

Re: Looking for simple renaming program - 01/04/2008 13:29

Originally Posted By: mlord

That looks rather clever, except it doesn't work on anything other than very simple, space-separated words in quote-free file names.


huh?

Code:
$ ls -1
FILENAME 'wItH sinGLE quotes'
filename "with double quotes"
foo BAR-bAz quxxX

$ zmv -v '(* *)' '${(C)1}' 
mv -- FILENAME\ \'wItH\ sinGLE\ quotes\' Filename\ \'With\ Single\ Quotes\'
mv -- filename\ \"with\ double\ quotes\" Filename\ \"With\ Double\ Quotes\"
mv -- foo\ BAR-bAz\ quxxX Foo\ Bar-Baz\ Quxxx

$ ls -1
Filename "With Double Quotes"
Filename 'With Single Quotes'
Foo Bar-Baz Quxxx


confused
Posted by: mlord

Re: Looking for simple renaming program - 01/04/2008 13:31

Oh, even better, then!

Now if it would only handle "my_files_have_123under.scores.mp3" smile

Heh.. which reminds me.. my own example would capitalize the .Avi or .Mp3 part (oops)!

(now fixed).
Posted by: tonyc

Re: Looking for simple renaming program - 01/04/2008 13:36

It does handle that case, at least as far as the capitalization is concerned. Replacing the underscores with spaces is left as an exercise for the reader.
Posted by: mlord

Re: Looking for simple renaming program - 01/04/2008 13:37

Originally Posted By: tonyc
It does handle that case, at least as far as the capitalization is concerned. Replacing the underscores with spaces is left as an exercise for the reader.


And dots, and dashes, and colons, and commas ... smile
Posted by: Dignan

Re: Looking for simple renaming program - 01/04/2008 14:07

Originally Posted By: mlord
Originally Posted By: wfaulk
You realize that he's using Windows, right?

No, actually.

But what I'm really doing here, is demonstrating how certain classes of problems can be solved for free, without having to install binary malware from whomever.

And thus demonstrating that sometimes, free software is worth a little effort.

I'm sorry, I don't think I understand you. Are you saying that all free software on the internet is malware-ridden?

That said, I need to update my initial response in order to make Bitt happier:
Originally Posted By: Archeon
eg. I like the files to look: "Assault On Precinct 13.avi", and not "assault on precinct 13.avi".

I just re-read your post. If you want to do exactly this, then the program I linked will rename your files just like you want them. However, if you want to capitalize the files correctly, then I'm afraid it appears that my application won't do it. THE Rename merely capitalizes every word, without consideration for words like and, the, on, etc. Sorry about that. Maybe one of those other applications will help you out.

Just so people know, MP3 Tag Studio do correct capitalization for MP3s.
Posted by: peter

Re: Looking for simple renaming program - 01/04/2008 14:27

Quote:
I'm sorry, I don't think I understand you. Are you saying that all free software on the internet is malware-ridden?

If he's saying that a sufficient proportion is, that it's reasonable to worry about downloading binaries, then I'd go along with him. (And I wouldn't restrict that worrying to "free" software "on the internet", either.)

Quote:
capitalizes every word, without consideration for words like and, the, on, etc.

Unless the US list of not-capitalised words is written somewhere so definitively that changing it requires large majorities in both Houses of Congress and ratification by all the states, I'd recommend capitalising everything. Part of the reason you're capitalising in the first place is (presumably) to canonicalise the names and avoid ambiguity -- ambiguity which roars back again if there's any disagreement about the list of uncapitalised words.

Peter
Posted by: wfaulk

Re: Looking for simple renaming program - 01/04/2008 14:49

Originally Posted By: peter
Part of the reason you're capitalising in the first place is (presumably) to canonicalise the names and avoid ambiguity

Not that I'm the one wanting to capitalize in this case, but I capitalize my files so that they look right, not to disambiguate.

Originally Posted By: peter
ambiguity which roars back again if there's any disagreement about the list of uncapitalised words.

Case-insensitive compares are easy.
Posted by: tanstaafl.

Re: Looking for simple renaming program - 01/04/2008 15:56

Quote:
Not that I'm the one wanting to capitalize in this case, but I capitalize my files so that they look right, not to disambiguate.


Disambiguate! Wonderful use of language. Bitt, you never fail to impress and amaze me.

tanstaafl.
Posted by: tfabris

Re: Looking for simple renaming program - 01/04/2008 15:59

As impressive as Bitt's command of the English language is, actually the term is in common usage among programmers, so it's not unusual to see it in a discussion about programming code.
Posted by: mlord

Re: Looking for simple renaming program - 01/04/2008 16:06

Originally Posted By: peter
Unless the US list of not-capitalised words is written somewhere so definitively that changing it requires large majorities in both Houses of Congress and ratification by all the states, I'd recommend capitalising everything. Part of the reason you're capitalising in the first place is (presumably) to canonicalise the names and avoid ambiguity -- ambiguity which roars back again if there's any disagreement about the list of uncapitalised words.

Agreed.

But just in case, here's the deluxe model, which leaves 1,2,3 letter file extensions alone, as well having an internal list of other words to not touch (eg. the, of, and ..)

This is now a standalone gawk script, rather than being wrapped in a shell script as before, and can now handle (leaving alone) leading directories in the names:
Code:
#!/usr/bin/gawk -f
function rename_file(path, old_name  ,space,dquote,backslash,nlw,lw,new_name,i,in_word,w,tail,c,rename)
{
        space = " "             ## a space character
        dquote = "\""           ## a double-quote character
        backslash = "\\"        ## a backslash character
        max_extension = 4       ## length of .xxx file extensions to not touch

        ## Words to not captitalize:
        lowercase_words = "the of and a is flac"

        nlw = split(lowercase_words,lw," ");

        ## Insert backslashes in front of any " characters inside the file name,
        ## so that they are preserved when we later invoke system() on it:
        gsub(dquote, backslash dquote, old_name)

        ## Copy old_name to new_name, and capitalize first letters of all words:
        in_word = 0
        new_name = ""
        for (i = 1; i <= length(old_name); ++i) {
                tail = substr(old_name,i)

                ## Leave 1/2/3 character file extensions untouched:
                if (length(tail) <= max_extension && tail ~ "^[.][a-zA-Z0-9]*$") {
                        new_name = new_name tail
                        break;
                }
                c = substr(tail,1,1)

                ## Leave "lowercase_words" untouched:
                for (w = 1; w <= nlw; ++w) {
                        if (tail" " ~ "^" lw[w] "[^a-zA-Z]") {
                                in_word = 1;
                                break;
                        }
                }

                ## Capitalize anything else:
                if (c !~ "[a-zA-Z]") {
                        in_word = 0
                } else if (in_word == 0) {
                        in_word = 1
                        c = toupper(c)
                }
                new_name = new_name c
        }

        ## If new_name differs from old_name, then rename the file:
        if (new_name != old_name) {
                ## Issue a command to rename the file:
                rename = "/bin/mv"
                print rename space dquote path old_name dquote space dquote path new_name dquote
                system(rename space dquote path old_name dquote space dquote path new_name dquote)
        }
}

BEGIN {
        ## Command line arguments are files to rename
        for (a = 1; a < ARGC; ++a) {
                path = ARGV[a]
                ## Split path into directories + filename:
                if (1 == match(path,"^(.*/)([^/]*$)",p))
                        rename_file(p[1],p[2])
                else
                        rename_file("",path)
        }
        exit(0);
}


Posted by: wfaulk

Re: Looking for simple renaming program - 01/04/2008 16:29

Actually, my usage of "disambiguate" comes from Wikipedia.
Posted by: Roger

Re: Looking for simple renaming program - 02/04/2008 08:38

Originally Posted By: tfabris
As impressive as Bitt's command of the English language is, actually the term is in common usage among programmers, so it's not unusual to see it in a discussion about programming code.


I remember using the word when we were writing Rio Music Manager (in the context of mapping SCSI LUNs to drive letters, which turned out to be quite hard with the ripping library we opted for). ISTR that RobV went "Huh? Oh. Cool word." smile