473,508 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Perl for iterating and concatenating.

15 New Member
HI,

I am new to perl and I am trying to write a perl script which does the following: Can someone help me?

Firstly,

There are a set of files named yyy.properties present in many folders in a single dir eg: C:\test\perl

so i have C:\test\perl\one\yyy.properties
C:\test\perl\two\yyy.properties
C:\test\perl\three\yyy.properties etc..

Now, I need to search for this yyy.properties in each file,i.e. I need to iterate through each of this .properties file and create corresponding backups in another dir say C:\temp\one\yyy.properties
C:\temp\two\yyy.properties
C:\temp\three\yyy.properties etc...

Secondly,

There is some action happening where in these original yyy.properties are being modified. I want to add the contents of the C:\temp\one\yyy.properties to the new yyy.properties present in C:\test\perl\one\yyy.properties. And so on for the others. How can I write it in Perl? If not possible how else can I go about this?

Kindly help me if there are any suggestions.

Thanks,
Teena
Sep 8 '08 #1
9 5754
nithinpes
410 Recognized Expert Contributor
Let us know what you have tried so far. All you need to do is parse through the files in source directory using opendir() and read these files (open()) and write it to the new destination..
Sep 8 '08 #2
TeenaRoz
15 New Member
I have written two scripts:
Expand|Select|Wrap|Line Numbers
  1.    #!perl
  2.  
  3.     use strict;
  4.     use warnings;
  5.     use File::Find;
  6.     use File::Copy;
  7.  
  8.     # change these assignments as needed
  9.     my $srcdir = 'C:/Documents and Settings/tdrozario/Desktop';
  10.     my $destdir = 'C:/test/';
  11.  
  12.     finddepth(\&wanted, $srcdir);
  13.  
  14.     sub wanted {
  15.          return if -d;
  16.          my ($file) = $File::Find::name =~ /^...(.+)$/;
  17.          $file =~ s~/~_~g;
  18.          copy($File::Find::name, "$destdir/$file");
  19.     }
  20.  
