Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Shell Command to Show File Name and Last Access Time

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?

like image 464
michael_clarke Avatar asked Jul 30 '12 20:07

michael_clarke


People also ask

Which DOS command list files by date?

The dir command is used to list the files and subdirectories in a directory. The "/O:D" with the dir command is used to list the files by the date of the modification from the oldest first.

What is dir command in CMD?

For files, this command displays the name extension and the size in bytes. This command also displays the total number of files and directories listed, their cumulative size, and the free space (in bytes) remaining on the disk. The dir command can also run from the Windows Recovery Console, using different parameters.


1 Answers

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%"
like image 169
dbenham Avatar answered Oct 21 '22 22:10

dbenham