如何在 CMD 脚本中复制和重命名文件并附加日期/版本号

发布于 2025-01-12 14:33:09 字数 786 浏览 1 评论 0原文

我有一个 CMD 脚本,当前将文件从一个位置复制到另一个位置。它会覆盖目标源中具有该名称的旧文件,这很好。现在,我想在存档中保留历史数据(具有不同名称的单独 csv)。我试图在用新数据更新文件之前将现有文件复制到存档中。我的想法是创建 Archive 文件夹并将原始文件复制到其中。但是,根据我现在的设置,它将删除旧版本的文件,因为它们会应用相同的名称。

我尝试添加 DAT 变量(即当前日期)并将其附加到文件名的开头,但它提示语法错误。不确定这在 CMD 中是否可行。我真的很感激一些帮助。如果您从代码中取出 DAT ,它将正常工作并复制文件,但下次我运行此脚本时,它将覆盖该文件,而我想要名称包含以下内容的不同历史文件带有标识符的日期。如果无法附加日期,也许我们可以自己从 1 开始创建版本号。

这是我尝试过的代码:

@ECHO OFF


set day=%date:~0,2%
set month=%date:~3,2%
set year=%date:~6%

SET DAT=%DATE:~6%%DATE:~3,2% 
Set ZEIT=%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%

copy /Y \\srvditz1\DataXchange_R3\TUS_EXCHANGE\TUS300_digi_SFM\30_Visualization\ZPP00138_TUS350.csv \\srvditz1\DataXchange_R3\TUS_EXCHANGE\TUS300_digi_SFM\30_Visualization\Archive\%DAT%_ZPP00138_TUS350.csv

I have a CMD script that currently copies file from one location to another. It overwrites the old file with that name in the destination source which is fine. Now, I would like to keep historic data available in the Archive (separate csvs with different names). I am trying to copy the existing file to the archive before the file is updated with new data. What I thought is to create Archive folder and copy original file to it. However, with the setup I have now, it will be erasing the older version of the file because they would get the same name applied.

I tried adding DAT variable which is a current date and append this in the beginning of the file name but it prompted a syntax error. Not sure if this is even possible in CMD. I would really appreciate some assistance. If you take DAT out of the code, it will work and copy the file fine but the next time I run this script, it will overwrite the file while I want to have different historic files with name containing a date with identifier. If appending date is not possible, perhaps we could create version number by ourselves starting from 1.

Here is the code that I tried:

@ECHO OFF


set day=%date:~0,2%
set month=%date:~3,2%
set year=%date:~6%

SET DAT=%DATE:~6%%DATE:~3,2% 
Set ZEIT=%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%

copy /Y \\srvditz1\DataXchange_R3\TUS_EXCHANGE\TUS300_digi_SFM\30_Visualization\ZPP00138_TUS350.csv \\srvditz1\DataXchange_R3\TUS_EXCHANGE\TUS300_digi_SFM\30_Visualization\Archive\%DAT%_ZPP00138_TUS350.csv

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

零度℉ 2025-01-19 14:33:09

该行在行尾添加了一个额外的空白,这会给您带来麻烦。
设置 DAT=%日期:~6%%日期:~3,2%

This line adds an extra blank at the end of the line which causes your trouble.
SET DAT=%DATE:~6%%DATE:~3,2%

夜访吸血鬼 2025-01-19 14:33:09
copy /Y \\srvditz1\DataXchange_R3\TUS_EXCHANGE\TUS300_digi_SFM\30_Visualization\ZPP00138_TUS350.csv \\srvditz1\DataXchange_R3\TUS_EXCHANGE\TUS300_digi_SFM\30_Visualization\Archive\%DATE:~6%%DATE:~3,2%%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%_ZPP00138_TUS350.csv

如果您直接放置而不添加任何变量,则这是有效的。

copy /Y \\srvditz1\DataXchange_R3\TUS_EXCHANGE\TUS300_digi_SFM\30_Visualization\ZPP00138_TUS350.csv \\srvditz1\DataXchange_R3\TUS_EXCHANGE\TUS300_digi_SFM\30_Visualization\Archive\%DATE:~6%%DATE:~3,2%%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%_ZPP00138_TUS350.csv

If you put directly without any variable this works.

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