Solved Log File Date and Time

Hewlett-packard / Dl380 g5
March 12, 2009 at 07:57:20
Specs: Windows 2003 R2 Server , intel 3.2 - 4 Gig
I have a file name "log.txt", that I need to rename daily to log_DDMMYYYYhhmmss.txt based on the date that it was created. I've searched through the forums but have not found anything related to this format. Any suggestions would be greatly appreciated? thanks

See More: Log File Date and Time

Report •

March 12, 2009 at 11:55:59
✔ Best Answer
Run this and paste in remult. You're probably out of luck for ss.

@echo off & setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
echo %%~Ta

If at first you don't succeed, you're about average.


Report •

March 29, 2009 at 00:57:50
As you know, Time in Windows system has 24 hours format. Before 10 AM hours use only one position to hold numbers from 0 to 9. Try run command "Time" to see result: 1:14:16.57 and there are 10 positions total, first 0 doesn’t display. But after 10 AM time will display in 11 positions total: 11:15:18:33, with 2 positions for hours

To capture hours from 0 to 9 you need use format: %time:~1,1%.
So, code for name in case from 0 to 9 numbers is:

ren log.txt log_%date:~4,2%%date:~7,2%%date:~10,4%%time:~1,1%%time:~3,2%%time:~6,2%%time:~9,2%.txt

%time:~1,1% -- hours
%time:~3,2% -- minutes
%time:~6,2% -- seconds
%time:~9.2% -- milliseconds (fractions of seconds).

From 10 to 24, where hour numbers occupy 2 positions, the time has 11 positions format, and in this case you need use %time:~0,2% to extract hour numbers from 10 to 24 .
Final code for file name in 10-24 case will look like this:

ren log.txt log_%date:~4,2%%date:~7,2%%date:~10,4%%time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%.txt

As for date, when you run command Date, the result will display in format "Day mm/dd/yyyy"
The current date is: Sun 03/29/2009 --- total 14 positions.
Here is an explanation on date extraction:
%date:~4,2% --- month as 2 digits
%date:~6,2% --- days as 2 digits
%date:~10,4% --- year as 4 digits.

If you need present years by 2 digits use:

You can move any part of date around to change presentation format, for instance, when move year part (%date:10,4%) up front, it changes fomat to yyyyddmm, and so on.

You probably need to write a function to evaluate time of day, if you need to use one batch file. Or you can create 2 batch files and schedule one runs before 10 AM, and the other one after 10 AM.

Good luck.

Report •

March 29, 2009 at 04:50:19
It obviously wasn't important.

And there is no general substring solution for time & date.

If at first you don't succeed, you're about average.


Report •

Related Solutions

Ask Question