in reply to Appending to a file

You don't have a file open to append to. Try (untested) open(LOGFILE, '>>test.txt') or die"Can't append: $!";

and your print statement becomes print LOGFILE "\nDirectory: $file\n";