Solved Move Files based on Filename time

July 22, 2013 at 20:40:07
Specs: Solaris
Hi Experts,

Good day.

I would like to seek your assistance in achieving the following condition:


A cron script will run daily and hourly that will move the file to a certain directory based on the date and time specified in the file name. Format of the file name is mmddyyHHMM_DBCET.CSV

Example 1:
For instance, Cron Script will run on June 1, 2013 at 12 Midnight and the source directory has the following files:

0531132345_DBCET.CSV
0531132307_DBCET.CSV
0531132245_DBCET.CSV

Cron script will just move the files 0531132345_DBCET.CSV and 0531132307_DBCET.CSV but 0531132245_DBCET.CSV will be left on the source directory

Example 1:


For instance, Cron Script will run on June 1, 2013 at 8AM and the source directory has the following files:
0601130745_DBCET.CSV
0601130707_DBCET.CSV
0601130645_DBCET.CSV


Cron script will just move the files 0601130745_DBCET.CSV and 0601130707_DBCET.CSV but 0601130645_DBCET.CSV will be left on the source directory.

NOTE: cron script will move file just lagging an hour based on its file name as compared to the execution time of the cron script.

Any insights or assistance in greatly appreciated.

Thanks and Regards,
Amiti.


See More: Move Files based on Filename time

Report •

#1
July 22, 2013 at 22:59:43
I don't think awk is the best tool for this problem. I would use a scripting language like perl. If a perl solution works for you, I might look at it.

Report •

#2
July 22, 2013 at 23:47:15
Hi Nails,

Yes, I am open to use this one, although, I don't have knowledge on it, will just read the basics of perl.

Regards,
Amiti.


Report •

#3
July 28, 2013 at 19:46:52
✔ Best Answer
Sorry it took so long to answer this. My real job was getting in the way.

This perl program changes to the src directory and lists all files with a CSV extension. This program changes the datetime stamp to epoch (the number of seconds since Jan 1, 1970) and then comparing to the system datetime.

When the program works the way you want it, change the system command to use move, mv, instead of copy, cp.

Let me know if you have any questions:


#!/usr/local/bin/perl

use strict;
use warnings;
use Time::Local;
use English;

my $mysrc = "/home/nails/src"; # set your source directory
my $destdir = "/home/nails/destdir";  # set your destination directory

chdir $mysrc or die "can not change directory\n";

my $mday;
my $mon;
my $year;
my $hour;
my $min;
my $sec;
my $fileepochtime;

my $epochtime =  time();
my $totalsecs = 3600;  # number of seconds in an hour
my $pivotime = $epochtime - $totalsecs;

my $mycmd;
my @files = <*.CSV>; # get all the CSV files
foreach my $file (@files) 
   {
   if($file =~ /^[0-9]{10}/) # file name must start with 10 digits
      {
      # parse the time from the file name
      $mon = substr($file, 0, 2);
      $mday = substr($file, 2, 2);
      $year = substr($file, 4, 2);
      $hour = substr($file, 6, 2);
      $min = substr($file, 8, 2);
      $sec = 0;
      $year = "20" . $year;

      $fileepochtime = timelocal($sec,$min,$hour,$mday,$mon-1,$year);
      if($fileepochtime >= $pivotime)
         {
         print $file . "\n";
         # move to the destination directory
         $mycmd = "cp " . $file . " " . $destdir;
         system($mycmd);
         if($CHILD_ERROR != 0)
            {
            print "system function error $!\n";
            exit;
            }
         }

      }
   } 


Report •
Related Solutions


Ask Question