Not sure if this is correct. But it doesn do what I have wanted.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3.  
  4. use warnings;
  5. use strict;
  6. use File::Find;
  7. use File::Copy;
  8.  
  9. use Cwd;
  10.  
  11. my $file1=".properties";
  12.  
  13.  
  14.  
  15.  
  16. my $dir = 'c:\Documents and Settings/tdrozario/Desktop ';
  17.  
  18.  
  19.  
  20.  
  21. my @files = <c:\Documents and Settings/tdrozario/Desktop/*.properties>;
  22. foreach $file1 (@files) {
  23.  
  24. #open my $FILE, '>>', $file1 or die $!;
  25. my $bat1= "b.properties";   
  26. move($file1, $bat1)
  27. }
  28.  
Sep 8 '08 #3
nithinpes
410 Recognized Expert Contributor
I have written two scripts:
Expand|Select|Wrap|Line Numbers
  1.    #!perl
  2.  
  3.     use strict;
  4.     use warnings;
  5.     use File::Find;
  6.     use File::Copy;
  7.  
  8.     # change these assignments as needed
  9.     my $srcdir = 'C:/Documents and Settings/tdrozario/Desktop';
  10.     my $destdir = 'C:/test/';
  11.  
  12.     finddepth(\&wanted, $srcdir);
  13.  
  14.     sub wanted {
  15.          return if -d;
  16.          my ($file) = $File::Find::name =~ /^...(.+)$/;
  17.          $file =~ s~/~_~g;
  18.          copy($File::Find::name, "$destdir/$file");
  19.     }
  20.  
Not sure if this is correct. But it doesn do what I have wanted.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3.  
  4. use warnings;
  5. use strict;
  6. use File::Find;
  7. use File::Copy;
  8.  
  9. use Cwd;
  10.  
  11. my $file1=".properties";
  12.  
  13.  
  14.  
  15.  
  16. my $dir = 'c:\Documents and Settings/tdrozario/Desktop ';
  17.  
  18.  
  19.  
  20.  
  21. my @files = <c:\Documents and Settings/tdrozario/Desktop/*.properties>;
  22. foreach $file1 (@files) {
  23.  
  24. #open my $FILE, '>>', $file1 or die $!;
  25. my $bat1= "b.properties";   
  26. move($file1, $bat1)
  27. }
  28.  
I you are trying to get same folder structure inside destination directory, you should create the directories(e.g. 'one', 'two' etc.) containing required files in the destination path.
The following script will look for all files ending with '.properties' and copy them to destination path(will be put in subfolders as in the source path).

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.     use warnings;
  3.     use File::Find;
  4.     use File::Copy;
  5.  
  6.     # change these assignments as needed
  7.     my $srcdir = 'C:/Documents and Settings/tdrozario/Desktop';
  8.     my $destdir = 'C:/test/';
  9.  
  10.     finddepth(\&wanted, $srcdir);
  11.  
  12.     sub wanted {
  13.          return if -d;
  14.          if($File::Find::name =~ /^.+\.properties$/) {
  15.          my $file = $File::Find::name ;
  16.          #get the subfolder containing the file
  17.          my $subdir=$1 if($file=~/\/([^\/]+)\/[^\/]+\.properties$/); 
  18.          my $dest = $destdir.$subdir; 
  19.          mkdir($dest) unless(-e $dest); # create the folder-structure
  20.          copy($file, "$dest") or warn "$!";
  21.     }
  22.     }
  23.  
  24.  
- Nithin
Sep 9 '08 #4
TeenaRoz
15 New Member
Cool this seems to be working just fine!. Now I have to concatenate these .properties file with the current properties file and place it in the source itself. Because the current .properties file will be modified as the next step. So I have to write another perl file which simply appends the details in the test folder to the details in the source folder .properties. How do I proceed with it. I have too iterate through the files and do some concatenate function?
Sep 10 '08 #5
nithinpes
410 Recognized Expert Contributor
Cool this seems to be working just fine!. Now I have to concatenate these .properties file with the current properties file and place it in the source itself. Because the current .properties file will be modified as the next step. So I have to write another perl file which simply appends the details in the test folder to the details in the source folder .properties. How do I proceed with it. I have too iterate through the files and do some concatenate function?
Do you want to append to the source file or overwrite it?? If you are appending, there will be duplicate lines of data in source file as the destination file is a copy of it.
The following script will search for the modified files in the new path and append the data to source file. However, if what you need is overwriting file replace ">>" (append) operator with ">" (write).

Expand|Select|Wrap|Line Numbers
  1.  use strict;
  2.     use warnings;
  3.     use File::Find;
  4.     use File::Copy;
  5.  
  6.     # change these assignments as needed
  7.    my $srcdir = 'C:/Documents and Settings/tdrozario/Desktop';
  8.    my $destdir = 'C:/test/';
  9.  
  10.     finddepth(\&wanted, $srcdir);
  11.  
  12.     sub wanted {
  13.          return if -d;
  14.          if($File::Find::name =~ /^.+\.properties$/) {
  15.          my $srcfile = $File::Find::name ;
  16.          my $destfile = $srcfile; 
  17.          $destfile =~ s/$srcdir/$destdir/; # get destination filename
  18.          open(R, "$destfile") or warn "open $destfile failed:$!";
  19.          open(O,">>$srcfile") or warn "open $srcfile failed:$!";#append file 
  20.           if((-M $destfile) != (-M $srcfile)) { #if modified time is not same
  21.              print O "\n";
  22.              print O while(<R>);
  23.              }
  24.           close R; close O;       
  25.     }
  26.     }
  27.  
  28.  
Sep 10 '08 #6
TeenaRoz
15 New Member
Hi nithinpes ,

I see that from the src file its trying to retrieve some files which do not even exist there. Is it something to do with the src file?


open C:/test/x.properties failed:No such file or directory at C:\Documents and S
ettings\tdrozario\Desktop\ReplaceExistingScript.pl line 22.
open C:/test/BnR/HTMLPAGES/us/motors/build.properties failed:No such file or dir
ectory at C:\Documents and Settings\tdrozario\Desktop\ReplaceExistingScript.p l l
ine 22.
open C:/test/BnR/HTMLPAGES/us/us/build.properties failed:No such file or directo
ry at C:\Documents and Settings\tdrozario\Desktop\ReplaceExistingScript.p l line
22.
open C:/test/BnR/sukanya/motors/build.properties failed:No such file or director
y at C:\Documents and Settings\tdrozario\Desktop\ReplaceExistingScript.p l line 2
2.
open C:/test/BnR/sukanya/us/build.properties failed:No such file or directory at
C:\Documents and Settings\tdrozario\Desktop\ReplaceExistingScript.p l line 22.
open C:/test/BnR/sukanya/us/BuildInfo.properties failed:No such file or director
y at C:\Documents and Settings\tdrozario\Desktop\ReplaceExistingScript.p l line 2
2.
open C:/test/BnR/sukanya/us/Groups/4cb_assets/BuildInfo.properties failed:No suc
h file or directory at C:\Documents and Settings\tdrozario\Desktop\ReplaceExisti
ngScript.pl line 22.
open C:/test/BnR/sukanya/us/motors/build.properties failed:No such file or direc
tory at C:\Documents and Settings\tdrozario\Desktop\ReplaceExistingScript.p l lin
e 22.



These are the errors am getting. I am not sure why it is trying to search into some folders which are not even present there? Please can you help.
Sep 12 '08 #7
TeenaRoz
15 New Member
Hi nithin I resolved it. Thanks so much for your help and patience.
Sep 12 '08 #8
TeenaRoz
15 New Member
Hi Nithin,

Please tell me what am doing wrong in the below code. Its the same functionality. I want to take the files from deep down the sub folders. But few folders return error:

Expand|Select|Wrap|Line Numbers
  1. #ICE/config/scripts/htmlpagesunzip
  2. # This file is used to create a back up of all the build.properties files.
  3.  use strict;
  4.  use warnings;
  5.  use File::Find;
  6.  use File::Copy;
  7.  # change these assignments as needed
  8.  my ($srcdir,$destdir);
  9.  
  10.  $srcdir = $ARGV[0];
  11.  $destdir = $ARGV[1];
  12.  finddepth(\&wanted, $srcdir);
  13.  
  14.  sub wanted {
  15.  return if -d;
  16.  if($File::Find::name =~ /^.+build.properties$/) {
  17.  my $file = $File::Find::name ;
  18.  
  19.  #get the subfolder containing the file
  20.  my $temfile_dir = $file;
  21.    $temfile_dir =~ s/$srcdir/$destdir/;
  22.    $temfile_dir =~ s/build.properties//;
  23.    print("\n before concat $temfile_dir \n");
  24.   mkdir("$temfile_dir") unless(-e "$temfile_dir"); # create the folder-structure
  25.     print("\n $temfile_dir \n");
  26.  copy($file, "$temfile_dir") or warn "$!";
  27.   }
  28.  
  29.  
  30. }
  31.  
Feb 17 '09 #9
nithinpes
410 Recognized Expert Contributor
Instead of posting in an old thread, you should have opened a new one.

Nothing seems to be wrong except for the mkdir(). The files with .properties may not be present inside some sub-folders(in which case the directory structure is not created in destination folder) but may be present in a deeper sub-folder (hence makedir fails to create the dir structure). Replace the mkdir()statement with following block inorder to get the error:

Expand|Select|Wrap|Line Numbers
  1.   unless(-e "$temfile_dir"){ 
  2.  # create the folder-structure
  3.   mkdir("$temfile_dir") or warn "Error creating folder: $!\n";
  4.  
If this is the root cause, you have to create the upper folders first using mkdir function.
Feb 17 '09 #10

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

Similar topics

2
1235
by: John Abel | last post by:
Does anyone know of a quick way of performing this: $testVar =~ s#/mail/.*$##g The only way I can think of doing it, is: mailPos = testVar.find( "mail" ) remainder = testVar Any ideas...
14
1732
by: foodic | last post by:
i am fresher to C++ programming, and I just want to learn Concatenating Calls, I have written a program, class SetMe { public: void setX(int x) {_x = x;} void setY(int y) {_y = y;} void...
7
1684
by: Dave Hansen | last post by:
OK, first, I don't often have the time to read this group, so apologies if this is a FAQ, though I couldn't find anything at python.org. Second, this isn't my code. I wouldn't do this. But a...
1
3417
by: Miguel Manso | last post by:
Hi there, I'm a Perl programmer trying to get into Python. I've been reading some documentation and I've choosed Python has being the "next step" to give. Can you point me out to Python...
4
2319
by: Juan | last post by:
Does any one know if there are reported bugs when concatenating strings? When debugging each variable has the correct value but when I try to concatenate them some values are missing (I can´t see...
4
6008
by: Gary Morrison | last post by:
I need to create a lot of fairly-short audio files from the concatenation of a lot of even shorter audio files. I'd like to control that from a Perl script. The audio files would presumably be...
2
2933
by: Andrew Robert | last post by:
I have two Perl expressions If windows: perl -ple "s/()/sprintf(q#%%%2X#, ord $1)/ge" somefile.txt If posix perl -ple 's/()/sprintf("%%%2X", ord $1)/ge' somefile.txt
7
2761
by: Mary | last post by:
I have a student who has a hyphenated first name. If I concatenate the name like this: StudentName:( & ", " & ), it works as expected. If, however, I try to get the first name first by...
10
1153
by: kj | last post by:
Hi. I'd like to port a Perl function that does something I don't know how to do in Python. (In fact, it may even be something that is distinctly un-Pythonic!) The original Perl function takes...
0
7233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7135
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
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7410
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5650
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,...
1
5060
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...
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.