Rename in a batch file

G

Guest

This is a batch programming issue, so I'm not sure if this is the right
newsgroup. If not, maybe someone could suggest a better one.

In a batch file which runs a backup I want the last line to rename the log
file

name_of_backup_job.txt to

name_of_backup_job_today's date.txt with the date in YMD format so that
files will sort into order properly in a directory listing.

How do I extract the date into a variable, and how can I get it into YMD
format?

Grateful for advice.
 
P

Pegasus \(MVP\)

simonc said:
This is a batch programming issue, so I'm not sure if this is the right
newsgroup. If not, maybe someone could suggest a better one.

In a batch file which runs a backup I want the last line to rename the log
file

name_of_backup_job.txt to

name_of_backup_job_today's date.txt with the date in YMD format so that
files will sort into order properly in a directory listing.

How do I extract the date into a variable, and how can I get it into YMD
format?

Grateful for advice.

I suspect you don't mean YMD format but perhaps YYYYMMDD.
You can do it like this:

@echo off
set D=%date:~6,4%%date:~0,2%%date:~3,2%
echo ren name_of_backup_job.txt name_of_backup_job_%D%.txt

Remove the "echo" word to activate the batch file.
Depending on your time zone and data format, you
may have to tune the numbers in the second line.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top