Windows批处理文件替换文本问题



我有这个代码来替换JS文件中的一些文本:

@echo off &setlocal
set "search=showLog: true"
set "replace=showLog: false"
set "textfile=globalsDebugTest.js"
set "newfile=new.js"
(for /f "delims=" %%i in ('findstr /n "^" "%textfile%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    echo(!line!
    endlocal
))>"%newfile%"
type "%newfile%"
这个脚本的问题是它在每一个新行中添加一个行号。我只是想把这个字符串:"showLog: true"替换为:"showLog: false"
知道怎么做吗?
for /f "tokens=1*delims=:" %%h in ('findstr /n "^" "%textfile%"') do (

(注意增加了一个tokens=短语,delims被赋值为:,循环元变量被更改为h)

加上tokens=表示元变量h接收分隔符(冒号)之前的数字,i接收冒号之后行的剩余部分。这使更改保持在一行。

另一个选择是从findstr中删除/n,这将导致从输出中删除所有空行。

最新更新