finddata has asked for the wisdom of the Perl Monks concerning the following question:
Code which i used to rename folders and sub folders as per text file:PROJECT:DCMS_DEMO:prj1 BLOCK:de_top:blk1 BLOCK:new_block2:blk2 BLOCK:test:blk3 CHECKLIST:Block_DV:checklist1 CHECKLIST:Block_Physical_design_checklist:checklist2 CHECKLIST:CAD_checklist:checklist3 CHECKLIST:DFT:checklist4
My output obtained from above code:##READ THE TEXT FILE AND RENAME THE DIRECTORIES #### my $dir_map = read_map($mapfile); my $top_dir=$output_dir; rename_dirs( $top_dir, $dir_map ); sub rename_dirs { my ( $top_dir, $dir_map ) = @_; opendir (my $dh, $top_dir) or die "Can't open $top_dir: $!"; my $save_dir = getcwd(); chdir $top_dir; while (my $dir = readdir $dh) { #print $dir,"\n"; next if ($dir eq '.') or ($dir eq '..'); if ( exists $dir_map->{$dir} ) { my $new_name = $dir_map->{$dir}; File::Copy::move( $dir, $new_name ) or die "Could not rename '$dir' as '$new_name': $!"; $dir = $new_name; } rename_dirs( $dir, $dir_map ) if -d $dir; } chdir $save_dir; } ##SUB ROUTINE TO READ THE TEXT FILE FROM COMMAND LINE ARGUMENTS## sub read_map { my ( $fn ) = @_; my %dir_map; open( my $fh, '<', $fn ) or die "Could not open file '$fn': $!"; while( my $line = <$fh> ) { chomp $line; my @fields = split /:/, $line; if ( @fields == 3 ) { $dir_map{$fields[2]} = $fields[1]; } } close $fh; return \%dir_map; }
My query explanation: Likewise i should do for all .config file contents inside the file.And i should not consider the last level directory structure for doing the same process?(i.e rev.*) cat DCMS_DEMO.configDCMS_DEMO |-- DCMS_DEMO.config |-- DCMS_DEMO.html |-- de_top | |-- Block_DV | | |-- Block_DV.config | | |-- Block_DV.html | | |-- rev1 | | | |-- rev1.config | | | `-- rev1.html | | `-- rev2 | | |-- rev2.config | | `-- rev2.html | |-- CAD_checklist | | |-- CAD_checklist.config | | |-- CAD_checklist.html | | |-- rev1 | | | |-- rev1.config | | | `-- rev1.html
Expected output:blk3 : 0% : 0% blk1 : 0.68% : 0.99% blk2 : 0.00% : 0.00% OVERALL_STATUS=0.23% PARTIAL_STATUS=0.33%
test : 0% : 0% de_top : 0.68% : 0.99% new_block2 : 0.00% : 0.00% OVERALL_STATUS=0.23% PARTIAL_STATUS=0.33%
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to match the file and rename the contents in the .config files of directories and subdirectories using perl?
by Marshall (Canon) on Mar 12, 2017 at 19:48 UTC | |
by finddata (Sexton) on Mar 13, 2017 at 04:05 UTC | |
by finddata (Sexton) on Mar 13, 2017 at 04:19 UTC | |
by Marshall (Canon) on Mar 14, 2017 at 22:06 UTC |