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
9 5754
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..
I have written two scripts: -
#!perl
-
-
use strict;
-
use warnings;
-
use File::Find;
-
use File::Copy;
-
-
# change these assignments as needed
-
my $srcdir = 'C:/Documents and Settings/tdrozario/Desktop';
-
my $destdir = 'C:/test/';
-
-
finddepth(\&wanted, $srcdir);
-
-
sub wanted {
-
return if -d;
-
my ($file) = $File::Find::name =~ /^...(.+)$/;
-
$file =~ s~/~_~g;
-
copy($File::Find::name, "$destdir/$file");
-
}
-
Not sure if this is correct. But it doesn do what I have wanted. -
#!/usr/bin/perl
-
-
-
use warnings;
-
use strict;
-
use File::Find;
-
use File::Copy;
-
-
use Cwd;
-
-
my $file1=".properties";
-
-
-
-
-
my $dir = 'c:\Documents and Settings/tdrozario/Desktop ';
-
-
-
-
-
my @files = <c:\Documents and Settings/tdrozario/Desktop/*.properties>;
-
foreach $file1 (@files) {
-
-
#open my $FILE, '>>', $file1 or die $!;
-
my $bat1= "b.properties";
-
move($file1, $bat1)
-
}
-
I have written two scripts: -
#!perl
-
-
use strict;
-
use warnings;
-
use File::Find;
-
use File::Copy;
-
-
# change these assignments as needed
-
my $srcdir = 'C:/Documents and Settings/tdrozario/Desktop';
-
my $destdir = 'C:/test/';
-
-
finddepth(\&wanted, $srcdir);
-
-
sub wanted {
-
return if -d;
-
my ($file) = $File::Find::name =~ /^...(.+)$/;
-
$file =~ s~/~_~g;
-
copy($File::Find::name, "$destdir/$file");
-
}
-
Not sure if this is correct. But it doesn do what I have wanted. -
#!/usr/bin/perl
-
-
-
use warnings;
-
use strict;
-
use File::Find;
-
use File::Copy;
-
-
use Cwd;
-
-
my $file1=".properties";
-
-
-
-
-
my $dir = 'c:\Documents and Settings/tdrozario/Desktop ';
-
-
-
-
-
my @files = <c:\Documents and Settings/tdrozario/Desktop/*.properties>;
-
foreach $file1 (@files) {
-
-
#open my $FILE, '>>', $file1 or die $!;
-
my $bat1= "b.properties";
-
move($file1, $bat1)
-
}
-
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). -
use strict;
-
use warnings;
-
use File::Find;
-
use File::Copy;
-
-
# change these assignments as needed
-
my $srcdir = 'C:/Documents and Settings/tdrozario/Desktop';
-
my $destdir = 'C:/test/';
-
-
finddepth(\&wanted, $srcdir);
-
-
sub wanted {
-
return if -d;
-
if($File::Find::name =~ /^.+\.properties$/) {
-
my $file = $File::Find::name ;
-
#get the subfolder containing the file
-
my $subdir=$1 if($file=~/\/([^\/]+)\/[^\/]+\.properties$/);
-
my $dest = $destdir.$subdir;
-
mkdir($dest) unless(-e $dest); # create the folder-structure
-
copy($file, "$dest") or warn "$!";
-
}
-
}
-
-
- Nithin
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?
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). -
use strict;
-
use warnings;
-
use File::Find;
-
use File::Copy;
-
-
# change these assignments as needed
-
my $srcdir = 'C:/Documents and Settings/tdrozario/Desktop';
-
my $destdir = 'C:/test/';
-
-
finddepth(\&wanted, $srcdir);
-
-
sub wanted {
-
return if -d;
-
if($File::Find::name =~ /^.+\.properties$/) {
-
my $srcfile = $File::Find::name ;
-
my $destfile = $srcfile;
-
$destfile =~ s/$srcdir/$destdir/; # get destination filename
-
open(R, "$destfile") or warn "open $destfile failed:$!";
-
open(O,">>$srcfile") or warn "open $srcfile failed:$!";#append file
-
if((-M $destfile) != (-M $srcfile)) { #if modified time is not same
-
print O "\n";
-
print O while(<R>);
-
}
-
close R; close O;
-
}
-
}
-
-
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.
Hi nithin I resolved it. Thanks so much for your help and patience.
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: -
#ICE/config/scripts/htmlpagesunzip
-
# This file is used to create a back up of all the build.properties files.
-
use strict;
-
use warnings;
-
use File::Find;
-
use File::Copy;
-
# change these assignments as needed
-
my ($srcdir,$destdir);
-
-
$srcdir = $ARGV[0];
-
$destdir = $ARGV[1];
-
finddepth(\&wanted, $srcdir);
-
-
sub wanted {
-
return if -d;
-
if($File::Find::name =~ /^.+build.properties$/) {
-
my $file = $File::Find::name ;
-
-
#get the subfolder containing the file
-
my $temfile_dir = $file;
-
$temfile_dir =~ s/$srcdir/$destdir/;
-
$temfile_dir =~ s/build.properties//;
-
print("\n before concat $temfile_dir \n");
-
mkdir("$temfile_dir") unless(-e "$temfile_dir"); # create the folder-structure
-
print("\n $temfile_dir \n");
-
copy($file, "$temfile_dir") or warn "$!";
-
}
-
-
-
}
-
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: -
unless(-e "$temfile_dir"){
-
# create the folder-structure
-
mkdir("$temfile_dir") or warn "Error creating folder: $!\n";
-
}
-
If this is the root cause, you have to create the upper folders first using mkdir function.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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
|
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...
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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,...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |