469,289 Members | 2,156 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,289 developers. It's quick & easy.

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,
Sep 27 '07 #1
5 886
KevinADC
4,059 Expert 2GB
It sounds possible. How do you know when new files are coming to the directory?
Sep 27 '07 #2
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

Expand|Select|Wrap|Line Numbers
  1. for my $file1 (@pair) {
  2.                 $file1 =~ s#.*\/##;
  3.                 }
  4.  
  5.                 while (defined ($file1 = shift(@pair))) {
  6.                 my $file2 = $file1;
  7.                 $file2 =~ s#.*\/##;
  8.                 my $file2_ext = $file2;
  9.                 $file2_ext =~ s/.*\.//;         # extension
  10.                         if ($file2_ext eq "abc") {
  11.                         # control file
  12.                         print " THIS IS A abc FILE : $file2_ext \n";
  13.  
  14.                         $file2 =~ s/\.abc$//;           # strip ".abc" off file2 for hash key
  15.                         }
  16.                         else {
  17.                  print " THIS IS NOT A abc FILE : $file2_ext \n";
  18.                 }
  19.                 $pairlist{$file2}{filepairs}++;
  20.                 }
  21.  
  22. for my $key (sort keys %pairlist){
  23.         if ($pairlist{$key}{'filepairs'} == 2) {
  24.  
  25.         print " PAIR FOUND -- $key *******  \n";
  26.         push (@pairmv,$key);
  27.  
  28.         }else{
  29.         print " NO PAIRS FOUND -- $key ***** \n";
  30.         }
  31.         }
  32.  
Sep 27 '07 #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

Expand|Select|Wrap|Line Numbers
  1. for my $file1 (@pair) {
  2.                 $file1 =~ s#.*\/##;
  3.                 }
  4.  
  5.                 while (defined ($file1 = shift(@pair))) {
  6.                 my $file2 = $file1;
  7.                 $file2 =~ s#.*\/##;
  8.                 my $file2_ext = $file2;
  9.                 $file2_ext =~ s/.*\.//;         # extension
  10.                         if ($file2_ext eq "abc") {
  11.                         # control file
  12.                         print " THIS IS A abc FILE : $file2_ext \n";
  13.  
  14.                         $file2 =~ s/\.abc$//;           # strip ".abc" off file2 for hash key
  15.                         }
  16.                         else {
  17.                  print " THIS IS NOT A abc FILE : $file2_ext \n";
  18.                 }
  19.                 $pairlist{$file2}{filepairs}++;
  20.                 }
  21.  
  22. for my $key (sort keys %pairlist){
  23.         if ($pairlist{$key}{'filepairs'} == 2) {
  24.  
  25.         print " PAIR FOUND -- $key *******  \n";
  26.         push (@pairmv,$key);
  27.  
  28.         }else{
  29.         print " NO PAIRS FOUND -- $key ***** \n";
  30.         }
  31.         }
  32.  

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

Expand|Select|Wrap|Line Numbers
  1.   my $firstfile = substr($file2,0,length($file2) - 4);
  2.                              if ( -e "$firstfile"){
  3.  
Thank you!
Oct 8 '07 #4
numberwhun
3,503 Expert Mod 2GB
I am able to accomplish this by checking for existance of file1 in while loop "if" condition.

Expand|Select|Wrap|Line Numbers
  1.   my $firstfile = substr($file2,0,length($file2) - 4);
  2.                              if ( -e "$firstfile"){
  3.  
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=perl]
<some code>
[/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
Oct 8 '07 #5
Sorry about that!
I will add code tags next time onwards. Thanks,
Oct 10 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by John Smith | last post: by
31 posts views Thread by surfunbear | last post: by
3 posts views Thread by lonelyplanet999 | last post: by
2 posts views Thread by James Hull | last post: by
1 post views Thread by sm00thcrimnl13 | last post: by
3 posts views Thread by John Smith | last post: by
reply views Thread by Kirt Loki Dankmyer | last post: by
1 post views Thread by M.N.A.Smadi | last post: by
13 posts views Thread by Otto J. Makela | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.