Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching a file(s) for a text string if found rename/copy/delete

Status
Not open for further replies.

elkh8me

Technical User
Jun 18, 2008
2
US
I am able to read a directory and then search files for a string match. When I find the string I want to write that file to a new file or rename and then copy to another directory the file. I can find the string but I can't stop there and do the rename/copy/delete. I'm not long in working with PERL so any help would be great. if the code does not make sence it might be because i have been trying many, many things.
Thanks for any help.

#! ./perl
use IO::File;
use File::Copy;
use File::Find;
## Open the directory
opendir(DIR,"C:\\watchin") or die "Can not open the input Directory!";
@files = grep(/\.dat$/,readdir(DIR));
closedir(DIR);
####### Now we read the files.
foreach $files (@files) {
$myfile = $files;
$report1 = 'AG Purchases Report';
open(FILE,"<C:\\Watchin\\$myfile") or die "Can't open the file darn-it \n";
open(outfile,">c:\\watchout\\AGPurchase.txt" )or die "Can't open the output file!";
@file = <FILE>;
chomp @file;
@match = grep(/AG Purchases Report/,@file);
print "@match \n";
if (@match) {
rename $myfile, AGPurchase.txt;
copy ("AGPurchase.txt", "c:\\watchout\\AGPurchase.txt");
}
print (outfile "%%EOF\n");
close FILE;
}
__END__
 
I'm a little confused. What happens if you find 2 strings that match? Are you going to keep overwriting AGPurchase.txt? If so why not just set a flag if you find it and do one copy at the end?

Some other hints... include $! in your die statements (it'll give you helpful information) copy ("blah", "blah") or die "Can't copy blah to blah: $!\n"; or something
you can use unix style slashes c:/blah/blah instead of escaping the windows style. Also if you have no variable in the quotes you should use single quotes.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Follow Travis' advice; scan the files, and when you find a match, push the file path into an array and skip to the next file. At the end, just iterate over the array to do the copy. This also has the advantage that while you are testing, you can change the 'copy' to 'print' for a safe sanity check on your code before you run it for real...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
To travs69: Yes the file will be overwritten, it's a weekly run. Yes I only want to find the first item that matches and then let the copy begin. These are reports that will be converted to PDF and then stored in a shared directory.
Thanks also to stevexff.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top