Solved

Rename log file based on age of file

Posted on 2013-12-27
7
266 Views
Last Modified: 2014-01-07
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
0
Comment
Question by:xzay1967
  • 4
  • 2
7 Comments
 
LVL 52

Expert Comment

by:Bill Prew
ID: 39748708
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
0
 
LVL 52

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39749109
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

Open in new window

~bp
0
 

Author Comment

by:xzay1967
ID: 39749363
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.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 52

Expert Comment

by:Bill Prew
ID: 39749370
Welcome.

~bp
0
 

Author Closing Comment

by:xzay1967
ID: 39762096
Script worked like a charm, thanks Bill.
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 39762229
Welcome, thanks for the feedback.

~bp
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

867 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

16 Experts available now in Live!

Get 1:1 Help Now