0

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
Max
  • 91
  • 1
  • 2
  • 9
  • What's the exact content of the `%dat%` variable? – Stephan Mar 08 '22 at 20:06
  • Probably your %date% variable contains spaces. If so, you could convert `dat` using `set "dat=%dat: =0%"` to convert the spaces to 0s. Or you could put the destination in "quotes" - that is, `"\\srvditz1\DataXchange_R3\TUS_EXCHANGE\TUS300_digi_SFM\30_Visualization\Archive\%DAT%_ZPP00138_TUS350.csv"` - the quotes instruct `cmd` to interpret the spaces as part of the name instead of being argument-separators. – Magoo Mar 08 '22 at 20:22
  • @Stephan %DAT% is today date taken from the windows system, you can see in the code where I declare it `SET DAT` – Max Mar 08 '22 at 20:49
  • I can read the code, that's why I asked for the exact content of the variable. `%date%` and `%time%` are dependent on user settings, see I can't guess. – Stephan Mar 08 '22 at 21:03
  • @Stephan In my system the current date is: Tue 03/08/2022 – Max Mar 08 '22 at 21:09
  • substring substitution character count is zero based. Correct the counters or better switch to a method [independent of local settings](https://stackoverflow.com/questions/7727114/batch-command-date-and-time-in-file-name/18024049#18024049) – Stephan Mar 08 '22 at 21:13
  • @Magoo I have tried to place quotes around the destination, it now just says "The system cannot find the path specified". I presume it might be related to the fact that it cannot find the destination file where to place this or something like that. Perhaps I need to create a blank file with some name in the destination folder as well? I was under the assumption it should just create new files every time, just with different names rather than overwrite anything – Max Mar 08 '22 at 21:15
  • Given your date format, the `dat` would be set to `Tu 0/08/2022` which is an illegal name. You would need `4,2` for the month, `7,2` for the day and `-4` for the year. Counting from the first character (character 0) 4 for 2 characters, 7 for 2 characters and the last 4. – Magoo Mar 08 '22 at 21:27

2 Answers2

0

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

ooLi
  • 63
  • 6
0
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.

Mahendra
  • 447
  • 6
  • 7