Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#308720 - 30/03/2008 16:36 Looking for simple renaming program
BartDG
carpal tunnel

Registered: 20/05/2001
Posts: 2616
Loc: Bruges, Belgium
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!
_________________________
Riocar 80gig S/N : 010101580 red
Riocar 80gig (010102106) - backup

Top
#308721 - 30/03/2008 16:47 Re: Looking for simple renaming program [Re: BartDG]
StigOE
addict

Registered: 27/10/2002
Posts: 568
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

Top
#308724 - 30/03/2008 21:58 Re: Looking for simple renaming program [Re: BartDG]
oliver
addict

Registered: 02/04/2002
Posts: 691
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...






Attachments
FileRenamer.zip (19 downloads)

_________________________
Oliver mk1 30gb: 129 | mk2a 30gb: 040104126

Top
#308726 - 31/03/2008 02:31 Re: Looking for simple renaming program [Re: oliver]
Dignan
carpal tunnel

Registered: 08/03/2000
Posts: 12318
Loc: Sterling, VA
Please give The Rename a try. I swear by the program and have used it on a very regular basis for about 6 years.
_________________________
Matt

Top
#308729 - 31/03/2008 13:31 Re: Looking for simple renaming program [Re: BartDG]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14484
Loc: Canada
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) "\"")}'



Edited by mlord (31/03/2008 13:32)

Top
#308730 - 31/03/2008 13:49 Re: Looking for simple renaming program [Re: mlord]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14484
Loc: Canada
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

Top
#308761 - 01/04/2008 08:00 Re: Looking for simple renaming program [Re: mlord]
BartDG
carpal tunnel

Registered: 20/05/2001
Posts: 2616
Loc: Bruges, Belgium
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!
_________________________
Riocar 80gig S/N : 010101580 red
Riocar 80gig (010102106) - backup

Top
#308762 - 01/04/2008 09:56 Re: Looking for simple renaming program [Re: mlord]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14484
Loc: Canada
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)
                }
        }'

Top
#308773 - 01/04/2008 12:33 Re: Looking for simple renaming program [Re: mlord]
peter
carpal tunnel

Registered: 13/07/2000
Posts: 4174
Loc: Cambridge, England
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

Top
#308774 - 01/04/2008 13:09 Re: Looking for simple renaming program [Re: peter]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14484
Loc: Canada
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.


Edited by mlord (01/04/2008 13:35)

Top
#308775 - 01/04/2008 13:10 Re: Looking for simple renaming program [Re: peter]
tonyc
carpal tunnel

Registered: 27/06/1999
Posts: 7058
Loc: Pittsburgh, PA
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.
_________________________
- Tony C
my empeg stuff

Top
#308776 - 01/04/2008 13:11 Re: Looking for simple renaming program [Re: mlord]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
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.
_________________________
Bitt Faulk

Top
#308777 - 01/04/2008 13:13 Re: Looking for simple renaming program [Re: wfaulk]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14484
Loc: Canada
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

Top
#308778 - 01/04/2008 13:22 Re: Looking for simple renaming program [Re: tonyc]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14484
Loc: Canada
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!


Edited by mlord (01/04/2008 13:22)

Top
#308780 - 01/04/2008 13:29 Re: Looking for simple renaming program [Re: mlord]
tonyc
carpal tunnel

Registered: 27/06/1999
Posts: 7058
Loc: Pittsburgh, PA
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
_________________________
- Tony C
my empeg stuff

Top
#308781 - 01/04/2008 13:31 Re: Looking for simple renaming program [Re: tonyc]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14484
Loc: Canada
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).


Edited by mlord (01/04/2008 13:36)

Top
#308782 - 01/04/2008 13:36 Re: Looking for simple renaming program [Re: mlord]
tonyc
carpal tunnel

Registered: 27/06/1999
Posts: 7058
Loc: Pittsburgh, PA
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.
_________________________
- Tony C
my empeg stuff

Top
#308783 - 01/04/2008 13:37 Re: Looking for simple renaming program [Re: tonyc]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14484
Loc: Canada
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

Top
#308784 - 01/04/2008 14:07 Re: Looking for simple renaming program [Re: mlord]
Dignan
carpal tunnel

Registered: 08/03/2000
Posts: 12318
Loc: Sterling, VA
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.
_________________________
Matt

Top
#308785 - 01/04/2008 14:27 Re: Looking for simple renaming program [Re: Dignan]
peter
carpal tunnel

Registered: 13/07/2000
Posts: 4174
Loc: Cambridge, England
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

Top
#308787 - 01/04/2008 14:49 Re: Looking for simple renaming program [Re: peter]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
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.
_________________________
Bitt Faulk

Top
#308792 - 01/04/2008 15:56 Re: Looking for simple renaming program [Re: wfaulk]
tanstaafl.
carpal tunnel

Registered: 08/07/1999
Posts: 5543
Loc: Ajijic, Mexico
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.
_________________________
"There Ain't No Such Thing As A Free Lunch"

Top
#308793 - 01/04/2008 15:59 Re: Looking for simple renaming program [Re: tanstaafl.]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31578
Loc: Seattle, WA
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.
_________________________
Tony Fabris

Top
#308794 - 01/04/2008 16:06 Re: Looking for simple renaming program [Re: peter]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14484
Loc: Canada
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);
}



Top
#308795 - 01/04/2008 16:29 Re: Looking for simple renaming program [Re: tanstaafl.]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
Actually, my usage of "disambiguate" comes from Wikipedia.
_________________________
Bitt Faulk

Top
#308810 - 02/04/2008 08:38 Re: Looking for simple renaming program [Re: tfabris]
Roger
carpal tunnel

Registered: 18/01/2000
Posts: 5682
Loc: London, UK
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
_________________________
-- roger

Top