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

发布于 2024-12-06 03:47:15 字数 783 浏览 4 评论 0原文

我正在尝试编写一个快速批处理脚本来查看一个文件的上次修改日期,并将其与其他几个文件的上次修改日期进行比较,如果它大于其他 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉梦枕江山 2024-12-13 03:47:15

另一种可能更简单的方法(假设我理解目标)是在 xcopy 上使用 /d 选项。如果给出了(没有日期),则仅当源较新时才会复制文件:

xcopy /d srcfile dstfile

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:

xcopy /d srcfile dstfile
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文