# File::Spec::Win32 example # #!/usr/bin/perl use strict; use warnings; use File::Spec::Win32; my @txt = ( 'C:\Book\C0001-Chapter-001.mp3', ); my @re = ( [ qr{^(C\d+)}msx => '"NEW-$1"', ], [ qr{(-Ch)}msx => 'lc("$1")', ], ); foreach my $fn ( @text ) { print qq{Before: }, $fn, qq{\n}; # # @fp - file parts (volume, directory, filename) # @dl - directory path to $fn my @fp = File::Spec::Win32->splitpath( $fn ); my @dl = File::Spec::Win32->splitdir( $fp[1] ); # # Apply regexes to file name # (could also add logic to modify some part of the # directory path in @dl as well). foreach my $regex ( @re ) { $fp[3] =~ s/$regex->[0]/$regex->[1]/gee; } # # Rebuild file name $fp[1] = File::Spec::Win32->catdir( @dl ); my $ffn = File::Spec::Win32->catpath( @fp ); print qq{After: }, $ffn, qq{\n}; } # # Output: # # Before: C:\Book\C000-Chapter-001.mp3 # After: C:\Book\NEW-C000-chapter-001.mp3