|
Answer» Dias I need your help!!I need to understand how the "maxdatenum" works as I am getting problem when latest date is 4/1/09 instead of 3/31/09.It is still extracting only 3/31/09 even if latest date is 4/1/09 or later. please help me understand.I have googled for this but no use. Apologies for the stupid error in my code. The script was supposed to extract the first 8 characters of each line which are MM/DD/YY e.g. 04/01/09 and re arrange them to make a number for the date in the form YYYYMMDD e.g. 20090331 or 20090401 such that a later date makes a greater number. However by a stupid error I had the month and day transposed. I would have caught it if I had tested with a month later than March. Fixed below.
Code: [Select]@echo off setlocal enabledelayedexpansion
set filename=test0401.txt
REM pass 1 - determine latest date echo Examining file - determining latest date set maxdatenum=0 set lastdate=
for /f "delims=" %%L in ( ' type "%filename%" ' ) do ( set thisline=%%L set datestamp=!thisline:~0,8! REM Fixed set dateM=!datestamp:~0,2! REM was previously !datestamp:~3,2! REM Fixed set dateD=!datestamp:~3,2! REM was previously !datestamp:~0,2! set dateY=20!datestamp:~6,2! REM Datenum=YYYYMMDD REM e.g. 20093103 REM e.g. 20090401 set datenum=!dateY!!dateM!!dateD!
if !datenum! GTR !maxdatenum! ( set maxdatenum=!datenum! set lastdate=!datestamp! echo Found date : !datestamp! ) ) echo Latest date found : %lastdate% REM filenames cannot have / character set filenamedate=%lastdate:/=-% REM pass 2 extract latest date's lines REM containing DOWNLOAD because only pairs have this line set alreadydone= REM The filename transferred is TOKEN 6 for /f "tokens=1-26 delims= " %%L in ( ' type "%filename%" ^| find "%lastdate%" ^| find "DOWNLOAD"' ) do ( set filename=%%~nxQ REM Echo Found filename : !filename! REM Find number part within transferred filename REM Delimiter is underscore character for /f "delims=_" %%A in ("!filename!") do ( set number=%%A echo !alreadydone! | find ",!number!,">nul || ( set alreadydone=,!number!,!alreadydone! echo. REM Echo File number part : !number! Set Outfile=%filenamedate%-!number!.txt Echo Creating file : !outfile! if exist "!outfile!.tmp" del "!outfile!.tmp" if exist "!outfile!" del "!outfile!" REM Examine entire log file and extract lines containing CURRENT REM transferred file number REM group each filenumber pair together, ignore lines containing message.txt REM Write to temp file first for /f "delims=" %%B in ( ' type "%filename%" ^| find "%lastdate%" ^| find "!number!" ^| find /v "message.txt"' ) do ( Echo %%B>>"!outfile!.tmp" ) REM Only take latest pair for each file number REM Count lines in output set numlines=0&for /f "delims=" %%C in ( ' type "!outfile!.tmp"' ) do set /a numlines+=1 REM Subtract 2, this GIVES the first line to take set /a startline=!numlines!-2 REM Transfer last 2 lines of tmp file to REM real output file set line=1 for /f "delims=" %%C in ( ' type "!outfile!.tmp"' ) do (
if !line! GTR !startline! ( echo %%C>>!outfile! echo %%C ) set /a line+=1 ) del "!outfile!.tmp" ) ) ) Echo. Yes I guessed you have add the date format numbers but when I tried to correct it and refered to other topics in google I did not find any.Thank you so MUCH for your time.It works perfect now.
|