Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with accessing all files in directory 1

Status
Not open for further replies.

rajkumar87

Technical User
Nov 28, 2011
2
0
0
SE
<p>
I have written a program which compares two text files namely female2.txt and icd-10-codes. icd-10-codes text contains values in tab delimited format. if there is match between female2 and array 0 of icd-10-code.txt then female2.txt should be replaced with array 1 values from icd-code.txt. I have done some code to accomplish this. Instead of comparing just female2.txt i wanted it to perform in all the files in directory and its subdirectory. I have used use::file to do that but it is not accessing the sub directory. I have tried to figure out but i could not . please anyone help me to solve this program.
</p>
Code:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper; $Data::Dumper::Useqq = 1;
use File::Find;

find(\&wanted,'D:\coding\patientperclinicandvisit_1days_train');

sub wanted
{
my $icd_path = 'D:\coding\icd-10-codes.txt';
my $in_path =  <*>;
my $out_path = 'D:\coding\female1_corrected.txt';


open my $icd_fh, '<', $icd_path
    or die "Cannot open '$icd_path': $!";

#print "Reading '$icd_path'\n";
my %lookup_icd;
while ( my $line = <$icd_fh> ) {
    chomp $line;
    my ( $lookup_code, $icd_code_and_text ) = split /\t/, $line;

#    print Dumper $lookup_code, $icd_code_and_text;

    if ( exists $lookup_icd{$lookup_code} ) {
        warn "Replacing $lookup_code;"
           . " was '$lookup_icd{$lookup_code}',"
           . " now '$icd_code_and_text'\n";
    }
    $lookup_icd{$lookup_code} = $icd_code_and_text;
}
close $icd_fh;


#print Dumper \%lookup_icd;


#print "Reading '$in_path'\n";
#print "Writing '$out_path'\n";
open my $in_fh, '<', $in_path
    or die "Cannot open '$in_path': $!";
open my $out_fh, '>', $out_path
    or die "Cannot open '$out_path': $!";

while ( my $line = <$in_fh> ) {
    chomp $line;

    my @cols = split / /, $line;

    for my $possible_icd (@cols) {
        my $replacement_icd = $lookup_icd{$possible_icd};
        if ($replacement_icd) {
            $possible_icd = $replacement_icd;
        }
    }

    print {$out_fh} join( ' ', @cols ), "\n";

}
close $in_fh;
close $out_fh
    or warn "Cannot close 'out_path': $!\nSome data may not have been written.";
unlink $in_path
    or die "Could not delete '$in_path': $!";

use File::Copy;
move( $out_path, $in_path )
    or die "Could not move file '$out_path' to '$in_path': $!";
}
 
Have you tried using 'glob' or if you prefer File::Glob ?


If I want to process all files in a directory I use this which does what I need...

Code:
 my @files = glob(MAIN_SERVER_PATH . '/MY_DIRECTORY/*');
    for(@files){
        if(-e $_){
            print "Found : $_\n";
        }
    }



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta
 
<p>I have made it work with use File::Find::Rule module. thanks for your suggestion</p>
 
glad you got it working.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top