Solved

batch file rename date

Posted on 2012-12-26
12
1,148 Views
Last Modified: 2012-12-26
On Windows Server 2003
The following batch file below will rename a file called: filename.txt
to filenameDate.txt
*where date = todays date and is displayed as
    MMDDYYYY (MM=Month DD = Day YYYY = year)

::BATCH FILE
@echo off
set today=%date:~4%
for %%a in ("filename.txt") do ren "%%~a" "%%~na%today:/=%%%~xa"
::END BATCH

The batch file runs daily and will always rename the file to the today's date.
I need to display the date as YYYYMMDD  (year month day)  instead of MMDDYYYY.
so the file will be renamed filenameYYYYMMDD.txt.   Any ideas on how to do this?  Thanks.
0
Comment
Question by:fjkaykr11
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 75 total points
ID: 38721710
You will have to split the date into its parts, and then join in the proper sequence.
@echo off
REM %date% is ddd, mm/dd/yyyy
set yyyymmdd=%date:~-4%%date:~-10,2%%date:~-7,2%
for %%a in ("filename.txt") do ren "%%~a" "%%~na%yyyymmdd%%~xa"

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38721713
I can't post a link at the mo. Because on mobile but I have written an article here on how to do such things.   Using %date%a variable is pretty unreliable as the date format can be all sorts of things, and at the moment your says to take 4 chars off the beginning and then change any /adding chars to -.  It wont add 0's when it is 1 March say and if the date doesnt have week day at start more issues...

So personally I nearly always use a one line vbscript in the batch file.  Really easy to do and 100% reliable as VBscript does dates natively.

If you click on my dragon-it link you should be able to see the article details.  There are examples in there to get you any date and time format you want.

Steve
0
 
LVL 10

Accepted Solution

by:
tdlewis earned 425 total points
ID: 38721727
Replace your "set today" statement with these:
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set year=%%c
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set month=%%a
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set day=%%b
set today=%year%%month%%day%

Open in new window

0
 
LVL 3

Author Comment

by:fjkaykr11
ID: 38721743
@qlemo.  I get an error when trying to run your batch file:
error:
The following usage of the path operator in batch-parameter
substitution is invalid: %~xa"
0
 
LVL 3

Author Comment

by:fjkaykr11
ID: 38721911
@TDLewis.  Thanks for  your reply. When I try your method:

@echo off
set today=for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set year=%%c
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set month=%%a
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set day=%%b
set today=%year%%month%%day%
for %%a in ("filename.txt.txt") do ren "%%~a" "%%~na%today:/=%%%~xa"

The file gets renamed but not with the year.  I get the following
filename1226.txt    I need it to be renamed to filename20121226.txt for today's date.
0
 
LVL 10

Assisted Solution

by:tdlewis
tdlewis earned 425 total points
ID: 38721984
Execute the command "date /T" and tell me what you get as output. Also, put these four lines after "set today" and tell me what you get as output:
echo year: %year%
echo month: %month%
echo day: %day%
echo today: %today%

Open in new window

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 3

Author Comment

by:fjkaykr11
ID: 38722001
@tdlewis when I run the command date /T
I get:
 Wed 12/26/2012
0
 
LVL 3

Author Comment

by:fjkaykr11
ID: 38722007
@tdlewis:  
When I add the four lines in to the batch file:
echo year: %year%
echo month: %month%
echo day: %day%
echo today: %today%

I get back
year:
month: 12
today: 1226
0
 
LVL 10

Assisted Solution

by:tdlewis
tdlewis earned 425 total points
ID: 38722013
I see the problem. Replace this line:
set today=for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set year=%%c
with
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set year=%%c

Open in new window

0
 
LVL 3

Author Closing Comment

by:fjkaykr11
ID: 38722028
that worked, fantastic!. Thanks.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38722060
You see my point then perhaps about why not to use date /T or %date% .... different machine, different user, language, date format (ddd dd/mm/yyyy, mm/dd/yyyy etc. And it breaks.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 38722235
Dragon is correct about the fragile usage of %date% and date /t. In a restricted environment it works as expected, but it might not work on other machines.

My code was only missing one out of 3 percent signs:
@echo off
REM %date% is ddd, mm/dd/yyyy
set yyyymmdd=%date:~-4%%date:~-10,2%%date:~-7,2%
for %%a in ("filename.txt") do ren "%%~a" "%%~na%yyyymmdd%%%~xa"

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now