Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch file copy and paste with current date 1

Status
Not open for further replies.

iuianj07

Programmer
Sep 25, 2009
293
US
Hello guys,

I am not sure if I am on the right forum section for this question, if not please lead to the right direction. Thanks


I am trying to create a batch file, for archiving purposes, to copy a file and paste into another folder, I created this code from notepad and it correctly copies and paste the said file into another folder:

xcopy "[source folder]" "[destination folder]" /Y/Q

What I would like to try though is that when it is pasted into the destination folder, is there a way that I could rename the file to add today's date? so example test.xls is the file I want to copy, when it's copied into the destination folder, the file name should be test7/15/2010.xls

Is there a way to automatically add the date, or anything that could have a work around for that?

Thanks!
 
Here's a starting point, change the filename/directories as required

set day=%date:~-10,2%
set mnth=%date:~-7,2%
set yr=%date:~-2,2%
if %mnth%==01 set mnth=JAN
if %mnth%==02 set mnth=FEB
if %mnth%==03 set mnth=MAR
if %mnth%==04 set mnth=APR
if %mnth%==05 set mnth=MAY
if %mnth%==06 set mnth=JUN
if %mnth%==07 set mnth=JUL
if %mnth%==08 set mnth=AUG
if %mnth%==09 set mnth=SEP
if %mnth%==10 set mnth=OCT
if %mnth%==11 set mnth=NOV
if %mnth%==12 set mnth=DEC
set dte=%day%%mnth%%yr%

xcopy c:\test\test1.txt c:\test1\test1_%dte%.txt


In order to understand recursion, you must first understand recursion.
 
Hello taupirho,

Thanks for your help, it does add the current date after the file name, I have follow up questions though :)

When I run the batch file, a command prompt shows up and I need to press F key to create the new file, is there a way that it would automatically create the file without asking if I need to press F or D? Thanks!

And also, just want some explanation about the code you wrote below:

set day=%date:~-10,2%
set mnth=%date:~-7,2%
set yr=%date:~-2,2%

what does ~-10,2% ~-7,2% and ~2,2% mean?

Thanks again!
 
what does ~-10,2% ~-7,2% and ~2,2% mean?
In your console windows type the following:
set /?
 
hello PHV,

sorry for the silly question, but what/where is console windows? :)

thanks
 
Hey PHV,

I know what console windows is now, its the command prompt! just got used it to be called command prompt instead of console windows.

thanks
 
You can avoid this prompt by echoing an F and piping it to the xcopy command e.g

echo F | xcopy ........

To explain what the date stuff is doing you need to know that DOS keeps a bunch of dynamic environment variables on the go. One of these is %date% which should contain todays date

When I do an echo %date% on my system it comes back as

15/07/2010

What set day=%date:~-10,2% says is start at the end of whatever value %date% holds and count back 10 characters, then get the next 2 characters from there and store them in the day variable



In order to understand recursion, you must first understand recursion.
 
hello taupirho,

Thanks for the time answering my questions! I appreciate it :)

the batch file is working now and with the echo F

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top