windows-10windows-scripting

Script to extract/cut contents from a log file into a new file


I have a log file that captures data and timestamps it, but it doesn't have a date stamp. So it ends up looking like the below with the newest information at the very bottom.

[01:10:01:943] numbers/words/etc
[23:55:53:505] numbers/words/etc
[00:15:43:432] numbers/words/etc
[23:54:01:442] numbers/words/etc

What I am trying to do is move the first "day" using the timestamps and moving it to a new file name with the current date and leaving the new "day" intact, below is what I want I am needing it to look like after it runs.

Original File

[00:15:43:432] numbers/words/etc
[23:54:01:442] numbers/words/etc

New File.Month_Day_Year

[01:10:01:943] numbers/words/etc
[23:55:53:505] numbers/words/etc

Any help would be very appreciated as I am relatively new to scripting as a whole and am trying to learn!


Solution

  • @ECHO OFF
    SETLOCAL ENABLEDELAYEDEXPANSION
    rem The following settings for the source directory, destination directory and filenames are names
    rem that I use for testing and deliberately include names which include spaces to make sure
    rem that the process works using such names. These will need to be changed to suit your situation.
    
    SET "sourcedir=u:\your files"
    SET "destdir=u:\your results"
    SET "filename1=%sourcedir%\q74381462.txt"
    SET "outfile=%destdir%\outfile.txt"
    SET /a fileseq=0
    SET "line="
    
    (
    FOR /f "usebackqdelims=" %%e IN ("%filename1%") DO (
     IF "!line!" gtr "%%e" SET /a fileseq+=1
     ECHO %%e>>"%outfile%.!fileseq!"
     SET "line=%%e"
    )
    )
    
    GOTO :EOF
    

    Splits the file into parts for each day. Since it appears possible that more than one day's data is contained in the file, applying a date is debatable. The number of files produced is in fileseq.

    Just while I think of it, my sceme would be:

    ren "%filename1%" newdata
    type newdata>>dregs
    rem now process dregs which is dregs from previous run+newdata
    move "%outfile%.%fileseq%" dregs
    del newdata
    

    So - incomplete data from the prior run is moved to dregs, then append new data - process and generate a new dregs file.