Welcome to WebmasterWorld Guest from 50.19.190.144

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

script will duplicate-rename data file

script will duplicate-rename data file

     
4:06 pm on Jul 21, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 9, 2003
posts:167
votes: 0


Hi all ...

Forum member "netcommr" said in a earlyer question to me:

NEVER make direct edits to a data TXT file. Create a new file and populate it with the new data(what your keeping), then rename your old 'data.txt' file to 'data.txt.bu' for backup, then rename your new file to 'data.txt'. The reason for doing this is if the system faults in the middle of the operation you have not destroyed your data file.

I think he is 100% right .. beter duplicate a file before you go edit it
and have risk some one else write to it as well and then mess up all data

so my question is :::: .......

Can some one show me in perl code
How i can duplicate a existing file and rename it as well the file ecists n ned to create first ...

lets say i have file named news.txt

how do duplicate news.txt and name this second file bak.news.txt

i cant find this clear in my perl cook book

7:46 pm on July 21, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 6, 2000
posts:904
votes: 0


There's a few options. The basic solution is to read the file into memory and then output the data to a new file (effectively copying it).

Then there's the File::Copy module which does the above for you wrapped in a simple function, or for a quick fix you can use shell commands enclosed in backticks - eg

$foo=`cp news.txt bak.news.txt`;

(That's on unix systems, I'm sure something similar can be done in Dos)

8:50 pm on July 21, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 9, 2003
posts:167
votes: 0


it only has to work on unix/linux freebsd and so on ...

no nt windows severs