Hello Monks! I am a new Perl programmer and using command prompt to compile in Windows enviornment. Please see my code below and help understand why its not working. I am trying to get all the file names in a folder into an array and then compare it to all the file names in a .txt file. The names in the array that are not in the file is stored in a different .txt file and also added to the original .txt file. Everything till the second comment works fine. Please excuse me if the code structure is not optimal... still learning the ropes.
Thank you!
##########################
my $directory = 'C:\';
opendir(DIR, $directory);
my @files = grep { $_ ne '.' && $_ ne '..' } readdir DIR;
closedir(DIR);
# storing all file names in the folder in an array
foreach(@files){
print $_,"\n";
}
# emailsent.txt-will contain the name of all files for whom an alert h
+as been sent
# emailtogo.txt-will contain the name of all new files for whom an ale
+rt has to be sent
my $matchfile = 'emailsent.txt';
my $outfile = 'emailtogo.txt';
open (IN, ">>$matchfile");
open (OUT, ">>&outfile");
# Comparing the array to the names in $matchfile
foreach(my $file = @files){
for my $file2(<IN>){
if ($file ne $file2){
print OUT $file;}
}
#Insert names to original text file
my @files = <OUT>;
foreach(@files){
print IN, "\n";
}
close(IN);
close(OUT);
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.