Aligator3000
Aligator3000

Reputation: 345

Copy files for backup and append date

I am using below script to append current date to a file when copying and making a backup. However, I have multiple excel files in the C:\Test directory. I would like to pass each filename as a variable, and append the date when making a copy to C:\Test\Archive\Processed directory.

@echo off

for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set DateTime=%%a

set Yr=%DateTime:~0,4%
set Mon=%DateTime:~4,2%
set Day=%DateTime:~6,2%

set BackupName=File Name__%Yr%-%Mon%-%Day%

copy "C:\Test\*" "C:\Test\Archive\Processed\%BackupName%.xlsx"

Desired output:

FilenameA__2019-03-20.xlsx
FilenameB__2019-03-20.xlsx
FilenameC__2019-03-20.xlsx
...

Many thanks in advance for your help!

Upvotes: 0

Views: 600

Answers (2)

aschipfl
aschipfl

Reputation: 34909

You should use a for loop, together with the ~ modifiers of the loop meta-variable:

for %%I in ("C:\Test\*.xlsx") do (
    copy "%%~I" "C:\Test\Archive\Processed\%%~nI__%Yr%-%Mon%-%Day%%%~xI"
)

Upvotes: 3

Dominique
Dominique

Reputation: 17491

Instead of your copy statement, put something like this (not tested):

FORFILES /M *.xlsx /C "cmd /c copy @file C:\Test\Archive\Processed\@fname_%BackupName%.xlsx"

Upvotes: 0

Related Questions