Computing.Net > Forums > Programming > Log File Date and Time

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Log File Date and Time

Reply to Message Icon

Name: hrodriguez411
Date: March 12, 2009 at 07:57:20 Pacific
OS: Windows 2003 R2 Server
CPU/Ram: intel 3.2 - 4 Gig
Product: Hewlett-packard / Dl380 g5
Subcategory: General
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: March 12, 2009 at 11:55:59 Pacific
Reply:

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.

M2


0

Response Number 2
Name: lmg
Date: March 29, 2009 at 00:57:50 Pacific
Reply:

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


0

Response Number 3
Name: Mechanix2Go
Date: March 29, 2009 at 04:50:19 Pacific
Reply:

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


0
Reply to Message Icon

Related Posts

See More


programming in java how to replace multiple s...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Log File Date and Time

Note log with date and time stamp www.computing.net/answers/programming/note-log-with-date-and-time-stamp/17686.html

Batch File - date and time on files www.computing.net/answers/programming/batch-file-date-and-time-on-files/9137.html

batch file name date and time www.computing.net/answers/programming/batch-file-name-date-and-time/15060.html