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


In reply to Re^2: HELP!!!! VB code to perl? by diotalevi
in thread HELP!!!! VB code to PERL? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.