Page 1 of 1

Pulling Windows2003 log files

Posted: 12 Sep 2006 12:51
by Cooldmitriy
I am trying to pull Windows2003 server log files via FTP.
The server creates new log files every day. I need to pull them daily and always for the prior day. Example: On Monday I pull log files for Sunday, on Tuesday for Monday and so on.
Log files have the following format: exYYMMDD.log
I know how to get current date and extract year, month and date portion. But how do I subtract 1 day from it?

Thank you all in advance.

Posted: 12 Sep 2006 19:10
by DosItHelp
Cooldmitriy

Copy the functions :jdate , :date2jdate , and :jdate2date to the end of your batch script.

http://www.dostips.com/DtCodeCmdLib.php#jdate
http://www.dostips.com/DtCodeCmdLib.php#date2jdate
http://www.dostips.com/DtCodeCmdLib.php#jdate2date

Then you can calculate the date like this:

Code: Select all

call:jdate d "%date%"
set /a d-=1
call:jdate2date d YYYY MM DD
set logfile=ex%YYYY:-2%%MM%%DD%.log


Hope this helps :wink:

Posted: 13 Sep 2006 09:31
by Cooldmitriy
Thank you DosItHelp ! For some reason I can't trim down CC portion of YYYY. YYYY:-2% doesn't seem to work
I am not sure what I did wrong. Here's my entire batch file for your review:

--------------------------------------------------------------------
C:
call:jdate d "%date%"
set /a d-=1
call:jdate2date d YYYY MM DD
set logfile=ex%YYYY%%MM%%DD%.log

:jdate JD DateStr -- converts a date string into julian day number
:: -- JD [out] - julian days
:: -- DateStr [in] - date string, i.e. "03/31/2006" or "Fri 03/31/2006"
SETLOCAL ENABLEDELAYEDEXPANSION
set DateStr=%~2
set DateStr=%DateStr:~-10%&rem consider only the last 10 characters
for /f "tokens=1-3 delims=/ " %%a in ("%DateStr%") do (
set /a YYYY=1%%c-10000
set /a MM=1%%a-100
set /a DD=1%%b-100
)
call:date2jdate JD "%YYYY%" "%MM%" "%DD%"
( ENDLOCAL & REM RETURN VALUES
IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
)
EXIT /b %ERRORLEVEL%


:date2jdate JD YYYY MM DD -- converts a gregorian calender date into julian day format
:: -- JD [out] - julian days
:: -- YYYY [in] - gregorian year, i.e. 2006
:: -- MM [in] - gregorian month, i.e. 12 for december
:: -- DD [in] - gregorian day, i.e. 31
SETLOCAL ENABLEDELAYEDEXPANSION
set YYYY=%~2
set MM=%~3
set DD=%~4
set /a I=%YYYY%
set /a J=%MM%
set /a K=%DD%
set /a JD=K-32075+1461*(I+4800+(J-14)/12)/4+367*(J-2-(J-14)/12*12)/12-3*((I+4900+(J-14)/12)/100)/4
( ENDLOCAL & REM RETURN VALUES
IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
)
EXIT /b %ERRORLEVEL%

:jdate2date JD YYYY MM DD -- converts julian days into gregorian date format
:: -- JD [in] - julian days
:: -- YYYY [out] - gregorian year, i.e. 2006
:: -- MM [out] - gregorian month, i.e. 12 for december
:: -- DD [out] - gregorian day, i.e. 31
SETLOCAL ENABLEDELAYEDEXPANSION
set /a L= %~1+68569
set /a N= 4*L/146097
set /a L= L-(146097*N+3)/4
set /a I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31
set /a J= 80*L/2447
set /a K= L-2447*J/80
set /a L= J/11
set /a J= J+2-12*L
set /a I= 100*(N-49)+I+L
set /a YYYY= I
set /a MM= J
set /a DD= K
( ENDLOCAL & REM RETURN VALUES
IF "%~2" NEQ "" (SET %~2=%YYYY%) ELSE echo.%YYYY%
IF "%~3" NEQ "" (SET %~3=%MM%) ELSE echo.%MM%
IF "%~4" NEQ "" (SET %~4=%DD%) ELSE echo.%DD%
)
EXIT /b %ERRORLEVEL%



