Hi, I am trying to figure out a solution (perl script) that would copy a file to another directory by the timestamp in which it was written for a certain day. Furthermore, this needs to be modular enough to handle different sets of directories running at different intervals. So for example, 3 different directories contain 3,000 files each. Directory 1 might copy a set of files over every 300 seconds. Directory 2 might copy a set of files over every 1800 seconds. So, all the file timestamps that fall in that particular range as compared to the current time should be copied over for processing. And this needs to run for 24 hours until all files have been copied. Below is where I am at now... I am able to read in files, get file timestamps, figure out intervals, but having trouble figuring out how to copy based on the current time and range in which to move the set of files. Thanks.
#!/usr/bin/perl use POSIX; $count_interfaces = `awk ' END { print NR } ' /MM/work/control/interfa +ce_frequencies.txt`; $count_interfaces = $count_interfaces - 1; print "count_interfaces = $count_interfaces\n"; chomp($count_interfaces); $starttime=localtime time; print "**********************Start Extracting Interfaces: STARTTIME=$s +tarttime**************************************\n"; removeInterfaceFile(); getDirectories(); getFiles($directory,$directory_count); calcIntervals($directory_count,$count_interfaces); $endtime=localtime time; print "*******************Finished Extracting Interfaces: ENDTIME=$end +time******************************************\n"; sub getCurrentTime { $now_string = strftime "%Y:%m:%d:%H:%M:%S", localtime; print "now_string = $now_string\n"; } sub getDirectories { $dir = "/MM/work/MMdayofdata"; $interface_directory="MMdayofdata"; $directory_count = 0; @directories = <$dir/*>; } sub getFiles { foreach $directory (@directories) { if (-d $directory) { print "INTERFACE_$directory_count=$directory\n"; $directory_count++; $subdir = $directory; @files = <$subdir/*>; ($dir1,$dir2,$dir3,$dir4,$dir5,$interface)=split(/\//,$dir +ectory); $interface =~ s/ //g; $interface =~ s/\n//g; $interface =~ s/\r//g; print "directory=$interface\n"; open(OUT3, ">/MM/work/control/$interface.dat"); #open(OUT3, ">>/MM/work/control/FILETIMESTAMPS.dat"); open(OUT3, ">>/MM/work/control/MMinound/"); foreach (@files) { my $file = "$files/$_"; $file =~ s/ //g; $file =~ s/\n//g; $file =~ s/\r//g; $file =~ s/\/\//\//g; my $mtime = (stat($file))[9]; $file_time = strftime("%Y%m%d_%H:%M:%S", localtime("$m +time")) . "\n"; $hour = strftime("%H", localtime("$mtime")) . "\n"; $minute = strftime("%M", localtime("$mtime")) . "\n"; #parse filename #get hour of day, minute of day to decide how to write + filename. #if the minute is 1-5 minutes, they fall into same cat +egory for 300 seconds #if (($hour == $nowhour) && ($minute == $nowminute)) { $subsubdir="/MM/work/control/$interface_directory/$int +erface/"; print "subsubdir=$subsubdir\n"; @interface_files = <$subsubdir/*>; print "interface_files = $interface_files[0]"; $reccount = 0; foreach $record (@interface_files) { chomp($record); #$record =~ s/*//g; #rename($record, "$interface_m_PT$hour$minute$ +reccount_00.dat"; $reccount++; } print OUT3 "$file\t" . "$file_time"; } close(OUT3); } } } sub calcIntervals { open(FH4, "</MM/work/control/interface_frequencies.txt") or die("C +ould not open interface_frequencies.txt"); open(OUT6, ">/MM/work/control/RUNTIME.dat") or die("Could not writ +e to RUNTIME.dat"); @Out4 = <FH4>; close(FH4); $count_rows = 0; #First record in OUT4 is a header row foreach $m_interface (@Out4) { chomp($m_interface); if ($count_rows == 0) { #Print out the hearder row for runtime.dat file print OUT6 "INTERFACE,FREQUENCY,TOTAL_INTERVALS_PER_DAY,FI +LES_PER_INTERVAL,TOTAL_FILES\n"; $count_rows++; next; } ($interface, $frequency)=split(/\|/,$m_interface); chomp($interface); chomp($frequency); open(FH5, "</MM/work/control/$interface.dat") or die("Could no +t open $interface.dat"); @Out5 = <FH5>; close(FH5); $total_files = `awk ' END { print NR } ' /MM/work/control/$int +erface.dat`; chomp($total_files); print "$interface = $total_files\n"; warn "Number of files for $interface is less than 1: $total_fi +les\n" if ($total_files < 2); $totalIntervalsPerDay = floor(86400/$frequency); warn "$interface: Number of intervals per day is less than 1: +$totalIntervalsPerDay\n" if ($totalIntervalsPerDay < 1); #print "$interface: Intervals Per Day: 3600/$frequency=$totalI +ntervalsPerDay\n"; $filesPerInterval = floor($total_files/$totalIntervalsPerDay); warn "$interface: Number of files per interval is less than 1 +_$filesPerInterval_ $totalIntervalsPerDay > $total_files\n" if ($file +sPerInterval <= 0); #print "Files Per Interval $interface: $total_files/$totalInte +rvalsPerDay=$filesPerInterval\n"; print OUT6 "$interface,$frequency,$totalIntervalsPerDay,$files +PerInterval,$total_files\n"; $count_rows++; } close(OUT6); } sub removeInterfaceFile() { print "Removing temp directories...\n"; `rm -rf /MM/work/control/RUNTIME.dat`; `rm -rf /MM/work/control/FILETIMESTAMPS.dat`; `rm -rf /MM/work/control/$interface.dat`; }

In reply to copy or ftp file by file timestamp by breezykatt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.