Batch - edit specified line in text file

前端 未结 1 452
萌比男神i
萌比男神i 2020-12-22 01:30

For example, line number 2 reads: \"0\" (without quotes). I want to change that 0 to a 1, without changing anything else in the text file.

I know what line the value

1条回答
  •  别那么骄傲
    2020-12-22 01:44

    try this:

    @ECHO OFF &SETLOCAL
    SET "file=file"
    SET /a Line#ToSearch=2
    SET "Replacement=0"
    
    (FOR /f "tokens=1*delims=:" %%a IN ('findstr /n "^" "%file%"') DO (
        SET "Line=%%b"
        IF %%a equ %Line#ToSearch% SET "Line=%Replacement%"
        SETLOCAL ENABLEDELAYEDEXPANSION
        ECHO(!Line!
        ENDLOCAL
    ))>"%file%.new"
    TYPE "%file%.new"
    

    Note: this doen't work properly for lines starting with colons :, this might be fixed if needed.

    0 讨论(0)
提交回复
热议问题