Windows 批处理脚本:比较两个文件的创建日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6735704/
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
Windows batch scripting: compare two files' created dates
提问by raddevon
I would like to fork my Windows batch script based on a comparison of the created dates of two files, and I'm not sure where to begin. I feel like there must be a way. Any ideas?
我想根据两个文件的创建日期的比较来分叉我的 Windows 批处理脚本,但我不确定从哪里开始。我觉得一定有办法。有任何想法吗?
UPDATE:Tried the solution in PA's answer. I copied the code snippet verbatim to the end of my current script. Then, I added this early in the script:
更新:在 PA 的回答中尝试了解决方案。我将代码片段逐字复制到当前脚本的末尾。然后,我在脚本的早期添加了这个:
IF EXIST "%PROGRAMFILES(X86)%" CALL :getCreationDate "%PROGRAMFILES(X86)%\oracle\jinitiator 1.3.1.28\lib\security\certdb.txt"
which gives me an error when I execute: Invalid alias verb.
这在我执行时给了我一个错误:无效的别名动词。
回答by Michael South
You need to put a caret before the equals sign to escape it (cmd.exe is sooo wonderful). I've verified this works:
您需要在等号前放置一个插入符号才能将其转义(cmd.exe 太棒了)。我已经验证这有效:
setlocal enableextensions enabledelayedexpansion
call :getCreationDate "C:\Windows\Notepad.exe"
echo Creation Date is: %creationdate%
endlocal
goto :EOF
:getCreationDate
set FILE=%~f1
set FILE=%FILE:\=\%
for /F "skip=1 tokens=* usebackq" %%A in (`wmic datafile where name^="%FILE%" get creationdate`) do (
set creationdate=%%A
)
goto :EOF
回答by PA.
In a bat you can get the creation date of a file with WMIC DATAFILE
command, using the GET CREATIONDATE
verb.
在 bat 中,您可以WMIC DATAFILE
使用GET CREATIONDATE
动词通过命令获取文件的创建日期。
You need to capture the output of the command into a variable, see HELP FOR
and HELP SET
.
您需要将命令的输出捕获到一个变量中,请参阅HELP FOR
和HELP SET
。
You can use :label
and GOTO :eof
to create a function that puts together this functionality.
您可以使用:label
和GOTO :eof
来创建一个将该功能组合在一起的函数。
Notice that for WMIC DATAFILE
, the WHERE NAME=
clause requires a fully specified filename. See HELP CALL
and the %~f
modifier.
请注意,对于WMIC DATAFILE
,该WHERE NAME=
子句需要完全指定的文件名。请参阅HELP CALL
和%~f
修饰符。
Notice also that WMIC DATAFILE WHERE NAME=
requires doubling the backslashes in the filename. See HELP SET
and the % : = %
syntax for replacing single backslashes with double backslashes.
另请注意,这WMIC DATAFILE WHERE NAME=
需要将文件名中的反斜杠加倍。请参阅HELP SET
和% : = %
用双反斜杠替换单反斜杠的语法。
something like this.....
像这样的东西......
:getCreationDate
set FILE=%~f1
set FILE=%FILE:\=\%
FOR /F "skip=1 tokens=* usebackq" %%A IN (`wmic datafile where name="%FILE%" get creationdate`) DO (
SET CREATIONDATE=%%A
)
goto :eof
You will need to use CALL :label
for invoking it.
您将需要使用CALL :label
来调用它。
CALL :getCreationDate myfile.txt
You'll need to extract the part of the datetime you are interested in compating. See HELP SET
using the ~
modifier.
您需要提取您感兴趣的日期时间部分。请参阅HELP SET
使用~
修饰符。
Finally, you'll need to compare the returned datefiles. See HELP IF
.
最后,您需要比较返回的日期文件。见HELP IF
。
回答by ovadia
try this:
尝试这个:
wmic datafile where name='c:\users\ovadia\test\addx.txt' get 'LAST MODIFIED' > dateofNEWadd.txt
wmic datafile where name='c:\users\ovadia\test\prevaddx.txt' get 'LAST MODIFIED' > dateofOLDadd.txt
fc /LB1 dateofNEWadd.txt dateofOLDadd.txt
if errorlevel 1 echo "fc err not 0"
del dateof*
attributes for the 'get' may be...
'get' 的属性可能是...
Access Rights,
Caption,
Class Name,
Compressed,
Compression Method,
Computer System Class Name,
Computer System Name,
Creation Date,
Current File Open Count,
Description,
Drive,
Eight Dot Three File Name,
Encrypted,
Encryption Method,
File Extension,
File Name,
File System Class Name,
File System Name,
File Type,
Hidden,
Install Date,
Last Accessed,
Last Modified,
Manufacturer,
Name,
Path,
Readable,
Should Be Archived,
Size,
Status,
System File,
Version,
Writeable