Could you try running this and let me know if it functions properly. I combined a few subs and eliminated the delete sub. But I wasn't sure how to test it.
#!/usr/bin/perl use strict; use warnings; use File::Copy; sub copy_from_root { my $ftp_dir = shift; opendir my $dir, $ftp_dir or die "Cannot open $ftp_dir: $!"; my %short_names; for my $file_name (grep /\.(?:txt|doc|mpd|mta)$/i, readdir $dir) { my $daym = (localtime)[3]; my @dirs = ("c:/afis3too/$daym", 'c:/scripts/attempt', 'c:/rep +orts'); print $file_name; chomp $file_name; my $source = "$ftp_dir/$file_name"; $short_names{sprintf('%.6s.bat', lc $file_name)}++; if ($file_name =~ /^(?:afinv|csinv|sfinv)/i) { s/\.mta$/\.mpd/i; push @dirs, 'c:/invoice'; } elsif ($file_name =~ /\.doc$/i) { s/doc$/txt/ig; } elsif ($file_name =~ /\.mta$/i) { s/mta$/mpd/ig; } foreach my $dir (@dirs){ copy($source, "$dir/$file_name") or die "Cannot copy $dir/$file_name: $!"; } unlink $source or die "Cannot unlink $source: $!"; } return keys %short_names; } sub batch_files { my $batch_dir = shift; opendir my $dir, $batch_dir or die "Cannot open $batch_dir: $!"; my @files = grep /\.bat$/i, readdir $dir; return \@files; } sub run_the_batches { my ($short_names, $batch_files, $dir) = @_; my @matches; foreach my $short_name (@$short_names){ push @matches, grep /$short_name$/i, @$batch_files; print "Working...\n"; } system("$dir/$_") for @matches; } my $ftp_dir = 'c:/ftproot'; my $report_dir = 'c:/reports'; print "\n\n\n"; print " Robco Inc. Presents : \n\n\n"; print " The NEW, AMAZING, INCREDIBLE ....\n\n\n"; print " Robco Document Rotisserie!!!!!\n\n\n"; print "Just RENDER it, THEN don't REMEMBER it!!!!!\n\n"; print " It's just that easy.\n\n\n\n\n"; while (1) { print localtime, "\n"; print "sleeping .... ZzZzZzZzZz......\n"; sleep (10); print "OK! OK! .... I'm awake! .... I'm awake\n"; my $short_names = copy_from_root($ftp_dir); my $batch_files = batch_files($report_dir); run_the_batches($short_names, $batch_files, $report_dir); }
As mentioned above, I am unsure how to test all my changes. I'm sure there's a glitch or two.
In reply to Re: Document maintenance script
by CharlesClarkson
in thread Document maintenance script
by blacksmith
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |