Search code examples
batch-filetextreplace

Find and replace line starts with specific value in text file via windows batch file


Even after deep Googeling I can't solve my problem. Have a text file named test.txt. What I need is to change the line starts with the word "Root:" with other content - using batch file.

setLocal EnableDelayedExpansion    
FINDSTR /B Root: test.txt 
::returns the correct line - works well
for /f %%i in ('FINDSTR /B Root: test.txt') do set root=%%i

echo %root%
::echos "Root:" - instead of the line content

FOR /F "tokens=*" %%G IN (test.txt) DO 
(set x=%%G
if !x!==%root% set x=Hello
echo !x! >> test.txt)
::The syntax of the command is incorrect.

How do i can do it?

EDIT: Based on Magoo and on RobW at Batch / Find And Edit Lines in TXT file - my problem solved as below:

for /f "tokens=*" %%i in ('"FINDSTR /B Root: test.txt"') do set root=%%i
::root holds test.txt's line starts with "Root:"
echo %root%

SETLOCAL=ENABLEDELAYEDEXPANSION
::iterate on test.txt's lines and compare to the root's value
        rename test.txt test.tmp
        for /f "tokens=*" %%a in (test.tmp) do (
            set foo=%%a
            echo !foo!
            echo %root%
            echo "%root%"
            if "!foo!"=="%root%" (set foo=hello)
            echo !foo! >> test.txt)                                       
    del test.tmp

Thanks! Roni


Solution

  • @ECHO OFF
    SETLOCAL
    SET "sourcedir=U:\sourcedir"
    SET "filename1=%sourcedir%\q34900978.txt"
    
    FINDSTR /B Root: "%filename1%" 
    ::returns the correct line - works well
    FOR /f "tokens=*" %%i IN (
     'FINDSTR /B Root: "%filename1%"
    ') do set "root=%%i"
    
    echo %root%
    ::echos "Root:" - instead of the line content
    
    FOR /F "usebackqdelims=" %%G IN ("%filename1%") DO (
     if "%%G"=="%root%" (
      ECHO(x=Hello
     ) ELSE (
     ECHO(%%G
     )
    )
    
    GOTO :EOF
    

    You would need to change the setting of sourcedir to suit your circumstances.

    I used a file named q34900978.txt containing some dummy data for my testing.

    The tokens=* option in the for...%%i (or delims=) assigns the entire line to "token 1" and thence to the metavariable %%i.

    The default is to assign token 1 but using [SpaceTab,;] as separators, hence you get just the string Root: with your code (up to, but not including the default separators)

    See

    for /?

    from the prompt for documentation.

    The syntax SET "var=value" (where value may be empty) is used to ensure that any stray trailing spaces are NOT included in the value assigned. set /a can safely be used "quoteless".

    Next step is to process the file. Same story (but since I quote filenames and provide full paths, I need the usebackq option.

    The entire line is assigned to %%G (note: except empty lines and lines which begin ;

    Then it's a simple if statement - if "the line content"=="target content". The quotes are required because quotes group "strings containing separators" into one string and if syntax is if string operator string2 (dothis) else (dothat)`

    Note that the opening parenthesis must be on the same physical line as the do and the same goes for if. else, the preceding close-parenthesis and the succeeding open-parenthesis must all be on the same physical line with a space between them.

    Note the use of ECHO( which will echo an empty line if %%G (in this case) has no value. The ( is not counted as far as nesting is concerned.

    (%%G here must have a value - but in the general case, echo %var% will yield echo is on/off if var is undefined, but echo(%var% will cleanly produce a new line).