I am trying to copy all *.csv
files from a folder to another folder, but while copying I need to add modified date and time of file to filename. For example, if filename is Test.csv
and it was modified on 11/21/2018
15:01:10
output should be Test11-21-201815_01-10.csv
. I found a script to add current timestamp to it, but I need to add modified date of file. You can see it below:
@echo off
set Source=C:\csvtest
set Target=C:\csvtest\csvtest\Archive
FOR /f "tokens=1-8 delims=/.:- " %%A in ("%date%%time%") DO (
SET Month=%%B
SET Day=%%C
SET Year=%%D
SET Hours=%%E
SET Minutes=%%F
SET Seconds=%%G
SET All=%%B-%%C-%%D_%%E-%%F-%%G
)
FOR %%i IN ("%Source%\*.csv") DO (
COPY "%%i" "%Target%\%%~Ni %All%.csv")
Thanks in advance for your help.
There are some examples here on SO for appending a date,
but I'd use PowerShell as a tool to accomplish this (wrapped in batch).
:: Q:\Test\2018\11\23\SO_53450598.cmd
@echo off
set "Source=C:\test"
set "Target=C:\test\Archive"
PowerShell -Nop -C "Get-ChildItem '%Source%\*.csv'|Copy-Item -Destination {'%Target%\{0} {1:MM-dd-yyyy_HH-mm-ss}.csv' -f $_.BaseName,$_.LastWriteTime} -WhatIf"
If the output looks OK, remove the -WhatIf
at the end.