Hi,
Its been about 3 years since I wrote Perl code and I have seem to forgot the little that I knew. I wrote something to search 2 directories and compare the files and copy any files that exist in the other. It works but not like how I want it to. For some reason it copies all the files every time when it is suppose to only copy the files directory 2 does not have. Im assuming it has something to do with the map function. I am using ActivePerl for my development. The code is below, any help would be greatly appreciated. Thanks in advance!
use Class::Date qw(:errors date localdate gmdate now);
# Check system date for filename
$date = localdate(time);
$date->monname;
$date->yr;
$month = substr($date->monname,0,3);
$file_name = $month . $date->yr . "BU";
# Opens log to write to
open (MYFILE, '>>c:\\Perl scripts\\TEST1.log');
# Opens source directory and compares all files to all the files - If
+ new files, it copies it to 2nd location
opendir (DIR1, "C:\\TEST1") or die print MYFILE "Today is $date \n Cou
+ldn't open directory, C:\\TEST1";
while ($file1 = readdir DIR1)
{
#print "$file1 \n";
push(@files1, $file1);
}
#print "@files1 \n";
closedir DIR1;
#opendir (DIR2, "C:\\TEST2") or die print MYFILE "Today is $date \n Co
+uldn't open directory, C:\\TEST2";
while ($file2 = readdir DIR2)
{
#print "$file2 \n";
push(@files2, $file2);
}
#print "@files2 \n";
closedir DIR2;
#Compares the directories and copies the missing files to the other di
+rectory
%temp = map {$_,$_} @files2;
for(@files1) {
next if exists $temp{$_};
system("copy C:\\TEST1\\$_ C:\\TEST2\\");
print MYFILE "$_ has been copied\n";
}
close(MYFILE);
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.