我正在使用Windows命令行环境编写一个批处理文件脚本,并希望更改文件中每次出现的一些文本(例如。“FOO”)和另一个(ex。“酒吧”)。最简单的方法是什么?有内置函数吗?


当前回答

@Rachel给出了一个很好的答案,但这里是它的一个变体,将内容读取到powershell $data变量。然后,在写入输出文件之前,您可以轻松地多次操作内容。还可以了解如何在.bat批处理文件中给出多行值。

@REM ASCII=7bit ascii(no bom), UTF8=with bom marker
set cmd=^
  $old = '\$Param1\$'; ^
  $new = 'Value1'; ^
  [string[]]$data = Get-Content 'datafile.txt'; ^
  $data = $data -replace $old, $new; ^
  out-file -InputObject $data -encoding UTF8 -filepath 'datafile.txt';
powershell -NoLogo -Noninteractive -InputFormat none -Command "%cmd%"

其他回答

下面是我在Win XP上发现的一个解决方案。在我运行的批处理文件中,我包括以下内容:

set value=new_value

:: Setup initial configuration
:: I use && as the delimiter in the file because it should not exist, thereby giving me the whole line
::
echo --> Setting configuration and properties.
for /f "tokens=* delims=&&" %%a in (config\config.txt) do ( 
  call replace.bat "%%a" _KEY_ %value% config\temp.txt 
)
del config\config.txt
rename config\temp.txt config.txt

替换后的。bat文件如下所示。我没有找到在同一个批处理文件中包含该函数的方法,因为%%a变量似乎总是给出for循环中的最后一个值。

replace.bat:

@echo off

:: This ensures the parameters are resolved prior to the internal variable
::
SetLocal EnableDelayedExpansion

:: Replaces Key Variables
::
:: Parameters:
:: %1  = Line to search for replacement
:: %2  = Key to replace
:: %3  = Value to replace key with
:: %4  = File in which to write the replacement
::

:: Read in line without the surrounding double quotes (use ~)
::
set line=%~1

:: Write line to specified file, replacing key (%2) with value (%3)
::
echo !line:%2=%3! >> %4

:: Restore delayed expansion
::
EndLocal

dostips.com上的BatchSubstitute.bat是一个使用纯批处理文件进行搜索和替换的示例。

它使用FOR、FIND和CALL SET的组合。

包含“&<>]|^”字符的行可能会被错误处理。


我是Aba搜索和替换的作者,你可以从Windows命令行使用。它可以在没有任何用户交互的情况下进行批量替换,也可以替换多个文件中的文本,而不仅仅是一个文件。

欢迎您试用我的工具;我很乐意回答任何问题。

这是批处理脚本做得不好的一点。

更多辣椒链接到的脚本可以用于某些文件,但不幸的是,它会阻塞那些包含管道和&号等字符的文件。

VBScript是一个更好的内置工具。请看这篇文章的例子: http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb05/hey0208.mspx

我知道我迟到了。

就我个人而言,我喜欢以下的解决方案: ——http://www.dostips.com/DtTipsStringManipulation.php Snippets.Replace

我们还广泛使用重复数据删除功能,帮助我们每天通过SMTP发送大约500封电子邮件: ——https://groups.google.com/forum/ # ! / alt.msdos.batch.nt / sj8IUhMOq6o话题

这些都是本地工作,不需要额外的工具或实用程序。

替代者:

DEL New.txt
setLocal EnableDelayedExpansion
For /f "tokens=* delims= " %%a in (OLD.txt) do (
Set str=%%a
set str=!str:FOO=BAR!
echo !str!>>New.txt
)
ENDLOCAL

DEDUPLICATOR(注意使用-9作为ABA编号):

REM DE-DUPLICATE THE Mapping.txt FILE
REM THE DE-DUPLICATED FILE IS STORED AS new.txt

set MapFile=Mapping.txt
set ReplaceFile=New.txt

del %ReplaceFile%
::DelDupeText.bat
rem https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/sj8IUhMOq6o
setLocal EnableDelayedExpansion
for /f "tokens=1,2 delims=," %%a in (%MapFile%) do (
set str=%%a
rem Ref: http://www.dostips.com/DtTipsStringManipulation.php#Snippets.RightString
set str=!str:~-9!
set str2=%%a
set str3=%%a,%%b

find /i ^"!str!^" %MapFile%
find /i ^"!str!^" %ReplaceFile%
if errorlevel 1 echo !str3!>>%ReplaceFile%
)
ENDLOCAL

谢谢!