File::Copy question

I am using Windows 98 and ActiveState Perl.  I have a log file that after a 
certain size is truncated and reused.  I have written a simple script to 
copy that file to another file using File::Copy.  Is there a way to make 
sure the second file is appended instead of overwritten, using File::Copy 
or do I need to use something else?


-----

The three most dangerous things are a programmer with a soldering iron, a 
manager who codes, and a user who gets ideas.

----
0
spatz
10/7/2002 1:45:44 PM
perl.beginners 29388 articles. 4 followers. Follow

4 Replies
436 Views

Similar Articles

[PageSpeed] 38

If you want to append the contents of one file to another, then you will
have to open both files, read the first one, and write it to the second one.
Something like this should work:

######################

use strict;
use warnings;

open(LOG,"mylog.log") || die "Couldn't open logfile for reading!";
open(ARCHIVE,">>archive.log") || die "Couldn't open the archive file for
appending!";

while(<LOG>){
  print ARCHIVE $_;
}

open(LOG,">mylog.log") || die "Couldn't truncate logfile!";
close LOG;
close ARCHIVE;

#######################



-----Original Message-----
From: Steve [mailto:spatz@pennswoods.net]
Sent: Monday, October 07, 2002 6:46 AM
To: beginners@perl.org
Subject: File::Copy question


I am using Windows 98 and ActiveState Perl.  I have a log file that after a 
certain size is truncated and reused.  I have written a simple script to 
copy that file to another file using File::Copy.  Is there a way to make 
sure the second file is appended instead of overwritten, using File::Copy 
or do I need to use something else?


-----

The three most dangerous things are a programmer with a soldering iron, a 
manager who codes, and a user who gets ideas.

----

-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
0
tjohnson
10/7/2002 2:20:29 PM
--45Z9DzgjV8m4Oswq
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

if you want to append to a second file then you should use ">>" when
opening, not ">" or you will delete the text already there. if it was just
a typo then ignore me.

On Mon, Oct 07, 2002 at 07:20:29 -0700, Timothy Johnson wrote:
>=20
> If you want to append the contents of one file to another, then you will
> have to open both files, read the first one, and write it to the second o=
ne.
> Something like this should work:
>=20
> ######################
>=20
> use strict;
> use warnings;
>=20
> open(LOG,"mylog.log") || die "Couldn't open logfile for reading!";
> open(ARCHIVE,">>archive.log") || die "Couldn't open the archive file for
> appending!";
>=20
> while(<LOG>){
>   print ARCHIVE $_;
> }
>=20
> open(LOG,">mylog.log") || die "Couldn't truncate logfile!";
> close LOG;
> close ARCHIVE;
>=20
> #######################
>=20
>=20
>=20
> -----Original Message-----
> From: Steve [mailto:spatz@pennswoods.net]
> Sent: Monday, October 07, 2002 6:46 AM
> To: beginners@perl.org
> Subject: File::Copy question
>=20
>=20
> I am using Windows 98 and ActiveState Perl.  I have a log file that after=
 a=20
> certain size is truncated and reused.  I have written a simple script to=
=20
> copy that file to another file using File::Copy.  Is there a way to make=
=20
> sure the second file is appended instead of overwritten, using File::Copy=
=20
> or do I need to use something else?
>=20
>=20
> -----
>=20
> The three most dangerous things are a programmer with a soldering iron, a=
=20
> manager who codes, and a user who gets ideas.
>=20
> ----
>=20
> --=20
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
>=20
> --=20
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org

--=20
Mat Harris			OpenGPG Public Key ID: C37D57D9
mat.harris@genestate.com	matthewh.genestate.com=09

--45Z9DzgjV8m4Oswq
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE9oZjoEsYvZsN9V9kRAkf0AKCz5VhwSxFwYncyJXJQku2d/Lq6cQCcCDCd
dlaEdtU8bRzrGn2pi4k73CA=
=NoRo
-----END PGP SIGNATURE-----

--45Z9DzgjV8m4Oswq--
0
mat
10/7/2002 2:23:36 PM
Sorry for not explaining better.  See commments below.

-----Original Message-----
From: Mat Harris [mailto:mat.harris@genestate.com]
Sent: Monday, October 07, 2002 7:24 AM
To: Timothy Johnson
Cc: 'Steve'; beginners@perl.org
Subject: Re: File::Copy question


if you want to append to a second file then you should use ">>" when
opening, not ">" or you will delete the text already there. if it was just
a typo then ignore me.

On Mon, Oct 07, 2002 at 07:20:29 -0700, Timothy Johnson wrote:
> 
> If you want to append the contents of one file to another, then you will
> have to open both files, read the first one, and write it to the second
one.
> Something like this should work:
> 
> ######################
> 
> use strict;
> use warnings;
>

