Is there a way to determine file type without changing the access date?

Well, that is a different question, and the answer is probably "no"... Unless of course the files you're dealing with happen to be named (or arranged in directories) in such a way that their type can be determined reliably by the path/name.

But if directory structure and file names are not sufficient to know the type of file content, then there is simply no way to figure out the type of content without actually reading some of the content. And there you go -- you've read the file and spoiled the access date.

On some systems (and for super-users on other systems) it may be possible to reset the access date of a file -- that is, get the file age first, then read some content to find out what type of content it has, then reset the access date to it's prior value. You'll need to look it up for your particular OS.

update: Maybe I misunderstood your question: Is it important that the access date remain as it was after your script is completely done (e.g. to avoid certain consequences for other processes)? Or is it just a matter of wanting to know the how long ago the file was accessed before you try to test its content (i.e. it's okay for the file's access time to have changed after the script is done)?

If the latter, just put the stat operation for access date before you check for text vs. binary content.


In reply to Re^3: Getting the Access Date of a file without changing it by graff
in thread Getting the Access Date of a file without changing it by slloyd

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.