Copy A file + Append Date to that file

softbless
softbless used Ask the Experts™
on
Dear All,

I need a bat file for this :

I have a folder that contain several files, for example Folder A with 3 files : Susan.log, Mitch.log, Bryan.log. OS : Windows XP

I want to copy those 3 files to folder B, but I need when copied the file name will append date time with format : yyyymmdd. With the yyyymmdd is fetched from the modified date of those 3 files.

So
File in Source Folder         Modified Date               The result of copy in Folder B
=================        ==============         =============================
Susan.log                           23-March-2010            Susan-20100323.log
Mitch.log                             18-Dec-2009                Mitch-20091218.log
Bryan.log                            7-Dec-2008                 Bryan-20081207.log

Please give me the bat file for these results : copy files from folder A to Folder B, with the files name including the modified date of the corresponding files.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Farhan KaziSystems Engineer
Top Expert 2007

Commented:
Check following batch script if this helps!

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=*" %%I IN ('DIR C:\DATA\*.log /B') DO (
	SET TDate=%%~tI
	SET MM=!TDate:~0,2!
	SET DD=!TDate:~3,2!
	SET YYYY=!TDate:~6,4!
	ECHO Coping "%%I"
	COPY "%%I" "D:\DATA\%%~nI_!YYYY!!MM!!DD!%%~xI">NUL)

Open in new window

Author

Commented:
Hi, it's not working.

I save your code in test.bat, and i run it from CMD

It said : copying... (see image)

But in d:\data there are no files copied.

Please help. Thank you.
cmd-printscreen.jpg
Systems Engineer
Top Expert 2007
Commented:
Can you try following and post the result:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /R C:\DATA %%I IN (*.log) DO (
        SET TDate=%%~tI
        SET MM=!TDate:~0,2!
        SET DD=!TDate:~3,2!
        SET YYYY=!TDate:~6,4!
        ECHO Copying %%I AS D:\DETA\%%~nI_!YYYY!!MM!!DD!%%~xI
	COPY "%%I" "D:\DATA\%%~nI_!YYYY!!MM!!DD!%%~xI")

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial