• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 171
  • Last Modified:

Matching input from keyboard to a file

Ask for input from the keyboard, if it is matched to a file in a particular directory, exit. If is does not match, looping for the input from the keyboard until it is matched.
Thanks in advance!
Andy
0
AndyDo
Asked:
AndyDo
1 Solution
 
rj2Commented:
#!/usr/bin/perl
opendir(THISDIR,"e:/temp") or die "Can not open directory: $!";
@allfiles = grep !/^\.\.?$/, readdir(THISDIR);
closedir(THISDIR);
for(;;) {
     print "Enter filename: ";
     $file=<STDIN>;
     chomp($file);
     foreach(@allfiles) {          
          if(lc($_) eq lc($file)) { #remove lc if you want case sensitive match
               exit;
          }
     }    
}
0
 
blinkie23Commented:
rj2 is quicker than i am.  but here is my version.  i posted it because it allows the script to continue instead of just exiting.  give rj2 points, not me.

#!/usr/bin/perl

my $dirname = "/home/jon/bin";

opendir DIR,"$dirname" or die "$!";
my @files = grep /.*/, readdir DIR;

my $last = 0;
while($last != 1){
    print "filename? ";
    $_ = <STDIN>;
    chomp;
    foreach my $filetest (@files){
        if($filetest eq $_){ $last = 1; last; }
    }
}

print("matched $_\n");
0
 
AndyDoAuthor Commented:
Thanks for your help!!!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now