批处理脚本以获取文件修改信息



我正在尝试编写一个在XP机器上使用的批处理脚本。我基本上想获得特定文件的文件修改信息,并将其输出到一个文件中。

以下是我迄今为止所写的内容:

SET filename="C:UsersxxxDesktoptestfile.txt"
 if exist %filename% (
    for %%A in (%filename%) DO (SET "bodytext=%bodytext%testfile.txt updated at %%~tA")
)
  else (
   SET "bodytext=%bodytext%Warning no file exists." 
 )
echo %bodytext% > results.txt

当我运行它时,它会将更新后的文件名和时间写入results.txt,但也会将"警告不存在文件"写入results.txt?

如果有人能帮我,我将不胜感激?

感谢

)
  else (

这必须全部在一条线上

)其他(

如果在"for"命令内设置变量,则必须使用"setlocal enabledelayedexpansion"。

@echo off
setlocal enabledelayedexpansion
SET filename="C:filename.txt"
if exist %filename% (
    for %%a in (%filename%) do (
        set filedate=%%~ta
        set bodytext=%filename% last updated at !filedate!
    )
) else (
    set bodytext=%filename% No File Exist.
)
echo %bodytext%>c:results.txt

最新更新