简体   繁体   English

Windows批处理脚本复制文件上次修改

[英]Windows batch script copy file last modified

I'm trying to write a quick batch script to look at the last modified date of one file and compare it to the last modified date of a few others, and if it's greater than those other lastmods, it copies the files to those directories. 我正在尝试编写一个快速批处理脚本,以查看一个文件的上次修改日期,并将其与其他几个文件的上次修改日期进行比较,如果它大于其他上次修改的日期,它将文件复制到这些目录中。 This is what I have so far: 这是我到目前为止的内容:

@echo off
for %%a in ([srcFile]) do set lastmodSrc=%%~ta
echo lastmodSrc
for %%a in ([dstFile1]) do set lastmodDst1=%%~ta
for %%a in ([dstFile2]) do set lastmodDst2=%%~ta
for %%a in ([dstFile3]) do set lastmodDst3=%%~ta
for %%a in ([dstFile4]) do set lastmodDst4=%%~ta

if lastmodSrc GTR lastmodDst1 xcopy [srcFile] [dstDir1] /-y
if lastmodSrc GTR lastmodDst2 xcopy [srcFile] [dstDir2] /-y
if lastmodSrc GTR lastmodDst3 xcopy [srcFile] [dstDir3] /-y
if lastmodSrc GTR lastmodDst4 xcopy [srcFile] [dstDir4] /-y

pause

The square brackets are full path names. 方括号是完整的路径名。 What it's doing right now is saving lastmodSrc and lastmodDst as just the strings (at least that's what it seems like it's what it's doing), and so it's not actually checking the mod dates. 现在它正在做的是将lastmodSrc和lastmodDst保存为字符串(至少看起来是这样),因此实际上并没有检查mod日期。 I'm woefully inadequate at batch scripting in Windows, figured someone here might be able to help. 我对Windows中的批处理脚本非常不满意,认为这里有人可以提供帮助。 Thanks in advance! 提前致谢!

Another approach that might be simpler (assuming I understand the goal) would be to use the /d option on xcopy. 另一种可能更简单的方法(假设我了解目标)将是在xcopy上使用/d选项。 If that is given (without a date), it will copy the file only if the source is newer: 如果指定了日期(无日期),则仅在源较新时才复制文件:

xcopy /d srcfile dstfile

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

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