Oh *that* is a particularly poor spot of code. Someone else noted that it has the side effect of removing all the contents of the listed files. I think that the problem needs re-thinking. Consider what happens when you just open a file and save it without changing anything - atime and mtime are altered. I surmize that the poster's problem is really to make the file appear "new" or to simulate the effect of the common `touch' program.
To that end - either the user should try using an honest to goodness real `touch' command from one of the unix tool sets. There are some good examples mentioned over at Looking for command-line UNIX-like 'tar' for Windows. A perl implementation is also very simple (which you well know if you redefine the problem this way). Here's the poster's original code snippet redone with this taken into account.
use constant MIN_FILES => "C:/*.min";
touch_files( MIN_FILES );
sub touch_files {
# The touch_files subroutine accepts a single
# parameter which is passed to the glob() function.
# All files matched by the file specification are
# noted as having been "touched" or updated. This
# routine does not actually alter the files so the
# contents are not altered.
# Get the filenames to search for from the
# parameter list.
my $filenames = shift;
# Get the current timestamp. All files will be stamped
# with this time.
my $now = time;
# Loop once for each file. See [perldoc] on glob()
# to see what is acceptable input.
for my $touch_file ( glob $filenames ) {
# Update the mtime/atime values and don't bother
# actually opening the file. This function (like
# all the others) is documented in [perlfunc].
utime $now, $now, $touch_file;
}
}
Update: I futzed with the code some so it's now a subroutine and there are more comments. I also added a 'my' to the $now variable. The sense of the code remains the same.
Fun Fun Fun in the Fluffy Chair |