By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,660 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,660 IT Pros & Developers. It's quick & easy.

Perl for iterating and concatenating.

P: 15
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
Share this Question
Share on Google+
9 Replies


nithinpes
Expert 100+
P: 410
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

P: 15
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
Expert 100+
P: 410
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

P: 15
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
Expert 100+
P: 410
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

P: 15
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

P: 15
Hi nithin I resolved it. Thanks so much for your help and patience.
Sep 12 '08 #8

P: 15
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
Expert 100+
P: 410
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

Post your reply

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