Perl Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jikes00
    New Member
    • Sep 2007
    • 4

    Perl Question

    Hi,

    I am new to perl scripting..I was wondering if someone can help me with this.
    I have files coming to one of the directories. Filenames are same except the extension. I want to move files if prefix is same but extension is different.
    Can this be accomplished in perl?

    Thanks,
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    It sounds possible. How do you know when new files are coming to the directory?

    Comment

    • jikes00
      New Member
      • Sep 2007
      • 4

      #3
      Hi Kevin,

      The requirement is to look for two files with same prefix but different file extension. We can receive number of pairs per transfer.
      I have written some code. But the issue I am having is that when I push the key into array before I move them, I am able to send only the file without the .abc extension.

      What I want to accomplish is that after checking that I have two files (file1 and file1.abc) I want to move these into different directory.

      If either of the pair (file1 and file1.abc) is missing I want files to be in the same directory.

      Thanks

      [code=perl]
      for my $file1 (@pair) {
      $file1 =~ s#.*\/##;
      }

      while (defined ($file1 = shift(@pair))) {
      my $file2 = $file1;
      $file2 =~ s#.*\/##;
      my $file2_ext = $file2;
      $file2_ext =~ s/.*\.//; # extension
      if ($file2_ext eq "abc") {
      # control file
      print " THIS IS A abc FILE : $file2_ext \n";

      $file2 =~ s/\.abc$//; # strip ".abc" off file2 for hash key
      }
      else {
      print " THIS IS NOT A abc FILE : $file2_ext \n";
      }
      $pairlist{$file 2}{filepairs}++ ;
      }

      for my $key (sort keys %pairlist){
      if ($pairlist{$key }{'filepairs'} == 2) {

      print " PAIR FOUND -- $key ******* \n";
      push (@pairmv,$key);

      }else{
      print " NO PAIRS FOUND -- $key ***** \n";
      }
      }
      [/code]
      Last edited by numberwhun; Sep 28 '07, 03:15 AM. Reason: add code tags

      Comment

      • jikes00
        New Member
        • Sep 2007
        • 4

        #4
        Originally posted by jikes00
        Hi Kevin,

        The requirement is to look for two files with same prefix but different file extension. We can receive number of pairs per transfer.
        I have written some code. But the issue I am having is that when I push the key into array before I move them, I am able to send only the file without the .abc extension.

        What I want to accomplish is that after checking that I have two files (file1 and file1.abc) I want to move these into different directory.

        If either of the pair (file1 and file1.abc) is missing I want files to be in the same directory.

        Thanks

        [code=perl]
        for my $file1 (@pair) {
        $file1 =~ s#.*\/##;
        }

        while (defined ($file1 = shift(@pair))) {
        my $file2 = $file1;
        $file2 =~ s#.*\/##;
        my $file2_ext = $file2;
        $file2_ext =~ s/.*\.//; # extension
        if ($file2_ext eq "abc") {
        # control file
        print " THIS IS A abc FILE : $file2_ext \n";

        $file2 =~ s/\.abc$//; # strip ".abc" off file2 for hash key
        }
        else {
        print " THIS IS NOT A abc FILE : $file2_ext \n";
        }
        $pairlist{$file 2}{filepairs}++ ;
        }

        for my $key (sort keys %pairlist){
        if ($pairlist{$key }{'filepairs'} == 2) {

        print " PAIR FOUND -- $key ******* \n";
        push (@pairmv,$key);

        }else{
        print " NO PAIRS FOUND -- $key ***** \n";
        }
        }
        [/code]

        I am able to accomplish this by checking for existance of file1 in while loop "if" condition.

        [code=perl]
        my $firstfile = substr($file2,0 ,length($file2) - 4);
        if ( -e "$firstfile "){
        [/code]

        Thank you!
        Last edited by numberwhun; Oct 8 '07, 04:03 PM. Reason: add code tags

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          Originally posted by jikes00
          I am able to accomplish this by checking for existance of file1 in while loop "if" condition.

          [code=perl]
          my $firstfile = substr($file2,0 ,length($file2) - 4);
          if ( -e "$firstfile "){
          [/code]

          Thank you!
          You have posted code into the forum without using proper code tags. It is best practice here on TSDN to wrap all code posted into the forum in code tags.

          Code tags start with [code] and end with [/code]. If you need an example other than this one, the please refer to the REPLY GUIDELINES (or POSTING GUIDELINES if you are starting a discussion) to the right of the Message window. You will find the examples and suggestions for posting there.

          In addition, you can also add the language to your code tags. Here is an example:

          [code=pe rl]
          <some code>
          &#91;/code]

          Please know that I have fixed your posts above to include the proper code tags. Please be sure and use code tags in all of your future posts here on TSDN.

          - Moderator

          Comment

          • jikes00
            New Member
            • Sep 2007
            • 4

            #6
            Sorry about that!
            I will add code tags next time onwards. Thanks,

            Comment

            Working...