Write batch variable into specific line in a text file

前端 未结 3 1217
渐次进展
渐次进展 2020-12-20 08:08

I have a batch file where I need to write a variable into a specific line of a text file and override what is all ready in that line. I have the code to read specific lines

3条回答
  •  攒了一身酷
    2020-12-20 08:33

    I figured it out!! Here is the code for anyone else who might ever need it.

    @echo off
    setlocal enableextensions enabledelayedexpansion
    
    set inputfile=variables.txt
    
    set tempfile=%random%-%random%.tmp
    
    copy /y nul %tempfile%
    
    set line=0
    
    for /f "delims=" %%l in (%inputfile%) do (
        set /a line+=1
        if !line!==4 (
            echo WORDS YOU REPLACE IT WITH>>%tempfile%
        ) else (
            echo %%l>>%tempfile%
        )
    )
    
    del %inputfile%
    ren %tempfile% %inputfile%
    
    endlocal
    

提交回复
热议问题