批量用变量替换文本文件的某些部分

批量用变量替换文本文件的某些部分

我尝试用相应的变量替换stat.scorestat.level,但它只是在每一行打印 stat.score=000099。

代码如下:

@echo off
mode con cols=100 lines=40
setlocal EnableDelayedExpansion
:GameRender
del %cd%\assets\GameRender\pets\cthulhu\GameRender.txt
set "stat.score=000099"
set "stat.level=0"
set "Render_Ln=0"

for /L %%a in (1,1,39) do (

if !Render_Ln! EQU 39 goto :pause
set /a "Render_Ln=!Render_Ln!+1"

for /f "usebackq delims=@ skip=1" %%g in ("%cd%\assets\GameRender\pets\cthulhu\GameRender_source.txt") do set Line=%%g
echo:!Line! | findstr "stat."
    if %errorlevel% EQU 0 (
        set Line=%Line:stat.score=!stat.score!%
    )
echo:!Line!>>"%cd%\assets\GameRender\pets\cthulhu\GameRender.txt"
)
:pause
pause
for %%a in (%cd%\assets\GameRender\pets\cthulhu\GameRender.txt) do type %%a 
pause>nul

这是GameRender_source.txt。

答案1

另一种选择是使用 PowerShell 脚本来替换值;该脚本应该是不言自明的:

(Get-Content d:\GameRender.txt) | ForEach-Object {$_ -replace "stat.score", "000099" -replace "stat.level", "0"} | Set
-Content d:\GameRender.txt

答案2

好的,在您的帮助下,我可以通过将 GameRender_source.txt 内容更改为仅一行来解决这个问题。

for /f "usebackq delims=@" %%a in ("%cd%\assets\GameRender\pets\cthulhu\GameRender_source.txt") do set Render=%%a
call set "Render=%%Render:stat.score=!stat.score!%%"
call set "Render=%%Render:stat.level=!stat.level!%%"
echo:!Render!>>"%cd%\assets\GameRender\pets\cthulhu\GameRender.txt"

相关内容