cd C:/WINDOWS/system32/Logfiles/W31C
copy %logfile% C:

Posted: 13 Sep 2006 18:05
by DosItHelp
Sorry I didn't try it out:
It had two errors:

1. to extract CC from YYYY use:
%YYYY:~-2%

2. to make sure MM and DD are always two digits, e.g. with leading zero, use:
set MM=00%MM%&set DD=00%DD%
set DD=%DD:~-2%&set MM=%MM:~-2%

You will also need to ensure the script stops before running into the functions.

The idea is to have the functions pasted at the end of the script, so that the main script at the beginning stays clean.


The following should work:

Code: Select all

@ECHO OFF
call:jdate d "%date%"
set /a d-=1
call:jdate2date d YY MM DD
set MM=00%MM%&set DD=00%DD%
set DD=%DD:~-2%&set MM=%MM:~-2%&set YY=%YY:~-2%
set logfile=ex%YY%%MM%%DD%.log
ECHO. %date% - %logfile%

ECHO.
PAUSE
GOTO:EOF



::-----------------------------------------------------------------------------------
::-- Functions start below here
::-----------------------------------------------------------------------------------


:jdate JD DateStr -- converts a date string into julian day number
::                -- JD      [out] - julian days
::                -- DateStr [in]  - date string, i.e. "03/31/2006" or "Fri 03/31/2006"
:$source http://www.DosTips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set DateStr=%~2
set DateStr=%DateStr:~-10%&rem consider only the last 10 characters
for /f "tokens=1-3 delims=/ " %%a in ("%DateStr%") do (
    set /a YYYY=1%%c-10000
    set /a MM=1%%a-100
    set /a DD=1%%b-100
)
call:date2jdate JD "%YYYY%" "%MM%" "%DD%"
( ENDLOCAL & REM RETURN VALUES
    IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
)
EXIT /b %ERRORLEVEL%


:date2jdate JD YYYY MM DD -- converts a gregorian calender date into julian day format
::                     -- JD   [out] - julian days
::                     -- YYYY [in]  - gregorian year, i.e. 2006
::                     -- MM   [in]  - gregorian month, i.e. 12 for december
::                     -- DD   [in]  - gregorian day, i.e. 31
:$reference http://aa.usno.navy.mil/faq/docs/JD_Formula.html
:$source http://www.DosTips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set YYYY=%~2
set MM=%~3
set DD=%~4
set /a I=%YYYY%
set /a J=%MM%
set /a K=%DD%
set /a JD=K-32075+1461*(I+4800+(J-14)/12)/4+367*(J-2-(J-14)/12*12)/12-3*((I+4900+(J-14)/12)/100)/4
( ENDLOCAL & REM RETURN VALUES
    IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
)
EXIT /b %ERRORLEVEL%


:jdate2date JD YYYY MM DD -- converts julian days into gregorian date format
::                     -- JD   [in]  - julian days
::                     -- YYYY [out] - gregorian year, i.e. 2006
::                     -- MM   [out] - gregorian month, i.e. 12 for december
::                     -- DD   [out] - gregorian day, i.e. 31
:$reference http://aa.usno.navy.mil/faq/docs/JD_Formula.html
:$source http://www.DosTips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set /a L= %~1+68569
set /a N= 4*L/146097
set /a L= L-(146097*N+3)/4
set /a I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31
set /a J= 80*L/2447
set /a K= L-2447*J/80
set /a L= J/11
set /a J= J+2-12*L
set /a I= 100*(N-49)+I+L
set /a YYYY= I
set /a MM= J
set /a DD= K
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" (SET %~2=%YYYY%) ELSE echo.%YYYY%
    IF "%~3" NEQ "" (SET %~3=%MM%) ELSE echo.%MM%
    IF "%~4" NEQ "" (SET %~4=%DD%) ELSE echo.%DD%
)
EXIT /b %ERRORLEVEL%


Which returns on my PC:

Code: Select all

Wed 09/13/2006 - ex060912.log


Hope this helps, let me know :wink: