Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8318571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:01:07+00:00 2026-06-08T22:01:07+00:00

I am trying to write a Windows command to list files and their last

  • 0

I am trying to write a Windows command to list files and their last access times, sorted by access time.

I have used

dir [directory] /O:D /T:A /S /B > output.txt

This outputs the files in directory and sub directories in order by their last access time; however I also need it to output the last access time as well. How is this accomplished?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:01:10+00:00Added an answer on June 8, 2026 at 10:01 pm

    from within a batch file:

    >output.txt (
      for /f "delims=" %%F in ('dir /o:d /t:a /s /b "c:\myPath\*"') @echo %%~tF %%F
    )
    

    However, there are some things you need to be aware of:

    • The files are sorted by access timestamp within a directory. It does not sort by access timestamp across all the directories. Your original code has the same problem. To sort accross directories requires parsing the access timestamp and converting it into a string that will sort chronologically when ordered via SORT. Something like “yyyy-mm-dd hh24:mm”. Even that is not particularly good because you don’t have access to the seconds. You could use WMIC DATAFILE to list file names with last access timestamps at a sub-second level. But why bother, considering that…

    • The last access timestamp maintained by Windows is not reliable! There are many situations whereby an application can read a file and yet the last access timestamp is not updated. I’ve seen some reference material somewhere that talks about that, but I don’t remember where.

    If you still think you want to get a list of files sorted by last access timestamp for an entire folder hierarchy, then the following will work. Assume you want to list all files under “c:\test\”

    wmic datafile where "drive='c:' and path like '\\test\\%'" get name, lastaccessed | sort
    

    The timestamp will have the format YYYYMMDDhhmmssddddddZZZZ where

    • YYYY = year
    • MM = month
    • DD = day
    • hh = hour (24 hour format)
    • mm = minutes
    • ss = seconds
    • dddddd = micro seconds
    • ZZZZ = timezone, expressed as minutes difference from GMT (the 1st character is the sign)

    EDIT

    The wildcard search in WMIC causes terrible performance. Here is a version that iterates through all the folders in the root hierarchy, running WMIC against each specific folder (no wildcard). It has decent performance.

    @echo off
    setlocal disableDelayedExpansion
    set "root=c:\test"
    set "output=output.txt"
    
    set "tempFile=%temp%\dir_ts_%random%.txt"
    (
      for /d /r "%root%" %%F in (.) do (
        set "folder=%%~pnxF\"
        set "drive=%%~dF"
        setlocal enableDelayedExpansion
        2>nul wmic datafile where "drive='!drive!' and path='!folder:\=\\!'" get name, lastaccessed|findstr /brc:[0-9]
        endlocal
      )
    ) >"%tempFile%
    sort "%tempFile%" >"%output%"
    del "%tempFile%"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a Windows command file to open a webpage in IE,
I am trying to retrieve list of files from a server with the windows
I'm using Powershell on Windows XP and am trying to write a command that
I'm trying to write a windows service. It installs fine, but fails when I
Hi I am trying to write a windows virtual com port driver which will
I am trying to write a simple CLR-Windows Form Application. It will show a
I'm trying to write a little helper for Windows which eventually will accept a
I'm trying to write some C++ code for Windows 7 which will connect to
I am trying to write a simple application that runs on a Windows Mobile
I am trying to write a simple HTML 5 app for Windows Phone 7/7.5.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.