Batch file 使用.bat逐行修改.txt

Batch file 使用.bat逐行修改.txt,batch-file,Batch File,我有一个.txt文件: Previsto: R$ 9.766,53 Previsto: R$ 423,65 Previsto: R$ 514,51 Previsto: R$ 492,63 所以我需要将单词“Previsto”改成另一个单词,比如: Visa: R$ 9.766,53 Elo: R$ 423,65 Hipercard: R$ 514,51 Sorocred: R$ 492,63 我做了这个.bat脚本: @echo off setloca

我有一个
.txt
文件:

Previsto:  R$ 9.766,53  
Previsto:  R$ 423,65  
Previsto:  R$ 514,51  
Previsto:  R$ 492,63  
所以我需要将单词“Previsto”改成另一个单词,比如:

Visa: R$ 9.766,53

Elo: R$ 423,65

Hipercard: R$ 514,51

Sorocred: R$ 492,63
我做了这个
.bat
脚本:

@echo off 
    setlocal enableextensions disabledelayedexpansion
    set "search=%Previsto"
    set "replace=%Visa"
    set "textFile=extract.txt"

    for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
        set "line=%i"
        setlocal enabledelayedexpansion
        >>"%textFile%" echo(!line:%search%=%replace%!
        endlocal
    )
但所有的行都在将“Previsto”替换为“Visa” 我不知道如何将它设置为仅将第一行更改为“Visa”,第二行更改为“Elo”,第三行更改为“Hipercard”,等等

我怎么做

谢谢

试着这样做:

@echo off
setlocal enabledelayedexpansion

set "$file=extract.txt"
set "$search=Previsto"

set $repl[1]=Visa
set $repl[2]=Elo
set $repl[3]=Hipercard
set $repl[4]=Sorocred

set $count=1

(for /f "delims=" %%a in (%$file%) do (
   call:replace "%%a" !$count!
   set/a $count+=1
   )
)>out.txt

echo done..
exit/b

:replace

set "$line=%~1"
set $repl=!$repl[%2]!
set "$line=!$line:%$search%=%$repl%!"
echo !$line!
输出文件是
out.txt

编辑2

>第1行

后的空白行
:replace

set "$line=%~1"
set $repl=!$repl[%2]!
set "$line=!$line:%$search%=%$repl%!"
echo !$line!
if "%2"=="1" echo.
试着这样做:

@echo off
setlocal enabledelayedexpansion

set "$file=extract.txt"
set "$search=Previsto"

set $repl[1]=Visa
set $repl[2]=Elo
set $repl[3]=Hipercard
set $repl[4]=Sorocred

set $count=1

(for /f "delims=" %%a in (%$file%) do (
   call:replace "%%a" !$count!
   set/a $count+=1
   )
)>out.txt

echo done..
exit/b

:replace

set "$line=%~1"
set $repl=!$repl[%2]!
set "$line=!$line:%$search%=%$repl%!"
echo !$line!
输出文件是
out.txt

编辑2

>第1行

后的空白行
:replace

set "$line=%~1"
set $repl=!$repl[%2]!
set "$line=!$line:%$search%=%$repl%!"
echo !$line!
if "%2"=="1" echo.

我有一个与SachaDee类似的想法,但创建数组的方法不同 而且没有子程序

:: Q:\Test\2018\06\04\SO_50684988.cmd
@Echo off&SetLocal EnableExtensions EnableDelayedExpansion

Rem Set replace[0..3] to cards
Set i=-1&Set "replace= Visa Elo Hipercard Sorocred"
Set "replace=%replace: ="&Set /a i+=1&Set "replace[!i!]=%"

set "search=Previsto"
set "textFile=extract.txt"
set /A "cnt=pnt=0,i+=1"

(for /f "tokens=1* delims=:" %%A in ('type "%textFile%" ') do (
  if /i "%%A" equ "%search%" call Echo=%%replace[!pnt!]%%:%%B
  if !pnt!==0 Echo=
  Set /A "cnt+=1,pnt=cnt %% i"
) ) >"New_%textFile%

type "New_%textFile%"
visa后带有空行的样本输出:

>SO_50684988.cmd
Visa:  R$ 9.766,53

Elo:  R$ 423,65
Hipercard:  R$ 514,51
Sorocred:  R$ 492,63

批处理将依次替换任意数量的Previsto。

我的想法与SachaDee类似,但创建数组的方法不同 而且没有子程序

:: Q:\Test\2018\06\04\SO_50684988.cmd
@Echo off&SetLocal EnableExtensions EnableDelayedExpansion

Rem Set replace[0..3] to cards
Set i=-1&Set "replace= Visa Elo Hipercard Sorocred"
Set "replace=%replace: ="&Set /a i+=1&Set "replace[!i!]=%"

set "search=Previsto"
set "textFile=extract.txt"
set /A "cnt=pnt=0,i+=1"

(for /f "tokens=1* delims=:" %%A in ('type "%textFile%" ') do (
  if /i "%%A" equ "%search%" call Echo=%%replace[!pnt!]%%:%%B
  if !pnt!==0 Echo=
  Set /A "cnt+=1,pnt=cnt %% i"
) ) >"New_%textFile%

type "New_%textFile%"
visa后带有空行的样本输出:

>SO_50684988.cmd
Visa:  R$ 9.766,53

Elo:  R$ 423,65
Hipercard:  R$ 514,51
Sorocred:  R$ 492,63

该批次将依次用替换品替换任意数量的Previsto。

这些词从哪里取?你有文本文件吗?这些词从哪里来?你有文本文件吗?这没用。“Previsto”的每个字都是“Visa”的。我只需要签证上的第一个“Previsto”tur。第二个“Previsto”改为“Elo”。等等,非常好!!它工作得很好!!!还有一个问题:我想在第一行后面放一个空行,在这种情况下,在“Visa”行后面…我怎么做?谢谢太神了谢谢我无法在“%userprofile%/Desktop/”中保存文件。为什么?这不起作用。“Previsto”的每个字都是“Visa”的。我只需要签证上的第一个“Previsto”tur。第二个“Previsto”改为“Elo”。等等,非常好!!它工作得很好!!!还有一个问题:我想在第一行后面放一个空行,在这种情况下,在“Visa”行后面…我怎么做?谢谢太神了谢谢我无法在“%userprofile%/Desktop/”中保存文件。为什么?很好@LotPings@SachaDee谢谢,同样+1;-)漂亮的一个@LotPings@SachaDee谢谢,同样+1;-)