On Sat, 20 Jan 2001 at 10:14pm (-0500), John Aldrich wrote: > Anyone have a script or util that'll rename files with > spaces in 'em such that spaces become underscores? It's a > real bear when you have several HUNDRED files.... :-) > John >
#!/usr/bin/perl # GLOBALS ################################################################## use strict; use Symbol; use File::Basename; use Getopt::Std; use vars qw/ $ARGV0 /; sub main (); sub show_help(); $ARGV0 = basename($0); main(); die "Unreachable code reached"; # MAIN BODY ################################################################ sub main () { my(%opts); my($dir, $file); my($p) = gensym; unless (getopts('hv', \%opts)) { show_help(); exit(1); } if ($opts{'h'}) { show_help(); exit(0); } unless ($#ARGV == 0) { show_help(); exit(1); } unless (open($p, "find '$ARGV[0]' | sort -r|")) { print STDERR "open|: find '$ARGV[0]' | sort -r: $!\n"; exit(1); } while (<$p>) { chomp(); $dir = dirname($_); $file = basename($_); next unless ($file =~ s/\s/_/g); unless (rename($_, "$dir/$file")) { print STDERR "rename: $_ -> $dir/$file: $!\n"; exit(1); } print "$_ -> $dir/$file\n" if ($opts{'v'}); } close($p); exit(0); } # SUB FUNCS ################################################################ sub show_help () { print STDOUT " usage: $ARGV0 [<opts>] <directory> -h show help. causes this message to be shown. -v verbose. show names as files are renamed. All entries under given diretory containing spaces in their named will be renamed using '_' characters instead. \n"; return(); }