'How to copy and rename file in CMD script with a date/version number appended to it

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


Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ooLi