Jaanna
Jaanna

Reputation: 1670

adding dates to copied files

I am trying to add the dates to copied files from one directory to another directory. This is how it should look like

Original file name: XEsalary.csv

Result file name: XEsalary-2013-02-15.csv

Here is my code:

set REMOTE=U:\ 
set LOG=C:\Integration\FE\log_test 
set PVM=%DATE:~9,4%-%DATE:~6,2%-%DATE:~3,2%
set YY=%DATE:~9,4% 
set LOCAL=C:\FTP\VC\test
cd %LOCAL%

xcopy /y /f /v "%LOCAL%\*.csv" "%REMOTE%\" >>%LOG%\%PVM%.log 
xcopy /y /f /v "%LOCAL%\*.csv" "%LOCAL%\archive\*.csv"  
:: assist with turning this into a for loop
ren %LOCAL%\archive\*.csv %LOCAL%\archive\*%PVM%.csv 
echo. >>%LOG%\%PVM%.log

copying works just file. It is the renaming part which is not working. Any help please?

Thx in advance :-)

Upvotes: 0

Views: 165

Answers (2)

dbenham
dbenham

Reputation: 130819

Using wildcards with RENAME is tricky. There is no good official documentation, but I have experimented and published rules as to how it works: See How does the Windows RENAME command interpret wildcards?

You can accomplish your rename with the following command, as long as none of your file names include dots (the last extension dot is OK).

ren "%LOCAL%\archive\*.csv" ????????????????????-%PVM%*

The number of ? must be greater than or equal to the longest name in the folder.

The result will be incorrect if a file name contains a dot. For example, part1.part2.csv would become part1-2013-02-15.part2.csv.

Another option is to use a FOR loop. You can safely append the date to any file using this technique.

for %%F in ("%LOCAL%\archive\*.csv") do ren "%%F" "%%~nF-%PVM%%%~xF"

BUT, both solutions have a problem if you rerun the process on a later date. New files will be added to the archive, but both new and old archive files will be renamed to the current date. That won't work.

Better to copy and rename the file in one step using a FOR loop as suggested in rojo's answer.

Or better yet, create a new archive folder with the date in the folder name, and then copy the files to the dated archive folder without renaming :-)

set REMOTE=U:\ 
set LOG=C:\Integration\FE\log_test 
set PVM=%DATE:~9,4%-%DATE:~6,2%-%DATE:~3,2%
set YY=%DATE:~9,4% 
set LOCAL=C:\FTP\VC\test
cd %LOCAL%

xcopy /y /f /v "%LOCAL%\*.csv" "%REMOTE%\" >>%LOG%\%PVM%.log 
md "%LOCAL%\archive\%PMV%
xcopy /y /f /v "%LOCAL%\*.csv" "%LOCAL%\archive\%PVM%"

You might want to include the time in the folder name if the process could be run multiple times in the same day.

Upvotes: 2

rojo
rojo

Reputation: 24466

Windows doesn't let you rename with a wildcard. You have to name each file in a for loop. Might as well do the renaming as you're copying. Does this do what you intended?

@echo off
setlocal
set REMOTE=U:\ 
set LOG=C:\Integration\FE\log_test 
set PVM=%DATE:~9,4%-%DATE:~6,2%-%DATE:~3,2%
set YY=%DATE:~9,4% 
set LOCAL=C:\FTP\VC\test

xcopy /y /f /v "%LOCAL%\*.csv" "%REMOTE%\" >>%LOG%\%PVM%.log

pushd "%LOCAL%"
for %%I in (*.csv) do (
    rem Uncomment the following line to log the copy to archive.
    rem echo copy "%%I" -^> "%LOCAL%\archive\%%~nI-%PVM%.csv" >>%LOG%\%PVM%.log
    copy "%%I" "%LOCAL%\archive\%%~nI-%PVM%.csv">NUL
)
echo. >>%LOG%\%PVM%.log
popd

Upvotes: 2

Related Questions