473,401 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

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 925
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,509 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

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

Similar topics

2
by: Lionel | last post by:
Hi all, I would like having more informations on how we could exchange informations and/or objects between PERL and JAVA. We have a Java programs that open, maintain and close telnet...
6
by: John Smith | last post by:
Hello, I have a rather odd question. My company is an all java/oracle shop. We do everything is Java... no matter what it is... parsing of text files, messaging, gui you name it. My question...
31
by: surfunbear | last post by:
I've read some posts on Perl versus Python and studied a bit of my Python book. I'm a software engineer, familiar with C++ objected oriented development, but have been using Perl because it is...
3
by: lonelyplanet999 | last post by:
Hi, I'm a newbie to perl and is now studying about perl programming, I read some perl programming tutorials online (enter 'Perl tutorial' at google.com) and also find some sample perl scripts...
2
by: James Hull | last post by:
Hi All: I am new in the Perl world. So far I have installed cygwin and Perl 5.8.0 along with Perl DBI and DBD::Oracle modules. NowI am trying to install Perl Tk on my PC (Windows 2K). I have...
1
by: sm00thcrimnl13 | last post by:
if i have windows 2000 and know how to write perl scripts, how to i actuvate the script through perl?
3
by: John Smith | last post by:
Hello, I have a rather odd question. My company is an all java/oracle shop. We do everything is Java... no matter what it is... parsing of text files, messaging, gui you name it. My question...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
1
by: M.N.A.Smadi | last post by:
hi guys; sorry for sending a perl question here, but python guy "HAD TO" look at perl code; how can i split a string that contains white spaces and '_' any clue? thanks
13
by: Otto J. Makela | last post by:
I'm trying to install to php the Perl-1.0.0.tgz package (from http://pecl.php.net/package/perl, enabling one to call perl libraries) to a pre-existing Solaris system. Unfortunately, the attempt...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.