将批处理变量写入文本文件中的特定行

Spe*_*May 0 text batch-file

我有一个批处理文件,我需要将一个变量写入文本文件的特定行,并覆盖该行中的所有内容.我有代码从文件中读取特定行可能我可以切换它也写?

阅读行代码:

setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (variables.txt) do (
set /a N+=1
set v!N!=%%a
)
set variable1=!v1!
set variable2=!v2!
set variable3=!v3!
set variable4=!v4!
Run Code Online (Sandbox Code Playgroud)

我试图添加%variable1% > !v4!类似的回声,但它不起作用.

Spe*_*May 6

我想到了!!以下是可能需要它的任何其他人的代码.

@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
Run Code Online (Sandbox Code Playgroud)