##Here I open the logfile for reading and the archive file for appending
 
> open(LOG,"mylog.log") || die "Couldn't open logfile for reading!";
> open(ARCHIVE,">>archive.log") || die "Couldn't open the archive file for
> appending!";
> 

##Here I read the contents of the logfile and print them directly to the
archive

> while(<LOG>){
>   print ARCHIVE $_;
> }
> 

##Once I'm done, I reopen the logfile using the > operator to truncate it so
that
##I'm not appending the same data next time, then close both files

> open(LOG,">mylog.log") || die "Couldn't truncate logfile!";
> close LOG;
> close ARCHIVE;
> 
> #######################
> 
> 
> 
> -----Original Message-----
> From: Steve [mailto:spatz@pennswoods.net]
> Sent: Monday, October 07, 2002 6:46 AM
> To: beginners@perl.org
> Subject: File::Copy question
> 
> 
> I am using Windows 98 and ActiveState Perl.  I have a log file that after
a 
> certain size is truncated and reused.  I have written a simple script to 
> copy that file to another file using File::Copy.  Is there a way to make 
> sure the second file is appended instead of overwritten, using File::Copy 
> or do I need to use something else?
> 
> 
> -----
> 
> The three most dangerous things are a programmer with a soldering iron, a 
> manager who codes, and a user who gets ideas.
> 
> ----
> 
> -- 
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> 
> -- 
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org

-- 
Mat Harris			OpenGPG Public Key ID: C37D57D9
mat.harris@genestate.com	matthewh.genestate.com	
0
tjohnson
10/7/2002 2:32:11 PM
Steve wrote:
> 
> I am using Windows 98 and ActiveState Perl.  I have a log file that after a
> certain size is truncated and reused.  I have written a simple script to
> copy that file to another file using File::Copy.  Is there a way to make
> sure the second file is appended instead of overwritten, using File::Copy
> or do I need to use something else?

Yes there is a way to do this.

open TO, ">> $old_log_file" or die "Cannot open $old_log_file: $!";

copy( $current_log, \*TO ) or die "Cannot copy $current_log: $!";



John
-- 
use Perl;
program
fulfillment
0
krahnj
10/7/2002 7:39:10 PM
Reply:

Similar Artilces:

File.Copy and File.CopyTo DO NOT COPY CONTENTS OF FILE!@
I posted on here before but no one has been of any help. I am trying to copy files of type .zip .jpg and .pdf from ones folder to another (I dont want to move it, just make a copy). How can I do this? When I use File.Copy and File.CopyTo it makes a new file in the right folder but has no content and it also erases the content on the orignal file. WTF? This is really making me mad. Visit VistaForums.com #1 Windows Vista Forum Here you go --->  http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=207Regards, Narayana Rao Surapaneni Architect | Author | MCSD for .NET | MCSD | SCJP ww...

File::Copy::copy() dies on identical files?
I'm giving File::Copy's tests a bit of a face-lift and I came across this: eval { copy("copy-$$", "copy-$$") }; printf "ok %d\n", 15+$loopconst unless $@ =~ /are identical/ && -s "copy-$$"; which basically says, "copy() should die if given the same source and destination file" which is a little extreme. Warn perhaps, but not die. I think it's just parroting the behavior of the 'cp' shell command. If nobody has any objections, I'll downgrade that to a warning and copy() just succeeds. P...

copy file question
We would like to implement some version control in our PB apps. We have sucessfully saved and then retrieved the current version of the app from the PB.ini file. We have also connected to the database and retrieved the value of what should be the current version of the PB app. At this time we would like to make a call to a subroutine to copy all files necessary for the app from our server to the clients PC if the versions are different and then update pb.ini with the new version, log the user off the app so when they logged back on they would be executing the newest version. We have a ....

