Windows Shell中的命令显示文件名和上次访问时间 [英] Windows Shell Command to Show File Name and Last Access Time

查看:357
本文介绍了Windows Shell中的命令显示文件名和上次访问时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个Windows命令列出的文件和上次访问时间,根据访问时间排序。

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

我用

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?

推荐答案

从批处理文件中:

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

这篇关于Windows Shell中的命令显示文件名和上次访问时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