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

I am reading a directory for files and if the files are newer than say X then I would like to copy them. The issue I am having is that I cannot reset the time when the while loop reads the directory again. So if the file is newer than X the next time through the loop it reads the same time so that action is constantly repeated. I am hoping on each loop through that the time of how old the files will be updated by the value of X. So if my if statement becomes untrue the action will stop

Here is my code
##! /usr/local/bin/perl use strict; use warnings; use File::Copy; my $sleep = 10; my $dirname = "Directory A"; my $dirname2 = "Directory B"; my $filterstring = "\\.jpg\$"; while (1) { opendir ( DIR, $dirname ) || die "Error in opening dir $dirname\n"; foreach my $file (readdir (DIR)) { next if not $file =~ m/$filterstring/; $file = $dirname.'/'.$file; print "found $file, mtime: ".(-M $file)."\n"; if (-f $file && (0.0005 > -M $file)){ my $capture = $dirname . $file; my $qc = $dirname2; print "Copying $file to $qc\n"; copy($file, $qc); reset; } } reset ; closedir (DIR); sleep $sleep; 1; }

Replies are listed 'Best First'.
Re: Reset the time after a readdir
by Anonymous Monk on Oct 21, 2014 at 23:28 UTC

    ...

    reset the time? Is "the time" a variable of some sort? What is the name of this variable?

    Have you tried writing a program that loops, prints "the times", then every once in a while makes a file "newer", so that you can see how/if the time is changed?

    I think you'll be better off to use time and stat and avoid reset

      So maybe

      Or

      #!/usr/local/bin/perl -- ## theTimeDifferCopier.pl ## 2014-10-21-16:50:47 ## ## ## ## ## ## perltidy -olq -csc -csci=3 -cscl="sub : BEGIN END " -otr -opr -ce +-nibc -i=4 -pt=0 "-nsak=*" ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if " -otr -opr +-ce -nibc -i=4 -pt=0 "-nsak=*" ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if while " -otr + -opr -ce -nibc -i=4 -pt=0 "-nsak=*" #!/usr/bin/perl -- use strict; use warnings; use Path::Tiny qw/ path /; Main( @ARGV ); exit( 0 ); sub Main { my $basetime = $^T; my $lasttime = $basetime; while( 1 ) { my @files = path( $dir )->children( qr/$filterPattern/ ); for my $file ( @files ) { my $mtime = $file->stat->mtime; my $diffSeconds = $mtime - $lasttime; if( $diffSeconds < 30 ) { print "## $file is so new its $diffSeconds old\n"; } } sleep 1; $lasttime = time; } } ## end sub Main
Re: Reset the time after a readdir
by Laurent_R (Canon) on Oct 22, 2014 at 07:03 UTC
    Please use <code> and </code> tags for your code snippets, to make them readable.
Re: Reset the time after a readdir
by soonix (Chancellor) on Oct 23, 2014 at 07:48 UTC

    If you process the files in alphabetical order, the time is not so very useful to determine which files aren't processed yet. You have to sort the files by mtime and store the newest time somewhere.

    Another possibility: On DOS/Windows, there is an "Archive" file attribute, which is set whenever a file is modified. If your backup software doesn't make use of it (ask your sysadmin), you could clear this attribute for files you processed and filter them out. See Win32::File.