Quantcast
Channel: Chilkat Forum - latest questions
Viewing all articles
Browse latest Browse all 1061

How to change the names of the contents of the file using perl?

$
0
0

I treid the below code to change the directory names as per matching with text file.Ho can i do same process for contents of the file using perl?

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;
}

Viewing all articles
Browse latest Browse all 1061

Trending Articles