Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

Run this and paste in remult. You're probably out of luck for ss.
============================
@echo off & setLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
echo %%~Ta
)
=====================================
If at first you don't succeed, you're about average.M2

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
Where
%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:
%date:~12,2%,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.
lmg

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.M2

![]() |
programming in java
|
how to replace multiple s...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |