![](/img/trans.png)
[英]Windows batch file to replace nth line with new string or extract nth line and store string in variable
[英]Replace string with a new line in Batch
set NLM=^
set NL=^^^%NLM%%NLM%^%NLM%%NLM%
SET memoli=%token:QMZ=%NL%%%
echo %memoli%>>%tmp%\list2.txt
我不能用新行更改字符串“QMZ”。 怎么做?
非常簡單
setlocal EnableDelayedExpansion
set "token=HelloQMZworld"
echo !token:QMZ=^
!
它適用於批處理解析器首先解析多行插入符並用單個換行符替換它。
然后在延遲擴展階段,它用單個換行替換QMZ,這在該階段是合法的。
要使用替換的字符串設置新變量,只需使用
setlocal EnableDelayedExpansion
set "token=HelloQMZworld"
set newVal=!token:QMZ=^
!
echo !newVal!
set LF=^
rem ** Two empty lines required
FOR /F "delims=" %%a in ("%token:QMZ=!LF!%") do (
echo %%a>>%tmp%\list2.txt
)
我只是徘徊在代碼中,我只是無意識地做了這件事。 但它確實可以解決問題。
也許以下批處理代碼適用於您想要執行的操作:
@echo off
rem Define environment variable "token" with an example string.
set token=Line1QMZLine2QMZLine3
setlocal EnableDelayedExpansion
:NextLine
for /F "delims=QMZ tokens=1,*" %%I in ("!token!") do (
echo %%I>>"%tmp%\list2.txt"
set "token=%%J"
if not "!token!"=="" goto NextLine
)
endlocal
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.