Skip to content

Instantly share code, notes, and snippets.

@jeffcatania
Last active December 15, 2015 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffcatania/5246025 to your computer and use it in GitHub Desktop.
Save jeffcatania/5246025 to your computer and use it in GitHub Desktop.
Scan for new files in a directory. The first argument is a directory to scan. The second argument is optional, and it is a directory to copy any newly added files to. This is for debugging a running program.
#!/usr/bin/perl
use strict;
use warnings;
use File::Copy;
my $dir_to_scan = $ARGV[0];
my $dir_to_save = $ARGV[1];
if($dir_to_scan !~ /\/$/) { $dir_to_scan .= '/'; }
if($dir_to_save !~ /\/$/) { $dir_to_save .= '/'; }
my %found_files = ();
print "scanning dir for changes: " . $ARGV[0] . "\n";
while(1) {
opendir(DIR, $dir_to_scan) or die $!;
while(my $file = readdir(DIR)) {
my $full_file_path = $dir_to_scan . $file;
if($file !~ /^\./ and not defined($found_files{$file})) {
$found_files{$file} = 1;
print $file . "\n";
if(defined($dir_to_save)) {
print "Saving $full_file_path to $full_path_for_save\n";
copy($full_file_path, $dir_to_save) or die "Copy failed: $!";
}
}
}
closedir(DIR);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment