简体   繁体   English

如何在批处理文件中回显文件的最后修改时间?

[英]How can I echo a files last modified time in a batch file?

I have this code that echos if a file has been modified today or not. 我有这段代码可以回显文件是否已被修改。 How can I add so it can echo not only that the file was modified today, but also at what time? 我该如何添加,使其不仅可以回显文件今天已被修改,还可以回声什么时候?

Like for example "File modified today at %modifytime%". 例如,“今天在%modifytime%修改的文件”。

@echo off
for %%F in (C:\temp\file.txt) do (for /F %%D in ("%%~tF") do (set mdate=%%D))
for /F "tokens=2" %%D in ('date/t') do set cdate=%%D
if "%date%"=="%mdate%" echo File modified today
if not "%date%"=="%mdate%" echo File not modified today

Here's an quick idea using the built-in ForFiles command: 这是使用内置的ForFiles命令的快速提示:

@Echo Off
For %%A In ("C:\temp\file.txt") Do (If Not "%__CD__%"=="%%~dpA" PushD "%%~dpA"
    ForFiles /M "%%~nxA" /D 0 /C^
    "Cmd /C Echo @File was modified today0x09@fDate @fTime"2>Nul||(
    Echo "%%A" not modified today))
Pause

As with your example, this idea allows you to replace the single file between the parentheses on line 3 with: 与您的示例一样,此想法使您可以将第3行的括号之间的单个文件替换为:

  • * to output for every file in the current directory. *输出当前目录中的每个文件。
  • "C:\\temp\\file1.txt" "C:\\somewhere\\fileZ.exe" to output for multiple known files. "C:\\temp\\file1.txt" "C:\\somewhere\\fileZ.exe"输出多个已知文件。
    Special Note: The use of PushD without a PopD may restrict the number of different directories; 特别说明:在没有PushD情况下使用PopD可能会限制不同目录的数量; if using this option please use sparingly. 如果使用此选项,请谨慎使用。

For safety, I recommend your entries between those parentheses be double quoted. 为了安全起见,建议您在这些括号之间输入双引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM