简体   繁体   中英

Creating a log file along with conditions in batch

I need to add a few functions in a batch script I am already working on. The functions need to do the following.

1) Read and check for the current date.

2) Set a variable for the log file its-self.

3) Name the log file as follows (log%day-of-the-week%.txt) Example: log24.txt

4) Conditional statement: If the log file is older than 30 days, then overwrite it. If not older than 30 days, append to it.

What we are trying to do is write information to a log file each time the script runs.

Ideal Process

The script runs, sees there is no log file set for today. Creates a log file called log24.txt. The file gets written to multiple times in that day and gets appended.

The script runs the following day (02/25/14), and creates a log file called log25.txt. The file gets written to multiple times in that day and gets appended.

Log Rotation Function

The script should check for any files created within the last 30 days. So assume its now 03/24/14. The function should see there is already a log24.txt and overwrites the 02/24/14 content within that file with new 03/24/14.

The idea is for these logs to be available for 30 days, and then overwrite themselves to prevent them from growing too large.

Any ideas would be appreciated.

Try this:

@echo off
setlocal

Call :RotatingLog "This is a test"
exit /b

:RotatingLog <text>
for /f "tokens=2 delims==" %%a in (
'wmic OS Get localdatetime /value'
) do set "dt=%%a"
set "DD=%dt:~6,2%" & set "MM=%dt:~4,2%"
set "logfile=Log%DD%.txt"
if exist %logfile% (
  for %%a in (%logfile%) do (
    for /f "Tokens=1 delims=/" %%b in ("%%~ta") do (
      if %%b EQU %MM% (echo %~1>>%logfile%
      ) ELSE (
      echo %~1>%logfile%)
    )    
  )
)  
exit /b

This will only work natively from vista and later OS, as robocopy is used for date operations, to retrieve the number of day without problems with windows localizations (and date variable contents structute) or admin rights needed (wmic), and to determine if the logfile needs to be rotated. Of course, you can get a copy of robocopy from Windows Server 2003 Resource Kit Tools ( downloadable from microsoft ) for usage in XP if needed.

This code is not intended for continual execution, as the day retrieval and log rotation is only done in batch start. Adapt as needed.

@echo off
    setlocal enableextensions disabledelayedexpansion

    :: retrieve log file name, adjust log path and rotate if necessary
    call :getLogFilename logFile
    set "logFile=%cd%\%logFile%"
    call :logRotate "%logFile%"

    :: Now, append to log as needed

    echo %time% Something >> "%logFile%"
    ( for /l %%a in (1 1 10) do echo %time% This is line numer %%a ) >> "%logFile%"

    endlocal
    exit /b

:getLogFilename returnVariable
    setlocal enableextensions enabledelayedexpansion
    set "day=" & for /f "tokens=*" %%a in ('robocopy^|find " : "') do for %%b in (%%a) do if not defined day ( set /a "day=%%b0/10" 2>nul & if !day!==0 set "day=")
    endlocal & if not "%~1"=="" set "%~1=log%day%.txt" & exit /b 

:logRotate logfile
    setlocal enableextensions
    robocopy "%~dp1." "%~dp1." "%~nx1" /njh /njs /ndl /is /minage:27 /l /nocopy | find ":\" >nul 
    if not errorlevel 1 break > "%~f1"
    endlocal & exit /b 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM