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

用于显示文件名和上次访问时间的Windows Shell命令,windows,batch-file,Windows,Batch File,我正在尝试编写一个Windows命令来列出文件及其上次访问时间,按访问时间排序 我用过 dir [directory] /O:D /T:A /S /B > output.txt 按上次访问时间顺序输出目录和子目录中的文件;但是我也需要它来输出最后的访问时间。这是如何实现的?在批处理文件中: >output.txt ( for /f "delims=" %%F in ('dir /o:d /t:a /s /b "c:\myPath\*"') @echo %%~tF %%F )

我正在尝试编写一个Windows命令来列出文件及其上次访问时间,按访问时间排序

我用过

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

按上次访问时间顺序输出目录和子目录中的文件;但是我也需要它来输出最后的访问时间。这是如何实现的?

在批处理文件中:

>output.txt (
  for /f "delims=" %%F in ('dir /o:d /t:a /s /b "c:\myPath\*"') @echo %%~tF %%F
)
但是,有些事情需要注意:

  • 文件在目录中按访问时间戳排序。它不会对所有目录按访问时间戳进行排序。您的原始代码也有同样的问题。要对accross目录进行排序,需要解析访问时间戳,并将其转换为字符串,当通过sort排序时,该字符串将按时间顺序进行排序。类似于“yyyy-mm-dd hh24:mm”的内容。即使这样也不是特别好,因为您没有访问秒的权限。您可以使用WMIC DATAFILE以次秒级别列出带有上次访问时间戳的文件名。但考虑到

  • Windows维护的上次访问时间戳不可靠!在许多情况下,应用程序可以读取文件,但最后的访问时间戳没有更新。我在某处看过一些关于这方面的参考资料,但我不记得在哪里

如果您仍然认为希望获得一个按整个文件夹层次结构的上次访问时间戳排序的文件列表,那么下面的操作将起作用。假设您想列出“c:\test”下的所有文件

时间戳的格式为
yyyymmddhhmmsddzzz
where

  • YYYY=年
  • 毫米=月
  • DD=天
  • hh=小时(24小时格式)
  • 毫米=分钟
  • ss=秒
  • dddd=微秒
  • ZZZ=时区,表示为与GMT的分钟差(第一个字符为符号)

编辑

WMIC中的通配符搜索会导致糟糕的性能。这是一个迭代根层次结构中所有文件夹的版本,针对每个特定文件夹运行WMIC(无通配符)。它的性能不错

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