Question

I have some code, that I am trying to use to take this file, and wait until it gets to line 39. And then at line 39 I want it to print something else. But i can't get it to ever get through that IF condition.

set line=0

FOR /f "usebackqdelims=" %%a in ("%filename2%") do (
    set /a line+=1
    if !line!==39 (
        echo REPLACED TEXT>>%tempfile%
    ) else (
        echo %%a>>%tempfile%
    ) )

After the

set /a line+=1

I tried

ECHO line

to see what my line variable was after I set it to line+=1. I received back all 0s. Does anyone see why my code is not incrementing the line like its supposed to? But when it outputs the file, it outputs perfectly except that line 39 that does not change. It remains the same.

Was it helpful?

Solution

I think you're missing setlocal ENABLEDELAYEDEXPANSION and you need to use echo !line! if you want to output inside the block:

@echo off
setlocal ENABLEDELAYEDEXPANSION
set line=0

FOR /f "usebackqdelims=" %%a in ("%filename2%") do (
    set /a line = !line!+1 
    ECHO !line!
    if !line!==39 (
        echo REPLACED TEXT>>%tempfile%
    ) else (
        echo %%a>>%tempfile%
    ) )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top