批处理文件批处理脚本中的SubString替换

时间:2014-04-10 12:42:45

标签: batch-file scripting

您好我正在尝试使用batchscript替换文本文件的内容。

setlocal enableextensions enabledelayedexpansion
set line=0
set position=0
set replacetoken=POSITION
set newfile = new.txt
for /f "tokens=* delims=" %%x in (resproprty.txt)  do  (
@echo off
set /a line+=1
if not !line!==1 (
set /a position+=1
)
set newcount=0
set newcount=!position!
set content=%%x
echo !content!
set content=!content:%replacetoken%=POSITION@!position!!
echo !newcount!
echo !position!
@echo on
echo !content! >> NEW.TXT
)
set test=Hello!
echo %test%
endlocal

在这里,我正在尝试将POSITION替换为POSITION@1,即正在递增的POSITION@number。但它不起作用!位置!我没有给出任何输出,我甚至试过%position%,但只是输出为0.请帮助

2 个答案:

答案 0 :(得分:0)

试试这样:

@echo off
if exist output.txt del output.txt
setlocal EnableDelayedExpansion
set /a $count=1
for /f "delims=" %%a in ('type "resproprty.txt"') do (
set line=%%a
call set line=!line:POSITION=POSITION@%%$count%%!
echo !line!>>output.txt
set /a $count+=1
)

Echo ok

这将创建完成更改的文件output.txt

答案 1 :(得分:0)

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
(
 FOR /f "tokens=1*delims=:" %%a IN ('findstr /n /r "." q22988413.txt') DO (
 SET "line=%%b"
 SET "line=!line:position=position@%%a!"
 ECHO(!line!
 )
)>newfile.txt

GOTO :EOF

我使用了一个名为q22988413.txt的文件,其中包含

Hello position one
Hello position two
Hello position three
Hello position four
Hello position five

newfile.txt

中产生了此输出
Hello position@1 one
Hello position@2 two
Hello position@3 three
Hello position@4 four
Hello position@5 five