in reply to Re: Renaming Files
in thread Renaming Files

Thanks, but it's not quite what I'm looking for. I need a script that would start at the parent directory and recursively go into each subdirectory. While there, it would use the directory name (but not the full path) as part of the file name. It's a little more tricky than just renaming a couple files.

Replies are listed 'Best First'.
Re: Re: Re: Renaming Files
by blakem (Monsignor) on Aug 23, 2001 at 04:07 UTC
    Warning, untested code...

    how about something like:

    my $dir = '/my/mp3/directory'; opendir(DIR,$dir) or die "Cannot open $dir: $!"; while(defined($subdir = readdir(DIR))) { next if $subdir =~ /^\./; next unless -d "$dir/$subdir"; opendir(DIR2,"$dir/$subdir") or die "Cannot open $dir/subdir: $!"; while (defined($file = readdir(DIR2))) { next unless $file =~ /\.mp3$/i; my $src = "$dir/$subdir/$file"; my $dest = "$dir/$subdir/${subdir}_$file"; print "renaming $src => $dest\n"; # rename $src, $dest; } }

    -Blake

      Dude, your code rocks! With some quick mods, I also changed all TXT DOC and JPG files in the same directories. Thanks!