如何在 Windows 命令行上获取文件的上次修改日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2111333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get file's last modified date on Windows command line?
提问by Ricky Supit
I have been using the following command to get the file date. However, the fileDate
variable has been returning blank value ever since we moved to a different server (Windows Server 2003).
我一直在使用以下命令来获取文件日期。但是,fileDate
自从我们移动到不同的服务器 (Windows Server 2003) 以来,该变量一直返回空白值。
FOR /f %%a in ('dir myfile.txt^|find /i " myfile.txt"') DO SET fileDate=%%a
Is there any other more reliable way to get the file date?
还有其他更可靠的方法来获取文件日期吗?
回答by Andy Morris
Change %
to %%
for use in batch file, for %~ta
syntax enter call /?
更改%
到%%
在批处理文件中使用,对于%~ta
语法进入call /?
for %a in (MyFile.txt) do set FileDate=%~ta
Sample output:
示例输出:
for %a in (MyFile.txt) do set FileDate=%~ta
set FileDate=05/05/2020 09:47 AM
for %a in (file_not_exist_file.txt) do set FileDate=%~ta
set FileDate=
回答by user2607028
You can do it
你能行的
forfiles /M myfile.txt /C "cmd /c echo @fdate @ftime"
回答by PodTech.io
Useful reference to get file properties using a batch file, included is the last modified time:
使用批处理文件获取文件属性的有用参考,包括上次修改时间:
FOR %%? IN ("C:\somefile\path\file.txt") DO (
ECHO File Name Only : %%~n?
ECHO File Extension : %%~x?
ECHO Name in 8.3 notation : %%~sn?
ECHO File Attributes : %%~a?
ECHO Located on Drive : %%~d?
ECHO File Size : %%~z?
ECHO Last-Modified Date : %%~t?
ECHO Drive and Path : %%~dp?
ECHO Drive : %%~d?
ECHO Fully Qualified Path : %%~f?
ECHO FQP in 8.3 notation : %%~sf?
ECHO Location in the PATH : %%~dp$PATH:?
)
回答by aschipfl
To get the last modification date/time of a file in a locale-independentmanner you could use the wmic
commandwith the DataFile
alias:
要以与语言环境无关的方式获取文件的最后修改日期/时间,您可以使用带有别名的wmic
命令DataFile
:
wmic DataFile where "Name='D:\Path\To\myfile.txt'" get LastModified /VALUE
Regard that the full path to the file must be provided and that all path separators (backslashes \
) must be doubled herein.
请注意,必须提供文件的完整路径,并且所有路径分隔符(反斜杠\
)在此处必须加倍。
This returns a standardised date/time value like this (meaning 12thof August 2019, 13:00:00, UTC + 120'):
这将返回这样的标准化日期/时间值(意味着2019 年 8 月12日,13:00:00,UTC + 120'):
LastModified=20190812130000.000000+120
LastModified=20190812130000.000000+120
To capture the date/time value use for /F
, then you can assign it to a variable using set
:
要捕获日期/时间值 use for /F
,然后您可以使用以下方法将其分配给变量set
:
for /F "delims=" %%I in ('
wmic DataFile where "Name='D:\Path\To\myfile.txt'" get LastModified /VALUE
') do for /F "tokens=1* delims==" %%J in ("%%I") do set "DateTime=%%K"
The second for /F
loop avoids artefacts (like orphaned carriage-return characters) from conversion of the Unicode output of wmic
to ASCII/ANSI text by the first for /F
loop (see also this answer).
第二个for /F
循环避免wmic
了第一个for /F
循环将 Unicode 输出转换为 ASCII/ANSI 文本时的人工制品(如孤立的回车符)(另请参阅此答案)。
You can then use sub-string expansionto extract the pure date or the time from this:
然后,您可以使用子字符串扩展来从中提取纯日期或时间:
set "DateOnly=%DateTime:~0,8%"
set "TimeOnly=%DateTime:~8,6%"
To get the creation date/time or the last access date/time, just replace the property LastModified
by CreationDate
or LastAccessed
, respectively. To get information about a directory rather than a file, use the alias FSDir
instead of DataFile
.
要获得创建日期/时间或最后访问日期/时间,只需更换物业LastModified
的CreationDate
或LastAccessed
分别。要获取有关目录而不是文件的信息,请使用别名FSDir
而不是DataFile
.
For specifying file (or directory) paths/names containing both ,
and )
, which are usually not accepted by wmic
, take a look at this question.
要指定包含,
和)
通常不被接受的文件(或目录)路径/名称wmic
,请查看此问题。
Check out also this postas well as this oneabout how to get file and directory date/time stamps.
回答by Cheeso
It works for me on Vista. Some things to try:
它在 Vista 上对我有用。一些尝试:
Replace
find
with the fully-qualified path of the find command.find
is a common tool name. There's a unix find that is very differet from the Windows built-in find. like this:FOR /f %%a in ('dir ^|%windir%\system32\find.exe /i "myfile.txt"') DO SET fileDate=%%a
examine the output of the command in a cmd.exe window. To do that, You need to replace the %% with %.
FOR /f %a in ('dir ^|c:\windows\system32\find.exe /i "myfile.txt"') DO SET fileDate=%a
That may give you some ideas.If that shows up as blank, then again, at a command prompt, try this:
dir | c:\windows\system32\find.exe /i "myfile.txt"
替换
find
为 find 命令的完全限定路径。find
是一个常用的工具名称。有一个 unix find 与 Windows 内置 find 非常不同。像这样:FOR /f %%a in ('dir ^|%windir%\system32\find.exe /i "myfile.txt"') DO SET fileDate=%%a
在 cmd.exe 窗口中检查命令的输出。为此,您需要将 %% 替换为 %。
FOR /f %a in ('dir ^|c:\windows\system32\find.exe /i "myfile.txt"') DO SET fileDate=%a
这可能会给你一些想法。如果显示为空白,则再次在命令提示符下尝试以下操作:
dir | c:\windows\system32\find.exe /i "myfile.txt"
This should show you what you need to see.
这应该会显示您需要查看的内容。
If you still can't figure it out from that, edit your post to include what you see from these commands and someone will help you.
如果您仍然无法从中弄清楚,请编辑您的帖子以包含您从这些命令中看到的内容,有人会帮助您。
回答by ghostdog74
you can get a files modified date using vbscript too
您也可以使用 vbscript 获取文件修改日期
Set objFS=CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strFile= objArgs(0)
WScript.Echo objFS.GetFile(strFile).DateLastModified
save the above as mygetdate.vbs and on command line
将上述内容另存为 mygetdate.vbs 并在命令行上
c:\test> cscript //nologo mygetdate.vbs myfile
回答by BlueRaja - Danny Pflughoeft
What output (exactly)does dir myfile.txt
give in the current directory? What happens if you set the delimiters?
当前目录中的输出(确切地说)是dir myfile.txt
什么?如果设置分隔符会发生什么?
FOR /f "tokens=1,2* delims= " %%a in ('dir myfile.txt^|find /i " myfile.txt"') DO SET fileDate=%%a
(note the space after delims=
)
(to make life easier, you can do this from the command line by replacing %%a
with %a
)
(注意后面的空格delims=
)
(为了让生活更轻松,您可以从命令行替换%%a
为%a
)