rename the specific file with current time stamp using perl script for windows -
i using below script rename renaming file end sta.i need rename file starts krat or trat.
#!/usr/local/bin/perl use strict; use warnings; use file::copy; $directory = 'c:\users\desktop'; chdir($directory) or die "can't chdir $directory $!"; opendir(dir, $directory) || die "couldn't opendir: $!\n"; @files = grep { $_ ne '.' && $_ ne '..' } readdir dir; foreach(@files) { print $_,"\n"; $newname = $_; $newname =~ s/sta$/t00/g; print "renaming: $_ -> $newname \n"; rename($_, $newname); }
some minor changes script.
use strict; use warnings; use file::copy; $directory = 'c:\users\rajkunal_aps\desktop'; chdir($directory) or die "can't chdir $directory $!"; opendir(dir, $directory) || die "couldn't opendir: $!\n"; @files = grep { $_ ne '.' && $_ ne '..' } readdir dir; foreach(@files) { print $_,"\n"; $newname = $_; if ($newname =~ /^krat/ || $newname =~ /^trat/ ) { $newname =~ s/sta/t00/g; print "renamed: $newname $_ \n"; rename($_, $newname); } }
result
trat.aud.sta -> trat.aud.t00 stat.aud.sta -> no changes krat.eud.sta -> krat.eud.t00
explanation:
the problem script searching file ends sta
, rename t00
. instead should search files match.
so this, search see if beginning of line matches either krat
or trat
using /^krat/
, /^trat/
in if statement. if find matches, search , replace of sta
t00
using s/sta/t00//g;
Comments
Post a Comment