Loading ...
Sorry, an error occurred while loading the content.

Regex a filename

Expand Messages
  • Jshynes40@aol.com
    Hello, I have a folder on a network drive that has a couple of hundred files in it, here are some examples of filenames. This is on a WIN2000 machine, using
    Message 1 of 2 , Aug 31, 1945
    View Source
    • 0 Attachment
      Hello,
      I have a folder on a network drive that has a couple of hundred files in it, here are some examples of filenames.
      This is on a WIN2000 machine, using ActivePerl.

      123002-1053.|.N0212270.000
      122702-1417.|.N
      122702-1354.|.LEXIS000.000

      I use another program (not perl) that generates a text file with just this filename N (no extension, just the letter N, always captialized). I need to check for this filename and print it out to a report. Here's my logic:

      sub cost {
      my $costcount = 0;
      chdir "L:/time" or die "CANNOT CHANGE TO NETWORK DRIVE Eliteap2, FILE FOLDER NAMED 'time' ";
      open OUT, ">>timeout.txt" or die "Can't open timeout file for output: $!";
      print OUT "\n";
      print OUT "\t\t\tEQUITRACS COST DIRECTORY\n";
      print OUT "\n";
      print OUT "FILENAME\tFILE SIZE\n";
      chdir "L:/cost" or die "CANNOT CHANGE TO NETWORK DRIVE Eliteap2, FILE FOLDER NAMED 'cost' ";
      print "PROCESSING........COSTS\n";
      @file_name2 = glob "*.*";
      $name2 = @file_name2;
      foreach $name2(@file_name2) {
      if ($name2 =~ /^N\d{7}\.\d{3}/) {
      my $file_size = (-s $name2);
      print OUT "$name2";
      print OUT "\t$file_size\n";
      $costcount++;
      } elsif ($name2 =~ /^LEXIS000\.000/) {
      my $file_size1 = (-s $name2);
      print OUT "$name2";
      print OUT "\t$file_size1\n";
      $costcount++;
      } elsif ($name2 =~ /^N/) {
      my $file_size2 = (-s $name2);
      print OUT "$name2";
      print OUT "\t$file_size2\n";
      $costcount++;
      }

      }
      The third 'if' statement is not working, the other two work just fine. So, how do I regex on a single letter? or is regex the appropriate logic?

      Thanks.
    • Damien Carbery
      ... files in it, here are some examples of filenames. ... just this filename N (no extension, just the letter N, always captialized). I need to check for this
      Message 2 of 2 , Aug 31, 1945
      View Source
      • 0 Attachment
        --- In perl-beginner@yahoogroups.com, Jshynes40@a... wrote:
        > Hello,
        > I have a folder on a network drive that has a couple of hundred
        files in it, here are some examples of filenames.
        > This is on a WIN2000 machine, using ActivePerl.
        >
        > 123002-1053.|.N0212270.000
        > 122702-1417.|.N
        > 122702-1354.|.LEXIS000.000
        >
        > I use another program (not perl) that generates a text file with
        just this filename N (no extension, just the letter N, always
        captialized). I need to check for this filename and print it out to a
        report. Here's my logic:
        >
        > sub cost {
        > my $costcount = 0;
        > chdir "L:/time" or die "CANNOT CHANGE TO NETWORK DRIVE Eliteap2,
        FILE FOLDER NAMED 'time' ";
        > open OUT, ">>timeout.txt" or die "Can't open timeout file for
        output: $!";
        > print OUT "\n";
        > print OUT "\t\t\tEQUITRACS COST DIRECTORY\n";
        > print OUT "\n";
        > print OUT "FILENAME\tFILE SIZE\n";
        > chdir "L:/cost" or die "CANNOT CHANGE TO NETWORK DRIVE Eliteap2,
        FILE FOLDER NAMED 'cost' ";
        > print "PROCESSING........COSTS\n";
        > @file_name2 = glob "*.*";
        > $name2 = @file_name2;
        > foreach $name2(@file_name2) {
        > if ($name2 =~ /^N\d{7}\.\d{3}/) {
        > my $file_size = (-s $name2);
        > print OUT "$name2";
        > print OUT "\t$file_size\n";
        > $costcount++;
        > } elsif ($name2 =~ /^LEXIS000\.000/) {
        > my $file_size1 = (-s $name2);
        > print OUT "$name2";
        > print OUT "\t$file_size1\n";
        > $costcount++;
        > } elsif ($name2 =~ /^N/) {
        > my $file_size2 = (-s $name2);
        > print OUT "$name2";
        > print OUT "\t$file_size2\n";
        > $costcount++;
        > }
        >
        > }
        > The third 'if' statement is not working, the other two work just
        fine. So, how do I regex on a single letter? or is regex the
        appropriate logic?
        >
        > Thanks.

        > } elsif ($name2 =~ /^N/) {
        You need to change it to:
        } elsif ( $name2 =~ /^N$/ ) {
        where '$' means end of string, similar to '^' meaning start of string.
      Your message has been successfully submitted and would be delivered to recipients shortly.