Wajo357
Wajo357

Reputation: 91

Need a quick way to process each line of a file using batch, without piping to echo

So I am using a batch to go through a text file line by line. The reason why I can't do a regular search is because I need to see what is written on each line and then determine how I want to process the next line. Anyway, my basic code is:

for /f "usebackq delims=" %%a in (%SourceLicenseFile%) do (
    Set LineofFile=%%a
    echo !LineofFile! | find /i "TIMESTAMP" >nul
    if !errorlevel! EQU 0 ( 
        FOR %%b IN (!LineofFile!) DO SET LogDate=%%b
    )
    echo %%a | find /i "(LOGTIME) IN" >nul
    if !errorlevel! EQU 0 ( 
        FOR %%d IN (!LineofFile!) DO SET Username=%%d
        FOR /f "tokens=1" %%c IN ("!LineofFile!") DO (
            SET Logtime=%%c
        )
        echo !LogDate!  !LogTime!   !Username!
        >> !ExportLicenseLog! echo !LogDate!    !LogTime!   !Username!
    )
)

I believe the echo is slowing down this processing. I have a file with thousands of lines and it would take me over a minute to process. Is there another method I can use to go through each line quicker?

Upvotes: 1

Views: 50

Answers (3)

Aacini
Aacini

Reputation: 67236

Yes, the echo ... | find ... slowdown the process, not because the echo, but for the pipe...

You have not clearly explained the purpose of your program. However, to check if !LineofFile! variable have the "TIMESTAMP" string, you may use this code:

if "!LineofFile:TIMESTAMP=!" neq "!LineofFile!" (
   echo TIMESTAMP string found in LineofFile variable
)

This code means: "Try to remove TIMESTAMP string from LineofFile variable. If the string was found, it was removed, so the replaced value is different than the original".

Upvotes: 1

Wajo357
Wajo357

Reputation: 91

OK. I got it. Using substitution is MUCH faster

for /f "usebackq delims=" %%a in (%SourceLicenseFile%) do (
    Set LineofFile=%%a
    if not "x!LineofFile:TIMESTAMP=!" EQU "x!LineofFile!" (
        FOR %%b IN (!LineofFile!) DO SET LogDate=%%b
    )
    if not "x!LineofFile:IN:=!" EQU "x!LineofFile!" (
        FOR /f "tokens=1,5" %%c IN ("!LineofFile!") DO (
            SET Logtime=%%c
            SET Username=%%d
        )
        if "!LogDate!" NEQ "DATE NOT FOUND" (
            echo !LogDate!  !LogTime!   !Username!
            >> !ExportLicenseLog! echo !LogDate!    !LogTime!   !Username!
        )
    )
    Set /A LineNumber=!LineNumber!+1
    Set /A PercentDone=!LineNumber!/%TotalLines%
)

Upvotes: 0

Compo
Compo

Reputation: 38709

You don't need the echo:

find /i "SOMETHING"<%SourceLicenseFile%

Escape thus ^< inside a for parentheses.

Upvotes: 0

Related Questions