2

Suppose I have two directories: N:\test1 and N:\archive.

I want to move any .csv files from test1 to archive.

I want to archive the files, as now they are being overwritten, so I thought of adding a timestamp.

I am following some sort of logic like this so far, but not sure how do I append the timestamp to the file name:

REM TimeStamp
for /f "tokens=2-8 delims=.:/ " %%a in ("%date% %time%") do set DateNtime=%%c-%%a-%%b_%%d%%e%%f

REM move the file
move /Y N:\test1\*.csv N:\archive

REM Rename the file with timestamp
ren N:\archive\*.csv *_%DateNtime%.csv

This is the part I´m stuck at: ren N:\archive\*.csv *_%DateNtime%.csv

As you can imagine, there'll be existing .csv archived files from the first time, so how do I tell it to rename the file the was just moved?

For example, suppose there's file1.csv in test1 folder. After it's moved to archive, it should be renamed to file1_%DateNtime%.csv.

Note: N:\ is a UNC/mounted shared drive

Cataster
  • 3,081
  • 5
  • 32
  • 79
  • Just move and rename at once: `move /Y "C:\test1\*.csv" "C:\archive\*_%DateNtime%.csv"`... – aschipfl Oct 10 '19 at 21:20
  • @aschipfl i get an error when running the batch file: The filename, directory name, or volume label syntax is incorrect. – Cataster Oct 10 '19 at 21:26
  • You cannot have `:` in file names... – aschipfl Oct 10 '19 at 21:27
  • @aschipfl i removed the colon but still getting the error – Cataster Oct 10 '19 at 21:31
  • 1
    Please read the answers on [How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?](https://stackoverflow.com/questions/203090/) There are much better methods to get date/time __region independent__ in the date/time format you would like, e.g. with using `wmic` or `robocopy`. The solution using `wmic` working also on Windows XP on which `robocopy` is by default not available is explained by me in detail at [Why does %date% produce a different result in batch file executed as scheduled task?](https://stackoverflow.com/a/44670322/3074564) – Mofi Oct 11 '19 at 05:45
  • I recommend reading the Microsoft article [Naming Files, Paths, and Namespaces](https://learn.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file). It explains which characters cannot be used in file/folder names like the colon which is used as separator between drive letter and a file/folder name. And I suggest reading [debugging a batch file](https://stackoverflow.com/a/42448601/3074564). It should be no problem to see what causes the error on running the batch file without `@echo off` from within a command prompt window instead of double clicking on batch file. – Mofi Oct 11 '19 at 05:46
  • @Mofi I have went through some suggestions and other posts but to no avail. Could it be because I'm using UNC path it's not gonna work with move? – Cataster Oct 11 '19 at 14:13
  • The usage of UNC paths instead of `N:` is definitely no problem. But missing `"` around source argument string and around destination argument string could be a problem if real paths contain a space or one of these characters ``&()[]{}^=;!'+,`~``. The account used to run this batch file needs of course the permissions to read the source files and write the destination files on network resource accessed using UNC path. See also: [What must be taken into account on executing a batch file as scheduled task?](https://stackoverflow.com/a/41821620/3074564) – Mofi Oct 11 '19 at 14:30

1 Answers1

0

here is the way we ended up doing it:

SET today=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%%TIME:~9,2%
SET today=%today: =0%

REM the second SET today was required because there was some weird execution with the 
REM timestamp happening at 8:00 am. something wrong with the formatting so the files were 
REM never getting timestamped at that time, but by adding that line the issue was resolved.

copy /y N:\test1\file1.csv N:\archive\file1_%today%.csv
Cataster
  • 3,081
  • 5
  • 32
  • 79