0
I have tried to do like this 

Inputfile contains the file which has to be read

for /F "tokens=* delims=" %%A in (%INPUTFILE%) do (
    SET my_line=%%A
    SET my_line=%my_line:#define ACCEL_PRESENT%
    findstr /C:"define ACCEL_PRESENT" 
    if %errorlevel%==0 ( SET my_line=%%A
    call :parse_it
    )

)

:parse_it
for /F "usebackq tokens=1-6 delims= " %%1 in ('%my_line%') do (
    echo %%3 > t.txt
)

I am getting nothing from the above code.Can someone help me with the desired results

  • SET my_line=%my_line:#define ACCEL_PRESENT% here I am looking for the line but I want to search for the string – Gaurav Singh Dec 12 '18 at 11:18
  • 1
    Syntax error? : It is `%var:search=replace%` not `%var:search%`! What do you want to do? To remove `#define ACCEL_PRESENT` string? – double-beep Dec 12 '18 at 12:27
  • 2
    Classic delayed expansion issue – dbenham Dec 12 '18 at 13:12
  • As `%my_line%` is a string don't enclose it into single-quotes, but into DOUBLE-QUOTES! – double-beep Dec 12 '18 at 13:23
  • 1
    `findstr /c:"string"` will give you nothing! – double-beep Dec 12 '18 at 13:36
  • Possible duplicate of [Variables in batch not behaving as expected](https://stackoverflow.com/questions/30282784/variables-in-batch-not-behaving-as-expected) – chwarr Dec 12 '18 at 14:08
  • Please provide an example of your input file and Please take the [Tour](https://stackoverflow.com/tour). Please learn, [How to ask a good question?](https://stackoverflow.com/help/how-to-ask). Also please read [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Squashman Dec 12 '18 at 16:29

1 Answers1

0

Here is one possible solution:

@echo off

setlocal EnableDelayedExpansion

for /f "tokens=* delims=" %%A in (%INPUTFILE%) do (
    set "my_line=%%A"
    set "my_line=!my_line:#define ACCEL_PRESENT=!"
    echo !my_line! | findstr /C:"define ACCEL_PRESENT"
    IF !errorlevel! EQU 0 ( SET "my_line=%%A" && call:parse_it )
)

:parse_it
for /F "usebackq tokens=1-6 delims= " %%A in ("%my_line%") do echo %%C > t.txt

rem exit /b

I hope you wanted to delete string #define ACCEL_PRESENT.

Also, say me if %my_line% is a command (I don't think so, so I removed single-quotes and I added double-quotes).

Better to use letter variables in for loops!

double-beep
  • 5,031
  • 17
  • 33
  • 41