Unoffical empeg BBS

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

Topic Options
#317228 - 10/12/2008 20:30 File uppercase renamer utility/script
drakino
carpal tunnel

Registered: 08/06/1999
Posts: 7868
I'm looking for a quick way to rename a bunch of files and folders all to uppercase. Methods acceptable are DOS command line utilities, or something that will run from Cygwin. Ideally, scriptable, and will recurse subdirectories automatically. So far, the method I am using is really slow for the ~7500 files I need to do this to.
Code:
for i in `find * -depth`; do (mv $i `echo $i | sed 's%[^/][^/]*$%%'``echo $i | sed 's!.*/!!' | tr [:lower:] [:upper:]`); done

That is currently taking I think around 45 minutes to an hour to do what I need.

Thanks in advance

Top
#317229 - 10/12/2008 20:44 Re: File uppercase renamer utility/script [Re: drakino]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31600
Loc: Seattle, WA
Can't help you with the script right now, but I'm deeply curious and fascinated as to why this would be a necessary task. I'm sure the story is hilarious.
_________________________
Tony Fabris

Top
#317230 - 10/12/2008 21:27 Re: File uppercase renamer utility/script [Re: drakino]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14496
Loc: Canada
This is the script I use. Feed it a list of pathnames (files or directories) as args.

Code:
#!/bin/bash
#
# Convert names from current or specified directory(s) to all uppercase:
#
ls -1 "$@" | awk -v Q="'" '
{
        if (toupper($0) != $0) {
                system("/bin/mv " Q $0 Q" "Q toupper($0) Q);
        }
}'


To convert an entire directory tree, use it like this:

## Assumes above script is saved as "uppercase" somewhere in $PATH
cd bigdirtree
find . -type d | xargs uppercase


Edited by mlord (10/12/2008 21:30)

Top
#317231 - 10/12/2008 21:31 Re: File uppercase renamer utility/script [Re: drakino]
oliver
addict

Registered: 02/04/2002
Posts: 691
Something like this has come up in the past, the request was to turn everything into first letter uppercase for mp3 files.

I was bored, and wrote a simple .net module, looks like you've have to change line 41 to VbStrConv.Uppercase, recompile, and it should work.



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

Top
#317233 - 10/12/2008 21:45 Re: File uppercase renamer utility/script [Re: mlord]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
That's likely to be just as slow as Tom's current solution, which is probably slow because of the spawning of 7500 instances of "mv". Yours might actually be slower, because awk's system() probably ends up executing via "sh -c".
_________________________
Bitt Faulk

Top
#317234 - 10/12/2008 22:08 Re: File uppercase renamer utility/script [Re: drakino]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
This should be fairly fast:

find . -type f | upper.pl

Where upper.pl is:
Code:
#!/usr/bin/perl

while (<>) {
        chomp();
        my $oldname = $_;
        my $newname = $oldname;
        $newname =~ s#^(.+)/([^/]+)$#$1/\U$2\E#;
        rename($oldname, $newname) if ($oldname ne $newname);
}


If you also want to change directories, run it again with:

find . -depth -type d | upper.pl

You could potentially do it in one step, but it would take me longer to write. Edit: Actually, I think you can do it all in one step with just "find . -depth | upper.pl".

Edge cases that fail include having filenames and pathnames with spaces in them, and providing filenames without any slashes in them. Thus, make sure to do "find ." and not "find *".

Also, I'd definitely test this out in a sandbox somewhere. I've done about 3 minutes of testing, if that.


Edited by wfaulk (10/12/2008 22:15)
_________________________
Bitt Faulk

Top
#317235 - 10/12/2008 22:12 Re: File uppercase renamer utility/script [Re: drakino]
tanstaafl.
carpal tunnel

Registered: 08/07/1999
Posts: 5549
Loc: Ajijic, Mexico
Quote:
I'm looking for a quick way to rename a bunch of files and folders all to uppercase.


This is totally off the wall, but I believe that MP3 Tag Studio would do what you want.

In "Browse/edit tags", choose the "Show all filetypes" box.

In "Direct rename files",
(1) choose advanced mode,
(2) and in General settings choose "Case fix and replace".
(3) Set "Expected file name format" to "<Title>",
(4) and "Output file name format" to "<Title>"
(5) in "Setup", set "Capitalization mode" to "Full uppercase".
(6) Check the "Include subdirs" box

Go as far up the directory tree as you need to go (Root?) and click Execute.

OK, I just tested this on a temporary directory, 19 folders and subfolders, 525 files, about 3.5 GB. It worked, it took 0.8 seconds to run. That's the good news. The bad news is that it only changes the filenames, and not the directory names. But maybe you can modify your script to do just the directories after the filenames are changed?

tanstaafl.







_________________________
"There Ain't No Such Thing As A Free Lunch"

Top
#317238 - 10/12/2008 23:31 Re: File uppercase renamer utility/script [Re: drakino]
Mojo
Unregistered


This took 46.5 seconds on 1140 files. Estimated time to rename 7500 files would be ~5 minutes. This is on a 2.16 Ghz Intel Core2Duo.

Code:
proc rename {file} {
	if { [file isdirectory $file] } {
		foreach nextFile [glob -nocomplain "$file/*"] {
			rename $nextFile
		}
	}
	file rename -force -- $file this_is_a_hack
	file rename -force -- this_is_a_hack "[file dirname $file]/[string toupper [file tail $file]]"
	return
}


On Windows, download ActiveState Tcl if you don't already have it and paste the rename procedure in tclsh.exe. Then do:
Code:
rename name_of_parent_folder


The "this_is_a_hack" portion of the code is because of a silly bug in Tcl that doesn't let you rename a file to itself with different capitalization on Windows. It makes it take twice as long, but it still beats 45 minutes.

Top