# Rename log file based on age of file

Hi i have a script that runs every 15 minutes and it writes to a log file that is growing waaay to fast. Just today alone it went from creation to 30MB. My solution is to have the script create a new log file every week, but copy the original file and rename it with a date_time stamp.log. I am looking for a simple code that would look at the age of the log file and if it is older than seven days, copy the log file and append date_time stamp to it, then using the set variable I have there (set logfile= blah blah)
create a new one and maybe append a number of some sort to prevent duplicate name being created. Thanks for all responses
Zay
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IT / Software Engineering ConsultantCommented:
Can you tell me what the EXACT output of the two commands below is at a DOS command line:

echo [%DATE%]
echo [%TIME%]

Do you want / need a pure DOS / BAT solution, or would the use of VBS (which has better date manipulation capability) be okay?

~bp
IT / Software Engineering ConsultantCommented:
Okay, haven't seen an answer yet, so here's a BAT approach that should be a good starting point.  Adjust the variables near the top as needed and give it some testing.  It doesn't create a new empty log file, I'm assuming your existing script probably does that if the file doesn't exist already.

This also assumes your system date is in MM-DD-YYYY format.

@echo off
setlocal EnableDelayedExpansion

REM Define full path to log file, and days before rolling log file
set DaysToKeep=7
set LogfilePath=c:\ee\EE28326513\log.txt

call :RollLog "%LogfilePath%" %DaysToKeep%

exit /b

:RollLog [logfile-path] [days-to-roll]
REM Get todays date (MM/DD/YYYY), convert to julian for age checks
call :jDate jToday %DATE:~-10%

REM Get files date, convert to julian for age checks
call :jDate jFile %~t1

REM See if it's tile to archive the current log file
set /A FileAge = %jToday% - %jFile%!
if %FileAge% GTR %DaysToKeep% (
REM Build YYYYMMDD_hhmmss format date/time stamp for new file name
set Stamp=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%_%TIME:~0,8%
set Stamp=!Stamp::=!
set Stamp=!Stamp: =0!

REM Rename log file
ECHO File:[%~1] is [%FileAge%] days old and has been renamed to [%~n1_!Stamp!%~x1].
ren "%~1" "%~n1_!Stamp!%~x1"
)

exit /b

REM Subroutine to calculate julian date
:jDate return-variable date-string(MM/DD/YYYY)
set DateStr=%~2
set yy=%DateStr:~6,4%
set /A mm=1%DateStr:~0,2%-100
set /A dd=1%DateStr:~3,2%-100
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
set /a %~1=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
exit /b

~bp

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Thanks Bill, I have to laugh, cause I actually figured it out a few days ago. Well not figured it out, I found this very script you wrote for me for another task I had. In trying to solve my issue, it dawned on me that I could use this same script. I appreciate you always coming to my rescue. You did so again without even realizing it, so thanks.
IT / Software Engineering ConsultantCommented:
Welcome.

~bp
Author Commented:
Script worked like a charm, thanks Bill.
IT / Software Engineering ConsultantCommented:
Welcome, thanks for the feedback.

~bp
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.