in reply to Modified Date, file renaming

The following code was used to delete log files older than x days. You should be able to modify it for your purposes. It's not state of the art (pretty old and designed to be supported by non perl experts so don't take it as gospel).

### ------------------------------- ### Read local directory opendir LOCALDIR, $location || croak "Unable to open: $location because $!"; @local_files = readdir LOCALDIR; foreach $file (@local_files){ ### Don't try to delete directories if ($file eq '.' || $file eq '..' || -d "$location$file" ) { next; } ### Test the age of each file in the directory and delete ### if too old (N.B. need to prepend directory to find file an +d ### use unlink for OS portable deletion). if ( -M "$location$file" <= $age) { $p_debug && print "$file is ok, only ", -M "$location$file", " days old\n"; } else { $p_debug && print "$file is too old, it's ", -M "$location$file", " days old\n"; unlink("$location$file") || croak "Unable to delete: $location$file because: $!"; } }

You could use something like:

### Move each file into archive directory move( "$location$file", "$target$file") || croak "Unable to move: $location$file because: $!";

... the above to move files around. You'll need to rename the $file for the target however.

Replies are listed 'Best First'.
Re: Re: Modified Date, file renaming
by ChemBoy (Priest) on May 16, 2001 at 18:12 UTC

    Look out! If you're going to use the "|| die" syntax instead of "or die", you need to put parentheses around the arguments to opendir.

    opendir (FOO, $bar) || die "No dice";
    and
    opendir FOO, $bar or die "No dice";
    are equivalent: they try to open the dirhandle FOO to the directory listed in $bar, and if opendir fails, they die. But
    opendir FOO, $bar || die "No dice";
    is equivalent to
    opendir FOO, ($bar || die "No dice");
    which means that die is only called if $bar is empty, not if opendir fails. Which is unlikely to be what you intended.



    If God had meant us to fly, he would *never* have give us the railroads.
        --Michael Flanders

      Thanks - well spotted.

      $clean_up_code_before_posting || die "red face: $!";

      Must remember to clean up before I post (I now use or die)...