lomSpace has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks!
I am developing a script that will work as a cron job.
When I hard code the path where the files are located
it works fine.

/home/lomSpace/testdir1/RESULTS 2009/JAN 2009
The problem is that it needs to work this year
and several more. I have been using:
use Time::localtime; and use POSIX qw(strftime);
to deal with the issue of the subdir names. So the script
needs to know which "RESULT YEAR" and
"MONTH YEAR" to search to for the files.

My print statement returns the Year and month and the last dir
to be modified. I need to go down another dir. Any ideas?

#!/usr/bin/perl use warnings; use strict; use File::stat; use Time::localtime; use File::Copy; use File::Find; use POSIX qw(strftime); #=for my $tm = localtime; my $YEAR= $tm->year+1900; my $MONTH = $tm->mon; #my $subdir = strftime "Results %m-%d-%y", localtime; #my $YEAR=year+1900; #my $MONTH = #my $YR_RESULT_dir = strftime "RESULTS $YEAR", localtime; #my $Month_YR_dir = strftime "$MONTH $YEAR", localtime; my $root='/home/lomSpace/testdir1'; my $seqdir = "/home/lomSpace/testdir2/SeqAssembly"; print "$YEAR $MONTH $root\n"; opendir (my $dh, $root); my @files=readdir $dh; for my $f (@files){ my $mod_time=-M $root."/".$f; #skip unless modification date is less than one day ago next if $f eq '.' || $f eq '..' || $mod_time > 1.0; print $f,"\t",$mod_time,"\t",ctime(stat($root."/".$f)->mtime),"\n" +; copy("$f","$seqdir"); } closedir $dh;
  • Comment on Dynamically searching subdirs with titles based on year,month, and date
  • Download Code

Replies are listed 'Best First'.
Re: Dynamically searching subdirs with titles based on year,month, and date
by johngg (Canon) on Jan 16, 2009 at 22:54 UTC

    Vaguely echoing JavaFan, you use File::Find but don't actually seem to use it. That would go down another dir, or several if you so desire. Have a look at the documentation to see how you can apply it.

    Cheers,

    JohnGG

Re: Dynamically searching subdirs with titles based on year,month, and date
by JavaFan (Canon) on Jan 16, 2009 at 21:56 UTC
    I need to go down another dir. Any ideas?
    Uhm, chdir?

      Ok, so I chdir. The deal is that the subdir is based on
      the script looking at the most recent subdir.
      Next month the "FEB 2009/Results %m-%d-%y" subdirs
      will be created under "RESULTS 2009". Now there will
      be "JAN 2009" and "FEB 2009" subdirs with
      "Results %m-%d-%y" subdirs.

      #!/usr/bin/perl use warnings; use strict; use File::stat; use Time::localtime; use File::Copy; use File::Find; use POSIX qw(strftime); #=for my $tm = localtime; my $YEAR= $tm->year+1900; my $MONTH = $tm->mon; #my $subdir = strftime "Results %m-%d-%y", localtime; #my $YEAR=year+1900; #my $MONTH = #my $YR_RESULT_dir = strftime "RESULTS $YEAR", localtime; my $Month_YR_dir = strftime "$MONTH $YEAR", localtime; my $newdir = '/home/mgavi/testdir1/$Month_YR_dir/'; print "$monthyr"; my $root='/home/mgavi/testdir1'; my $seqdir = "/home/mgavi/testdir2/SeqAssembly"; print "$YEAR $MONTH $root\n"; opendir (my $dh, $root); chdir($newdir); # the new subdir to search my @files=readdir $dh; for my $f (@files){ my $mod_time=-M $monthyr."/".$f; #skip unless modification date is less than one day ago next if $f eq '.' || $f eq '..' || $mod_time > 1.0; print $f,"\t",$mod_time,"\t",ctime(stat($newdir."/".$f)->mtime),"\ +n"; copy("$f","$seqdir"); } closedir $dh;

      I get

      Usage: POSIX::strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1) at dirtt.pl line 20.
      when I run the script

        I get
        Usage: POSIX::strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1) at dirtt.pl line 20.
        when I run the script
        You get that error if you call strftime without enough argument.