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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

copy a file to a name including todays date 2

Status
Not open for further replies.

tomk01

ISP
Oct 18, 2004
69
US
As part of our back policy I need to copy or rename a file to a name that includes todays date. i.e. I have a file backup.txt that i want to change to 5/6/05backup.txt.
I though of using a batch file but I can't find the right commands.
 
OK I have a partial answer which might help but you may need to modify it to suit. I'm not sure the name you suggest is legal anyway.
First you create a temporary file with the date in it:

now>now.txt

Then you parse this with the FOR ... IN command to extract the parts of the date you want. On my computer now.txt contains

Fri May 06 22:11:49 2005

So I could extract the Day, Month and Year maybe with:

For /F "tokens=2,3,5" %%A IN ('type now.txt') do (ren backup.txt %B%A%C.cfg)

You can put these commands in a batch file with a @echo off at the beginning. It seems to work but I'm sure there's a much more elegant way of doing it.

Good luck!
 
Here is how you can do it without using any other utility like now.exe:

set todaydate=
for /f "tokens=1-2 delims= " %%A in ('date /t') do set todaydate=%%B
for /f "tokens=1-3 delims=/" %%A in ("%todaydate%") do set todaydate=%%A%%B%%C
ren backup.txt %todaydate%.backup.txt


You can adjust the parameters in the loop to make it formatted like you want. Let me know if this works for you.

-DJ
 
here is how to do it in vbscript. note the date will be in 05092005 format. You could use dashes if you wanted to. but not the forward slashes.

Code:
dim objFSO, MyDate
MyDate = Right("0" & Month(Date),2) & Right("0" & Day(Date),2) & Year(Date)

set objFSO=CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("c:\logs\Test.txt") Then
   objFSO.MoveFile "c:\logs\Test.txt", "c:\logs\Test" & MyDate & ".txt"
End If

I hope you find this post helpful.

Regards,

Mark
 
thank you all so much for you help. I have chosen the vb scropt because i can do so many other things with our back up policy utizing the vbs.
 
I know this has been answered, but here is another way to do this, just in case someone else in the future needs it. %DATE% is already set in the OS and will give you a date like Thu 05/26/2005. Try going to a command prompt and type ECHO %DATE% and it will show todays date. You can just get parts of the date by using the colon and tilday. %DATE:~0,3% will show you the first 3 characters of %DATE%. That's what the 0,3 does. Starts at spot 0 (zero) and shows the next 3 characters (this will include spaces) So, %DATE:~0,3% translates to "Wed". If you want just the date, you would use %DATE:~4,10%. This will show you the mm/dd/yyyy. Because you can't have slashes in a file name, you would have to grab parts of %DATE% and put them together. To get the date without slashes, you can use

ECHO %DATE:~4,2%%DATE:~7,2%%DATE:~10,4%

This translates to 05262005 (todays date as I type this)

To explain this, there are 3 parts...

%DATE:~4,2% gives the "05"

%DATE:~7,2% gives the "26"

%DATE:~10,4% gives the 2005

So, to change, say, a file named "today.txt" to 05262005.txt you would type...

REN today.txt %DATE:~4,2%%DATE:~7,2%%DATE:~10,4%.txt

This is probably a longer explanation than was needed, but just in case someone in the future could use it, there you go.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top