
yashukla
Novice
Jun 17, 2010, 10:41 AM
Post #1 of 2
(3680 views)
|
Script to append a file only once and return error on subsequent run
|
Can't Post
|
|
I want to write a perl script that appends "My_File.txt" with a "string" when the script is run for the very first time. Subsequently if I run this script again, I shud get a message that "string" is already appended in "My_File.txt". I've written a code, but it is not giving the desired result. Pls let me know the issue with this script and suggest a relevent solution for the same..#!C:/Perl/bin/perl.exe -w $My_File="D:/My_File.txt"; $Target= "ADD_STRING_HERE"; open($My_File_FP,"<$My_File") || die("This file will not open!"); while(<$My_File_FP>) { if (/$Target/) { print " found $Target on line $. \n"; close($My_File_FP); } else { open($My_File_FP,">>$My_File") || die("This file will not open!"); print $My_File_FP "\n ADD_STRING_HERE \n"; } } close($My_File_FP); Pls let e kno the issue... I'm a newbie in Perl and want to lear more thru this forum.. Thanks in advance!
|