且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Windows Shell中的命令显示文件名和上次访问时间

更新时间:2022-02-24 08:59:58

从批处理文件中:

>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:


  • 该文件被访问的时间戳 目录中的排序。它的排序在所有目录的访问时间戳。您原来的code有同样的问题。排序翻过目录需要解析访问时间戳,并将其转换成字符串时,通过SORT下令将按照时间顺序进行排序。像YYYY-MM-DD HH24:MM。即使是因为你没有访问秒也不是特别好。你可以使用WMIC DATAFILE以亚秒级列出与上次访问时间戳的文件名。但是何苦,考虑到...

  • 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...

由Windows维护上次访问时间戳不可靠!有许多情况,由此,应用程序可以读取一个文件,但在最后的访问的时间戳不更新。我见过一些地方参考的材料,谈到这一点,但我不记得在哪里。

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.

如果你还认为你想获得由整个目录最后一次访问的时间戳排序的文件列表,那么下面的工作。假设要列出下的所有文件C:\\测试\\

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

该时间戳的格式为 YYYYMMDDhhmmssddddddZZZZ 其中,


  • YYYY =年

  • MM =月

  • DD =日

  • HH =小时(24小时格式)

  • 毫米=分钟

  • SS =秒

  • DDDDDD =微秒

  • ZZZZ =时区,pssed为分钟从不同的前GMT $ P $(的第一个字符是符号)


修改


EDIT

在WM​​IC的通配符搜索将导致可怕的表现。这是通过在根层级中的所有文件夹的遍历,运行WMIC反目成仇特定的文件夹(无通配符)的一个版本。它有不俗的表现。

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%"