in reply to Mass File Renaming
#!/usr/bin/perl #usage: zrn 'oldext' 'newext' 'topdir' #include the the . for extensions, otherwise anystring will change in +the filenames #if you want to append .bak to files, use $ for 'oldext' ex: zrn $ .ba +k . #the ext is always applied to the end of the filename #to strip extensions: zrn .bak '' . use strict; use File::Find; my ($in,$out,$path) = @ARGV; my $timestamp = localtime(); $timestamp =~ s/ /|/g; if (@ARGV <2){die "Usage zrn 'oldext' 'newext' 'top directory' -> '.' +is good examples: zrn .bak .old . zrn \$ .bak . zrn .bak '' .\n"}; finddepth (\&wanted,$path); sub wanted { my $old = $_; return unless -f; #-d for dir ops or comment out for both $_ =~ s/$in$/$out/; #do ops here if ($_ eq $old){return}; if (-e $_) {$_ = $_.$timestamp; warn("timestamping $_ to avoid overwri +ting existing file")}; rename $old, $_ or warn "Cannot rename $old to $_ in $File::Find::dir: + $!"; };
|
|---|