Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 输入一个查找值列表,并用批处理替换值_Windows_Batch File_Replace_Find - Fatal编程技术网

Windows 输入一个查找值列表,并用批处理替换值

Windows 输入一个查找值列表,并用批处理替换值,windows,batch-file,replace,find,Windows,Batch File,Replace,Find,我正在寻找一种使用Windows批处理脚本查找和替换文本文件中多个单词的方法 我知道用这个bat脚本可以替换一个单词: @echo off &setlocal set "search=%1" set "replace=%2" set "textfile=Input.txt" set "newfile=Output.txt" (for /f "delims=" %%i in (%textfile%) do ( set "line=%%i" setlocal enablede

我正在寻找一种使用Windows批处理脚本查找和替换文本文件中多个单词的方法

我知道用这个bat脚本可以替换一个单词:

@echo off &setlocal
set "search=%1"
set "replace=%2"
set "textfile=Input.txt"
set "newfile=Output.txt"
(for /f "delims=" %%i in (%textfile%) do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    echo(!line!
    endlocal
))>"%newfile%"
del %textfile%
rename %newfile%  %textfile%
现在,我只需再进一步,输入搜索并替换与此格式类似的另一个文件中的字符串:

search_string1, replace_string1
search_string2, replace_string2
search_string3, replace_string3
.
.
.

我认为我会以某种方式逐行处理文件,并将它们解析为两个变量(search、replace),然后将其输入到上面的脚本中。有什么想法吗?我不熟悉Windows批处理脚本,以前从未真正编写过批处理脚本,所以请注意我的新手问题。

这种类型的文本替换速度很慢,通过批处理文件执行时容易失败。我编写了FindRepl.bat程序,这是一个批处理JScript混合脚本,它不仅运行得更快而且没有错误,而且还允许在数据文件的一个处理过程中执行您正在寻找的多个替换。JScript是一种编程语言,包含在XP上的所有Windows版本中。使用FindRepl.bat程序,您可以通过以下方式解决问题:

@echo off
setlocal EnableDelayedExpansion
set "search="
set "replace="
for /F "tokens=1,2 delims=," %%a in (replacements.txt) do (
   set "search=!search!|%%a"
   set "replace=!replace!|%%b"
)
set "search=!search:~1!"
set "replace=!replace:~1!"
< Input.txt FindRepl =search /A =replace > Output.txt
@echo关闭
setlocal EnableDelayedExpansion
设置“搜索=”
设置“替换=”
对于(replacements.txt)中的/F“tokens=1,2 delims=,”%%a(
设置“搜索=!搜索!|%%a”
设置“替换=!替换!|%%b”
)
设置“搜索=!搜索:~1!”
设置“替换=!替换:~1!”
Output.txt
请注意,替换文件中逗号后的所有文本都是替换字符串,包括空格

您可以从下载FindRepl.bat程序。将它放在上一个程序的同一文件夹中,或者更好的是放在%PATH%中包含的文件夹中,这样您就可以直接使用它了