si_lence has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow monks,

I have a bunch of files, that I would like to upcase the names.
So far I have a small script that does this:

use strict; use warnings; use File::Find; use File::Copy; my $scandir="path/to/files"; my $outpath="path/to/out/directory"; find(\&update, $scandir); sub update() { if (-f $_) { my $file = $_; $file = uc($file); move("$File::Find::name","$outpath/$file") } }

Is there a way to do that in a one-liner?
(the files need not to be copied to a different location, inplace is fine too)

Thanks
si_lence

Replies are listed 'Best First'.
Re: rename files to upcase
by davido (Cardinal) on Nov 16, 2004 at 08:13 UTC

    rename is a built-in function. Using it would eliminate the need for File::Copy.

    The following is definately untested. Wield at your own risk. ;)

    perl -MFile::Find -e "find( sub{ rename( $_, uc $_ ) if -f $_; }, $ARG +V[0] );" path/name

    Update: Remember, File::Find behaves recursively, unless you explicitly limit it. DO NOT turn this sort of script loose on a root directory, unless you want your entire filestructure to be uppercased.


    Dave

      you probably know this, but you can make this slightly more efficient if you add a guard against cases where $_ eq uc $_, saving a disk access. This isn't really important in a one-liner though.
Re: rename files to upcase
by eyepopslikeamosquito (Archbishop) on Nov 16, 2004 at 08:23 UTC
Re: rename files to upcase
by PodMaster (Abbot) on Nov 16, 2004 at 08:14 UTC
    perl -e"use strict; use warnings; use File::Find; use File::Copy;  my $scandir=shift; my $outpath=shift; find(\&update, $scandir); sub update{ if (-f $_) { my $file = $_; $file  = uc $file; move $File::Find::name,q($outpath/$file); } } "   path/to/files path/to/out/directory

    Now that's a good one-liner, although I'd probably just save it as 'upcasemove.pl' or some such :)

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: rename files to upcase
by Happy-the-monk (Canon) on Nov 16, 2004 at 08:39 UTC

    The problem being solved, I think of two more concerns:

    • There might be a speed issue or a style issue on making sure the files aren't already uppercase.
    • There probably is a security issue on certain os' about overwriting already existing files in all-uppercase.

    Cheers, Sören

Re: rename files to upcase
by TedPride (Priest) on Nov 16, 2004 at 12:51 UTC
    Just check to see if the uppercase version of the file name is in existence already (or the same as un-uppercase version), print a message if so, and rename the file if not. I wouldn't personally try doing this in one line.
    use strict; use warnings; my ($old, $new); my $dir = "/u/web/hom420/test/"; die "Directory $dir does not exist" unless -e $dir; die "Directory $dir could not be opened" unless opendir(DIR, $dir); while ($old = readdir(DIR)) { next if -d $dir.$old; next if ($new = uc $old) eq $old; if (-e $dir.$new) { print "Collision: $old -> $new\n"; next; } rename $dir.$old, $dir.$new; }
    NOTE: This is not a recursive solution. It also doesn't work on a case-insensitive OS - something like MacOS will need a more imaginative solution.