# perl script to create a script to rename files # input: a series of file names, with parameters for renaming. # # updated: 8/28/2017 $debug = 0; $point = ''; @standard_suffixes = qw/Na Nb Ea Eb Sa Sb Wa Wb/; print STDERR "standard suffixes are: ", join " ", @standard_suffixes; #while () { while (<>) { $debug && print STDERR "line $.: ", $_; chomp; /^#|^$/ && next; if ( /^%(\S+)/ ) { $point = $1; $standard_naming = 0; if ( /\s+STANDARD/i ) { $standard_naming = 1; $suffix_index = 0; } next; } $before = $suffix = ''; if ( $standard_naming ) { if ( /^(\S+)/ ) { $before = $1; if ($suffix_index >= 0+@standard_suffixes) { die "line $. : too many files for standard naming, point='$point'\n"; } $suffix = $standard_suffixes[$suffix_index++]; $before =~ s/^\s+//; $before =~ s/\s+$//; $suffix =~ s/^\s+//; $suffix =~ s/\s+$//; if ($suffix eq '') { print STDERR "ignoring line $.: $_\n"; next; } } } else { if ( /^(.*)\t(.*)/ ) { $before = $1; $suffix = $2; $before =~ s/^\s+//; $before =~ s/\s+$//; $suffix =~ s/^\s+//; $suffix =~ s/\s+$//; if ($suffix eq '') { print STDERR "ignoring line $.: $_\n"; next; } } } if (length($before)>0 ) { $dot_suffix = ''; if ( $before =~ /\.\w+$/ ) { #preserve the file extension, e.g. .JPG $dot_suffix= $&; } $after = $point . $suffix . $dot_suffix; $cmd = 'system("ren", "%1", "%2"); die $! if $?;'; $cmd =~ s/%1/$before/; $cmd =~ s/%2/$after/; print $cmd, "\n"; } else { print STDERR "unexpected line $.: $_\n"; } } __DATA__ %JCElk1977_ IMGP1370.JPG Na IMGP1371.JPG Nb IMGP1372.JPG Ea IMGP1373.JPG Eb IMGP1374.JPG Sa IMGP1375.JPG Sb IMGP1376.JPG Wa IMGP1377.JPG Wb # new example for 2017: %TMSEHC75_ standard IMGP1483.JPG IMGP1484.JPG IMGP1485.JPG IMGP1486.JPG IMGP1487.JPG IMGP1488.JPG IMGP1489.JPG IMGP1490.JPG