I'm a newbie in perl, have to do some patch work on my SDK so thaught perl can help me in doing this.
My requirement is as under:
---------------------------
root/
|--directory1---a.txt
|
|
|--directory2---b.txt
[ Hope this representation is clear!]
1. I've to insert some lines say "MY_COMMENT_HERE" in a.txt and b.txt files. This line is to be placed in between the code (not at start or end).
2. These lines shud be added to the code once only. If i re-run the script I shud receive an error message.
What I'm able to do:
--------------------
1. I've succedded in opening a file say "a.txt" and append my comment at the last (end of file). but this not a requirement.. actually it should append in between the code.
2. I am able to get the error message once I re-run my script. i.e. file is updated once only and not again and again..
Issues:
-------
1. I'm clueless about how to traverse across different folders in "root" directory and locate "a.txt" and then append the same...
2. How do I append in between the code..? I'm able to append at the end, but thats not required...
My code is as under:
place all code in
Expand|Select|Wrap|Line Numbers
- #!C:/Perl/bin/perl.exe -w
- $My_File="load_env.sh";
- $Target = " YOUR_COMMENT_HERE ";
- if ( !is_target_found( $My_File, $Target ) )
- { open($My_File_FP,">>", "$My_File") || die("$MyFilewill not open for update!");
- print $My_File_FP "\n $Target \n";
- close($My_File_FP); }
- sub is_target_found
- { (my $My_File, my $Target) = @_ ;
- open(my $My_File_FP, "<", "$My_File") || die "$MyFile will not open for input!";
- my $is_found = 0;
- TARGET_LOOP:
- while ( <$My_File_FP> )
- {
- if ( /$Target/ )
- {
- print " found $Target on line $. \n";
- $is_found = 1 ;
- last TARGET_LOOP;
- }
- }
- close $My_File_FP;
- return $is_found;
- }
Hope my approach is correct..
thanks for replying ASAP..
Regards,