Windows 批处理脚本复制上次修改的文件
我正在尝试编写一个快速批处理脚本来查看一个文件的上次修改日期,并将其与其他几个文件的上次修改日期进行比较,如果它大于其他 Lastmods,它将文件复制到这些目录。这就是我到目前为止所得到的:
@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
方括号是完整路径名。它现在所做的是将lastmodSrc和lastmodDst保存为字符串(至少看起来它就是这样做的),所以它实际上并没有检查mod日期。不幸的是,我在 Windows 中的批处理脚本方面能力不足,认为这里的某人可能能够提供帮助。提前致谢!
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. I'm woefully inadequate at batch scripting in Windows, figured someone here might be able to help. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
另一种可能更简单的方法(假设我理解目标)是在 xcopy 上使用
/d
选项。如果给出了(没有日期),则仅当源较新时才会复制文件:Another approach that might be simpler (assuming I understand the goal) would be to use the
/d
option on xcopy. If that is given (without a date), it will copy the file only if the source is newer: