perl - How to traverse all the files in a directory and subdirectories as well -
i working on script read file , based on file content have rename file.
problem facing :
1st not able read file in subdirectory. 2nd trying rename file error occuring.
here code:
folder structur : logs / / / \ b c d / \ \ e file file
where a,b,c,d,e dir file,file txt file
#!/usr/bin/perl use strict; use warnings; use file::copy::recursive; use cwd; $file; (@files,@file_01); ($dir_01,$dir_02); $new_file="kernel.txt"; chdir('c:\\abd\\efg'); $dir_01 = getcwd; opendir(dir_01, $dir_01); @files=readdir(dir_01); close(dir_01); foreach $dir_02 (@files) { opendir (dir_02, "$dir_01"."/"."$dir_02") ; @file_01=readdir(dir_02); close(dir_02); foreach $file (@file_01) { open(fh,"<","$dir_01/$dir_02/$file") or die "can not open file $!\n"; while(my $lines = <fh>) { if($lines=~ m/linux kernel version/i || $lines=~ m/usb_state=disconnected/gi) { #print "\t$lines\n\n"; rename($file,$new_file) or die "can not change name"; } } } }
rename($file,$new_file)
will not work did not specify path
rename("$dir_01/$dir_02/$file", $new_file)
but file matches renamed kernel.txt
in current directory. expected behavior?
Comments
Post a Comment