Copying all files in a directory using either file.copy or directory.copy across volumes
I need to copy all the files from a cd directory to a directory on my harddrive. I have tried the directory.copy method but it will not allow me to copy across volumes. I have tried the file.copy method but you must specify a file name in the path as well as the target. This sort of defeats the purpose of what I am trying to do. Basically I am using a radio button to specify a type of CD and based on this selection the application knows what directory to look in for the files I want. Here is the script I tried last to move the file. Sub KPacs(ByVal sender As Object, ByVal e As System.EventA...

File Copy Question
I have recently been assigned a task of reengineering some old PowerBuilder code to a new version and to clean up the old code. I noticed that the original developer copied a file into a blob then wrote the blob out to a different location. Is there some reason behind this or is the developer just being creative, wouldn't it be cleaner and faster just to use a FileCopy? Thanks in advance The FileCopy function was added in version 8. The program could have been written in an older version. "Doug" <dklee@schmidt.com> wrote in message news:1165351494.2...

Copying more than one file using File::Copy
Good day=3B I apologize in advance if this is a very stupid question=85 I=27m trying to concatenate two files and have the results written to a = third file=2E = Maybe I=27m missing something obvious=2E=2E=2E but in Perldoc for File=3A= =3ACopy I = read=3A =22The copy function takes two parameters=3A a file to copy from = and = a file to copy to=2E=22 What can I do if I have a second file I want to copy from=3F (i=2Ee=2E Li= ke = DOS copy command=3A c=3A=5C file1=2Etxt + file2=2Etxt file3=2Etxt will co= py the = contents of file1=2Etxt =26 file2=2Etxt and put the...

File.Copy does not release the lock on the copied file.
 Hi! I am trying to use the File.Copy function in order to copy a file from the Client's PC to the Server. The File to be copied is selected by the user using the FileUpload control and is then used by the server to perform certain operations. My problem is that when I copy the file using the File.Copy method and then execute a streamreader process on the file straight after, I get the following error; "The process cannot access the file '\\syddev1\latest\xmasEmail.html' because it is being used by another process. " Below is the piece of code where the problem...

Problems with File.Copy() Method for copy big files
I´m using the follow commando to copy a file: source = "c:\\domains\\pagina.com.br\\wwwroot\\folder\\filesou.xml";target = "c:\\domains\\pagina.com.br\\wwwroot\\folder\\filetar.xmb"; File.Copy(source, target, true); If I used a small file (source) with for example 233000 bytes the server copy to target without problems. But if I used the file (source) with 700000 bytes the server don´t copy. I´ m copy to the same folder... Why is this happened with big file as source ? Thank you... Rogério Does it throw any error. HighOnCodingWanna get high! I don´t know if it throw any error.Ho...

copying a file without using module File::Copy
Hello All, Is the only way to copy a file from one directory to another by using the copy module...? Would anyone have a snipet of code copying a file from one dir to another... Any assistance would be appreciated... Thank you, Mike ------------------------------------------------ On Fri, 14 Feb 2003 10:13:06 -0500, Michael Pastore <Michael.Pastore@frontstep.com> wrote: > > Hello All, > > Is the only way to copy a file from one directory to another by using the > copy module...? > > Would anyone have a snipet of code copying a f...

Error when copying using File.Copy and files exist
I'm trying to copy and overwrite files to an existing directory using :file.copy(source,destination,true)The command works perfectly when the files do not exist, but when trying to overwrite the files, I get an access denied error to the destination directory and files.It's not a permission issue because the folders have 'ASPNET' and 'Everyone' Access set up with full rights.Anyone have any ideas?Thanks in advance.JeffJeff Wasilauski, MCAD .NET, MCPC# WEB DEVELOPMENT-SQL SERVER DBA"If I have answered your question please mark as answer" Check if the File exists before copying and...

file to file copy
--nextPart3236501.eDU2PhZZ46 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8Bit hello, i have to write a perl script which copies text from one file to another but only if the text is not exisiting yet. For example: in file1: word: moon word2: sky .... the same syntax for every line in file2: #some text word: honey word3: lol word4: mu .... as well the same syntax for every line so now i want to merge file1 into file2, so that word: honey will be replaced by word: moon and word2: sky will be appended to file2. i have written the following...

To copy or not to copy: that is the question
i have a dataTable in the session object . I have a method which removes a column from the table before binding e.g. DataTable dt = (DataTable) Session["MyTable"]; dt.Columns.RemoveAt[0]; DataGrid dg = new DataGrid(); dg.DataSource = dt; // ...etc a dataTable is a reference object so by removing the column in my local method I am also removing a column from the dataTable in the session object as they are both looking at the same thing. what if I want to remove a column in my local method but preserve the integrity of the in session table? at the moment ...

Re: Question about copying files.
perldoc file::copy >>> <mystic_mac1@mac.com> 07/02/02 08:40am >>> I've got a script that parses out a mail file into an array then uses the print command to copy the array to another location. Effectively I'm just copying one file from its original location to another location, I'm using a cgi script to do this in, how can I call a "cp" command from within a cgi script? I'm thinking of doing something like: sh "cp filename1.txt /new/path/filename2.txt"; Would this be enough, or do I have to require another .pl scri...

Beginner question: Where are my source files
I have a DLL project with several modules on it. All the files, including the group project files are in a single folder. Since this week, all the modules have disapeared from the Project Manager list. I can only see the dll file name folder, "MyDLL.DLL", the "Build Configurations" subfolder and the "Target Platforms" subfolder, but not any of my other modules in the dll. Of course those modules are in the project, because I can compile it , and if I right click and "Find declaration" in the module name in the uses clause it opens the module. ...

Web resources about - File::Copy question - perl.beginners

Resources last updated: 11/30/2015 2:25:01 PM