我有一个批量文件, 我想打开一个文件, 并做一个非常简单的搜索, 替换该文件, 然后保存( 覆盖) 它搜索和替换的文件 。
我知道如何阅读文件的第一行:
set /p file= < file.txt
但却在努力使用批量方法 读取整个文件 并搜索/替换文件
我有一个批量文件, 我想打开一个文件, 并做一个非常简单的搜索, 替换该文件, 然后保存( 覆盖) 它搜索和替换的文件 。
我知道如何阅读文件的第一行:
set /p file= < file.txt
但却在努力使用批量方法 读取整个文件 并搜索/替换文件
用您的文件名和正确的搜索/替换字符串或文字替换文件名.txt和搜索1/replace1。
Echo off
set "textfile=filename.txt"
set "tempfile=filenametemp.txt"
(for /f "delims=" %%i in (%textfile%) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:search1=replace1!"
echo(!line!
endlocal
))>"%tempfile%"
del %textfile%
rename %tempfile% %textfile%
很难给出一个明确答案, 因为它不清楚您所说的“ 简单搜索” 是什么意思, 并替换“ / 搜索”, 但这里需要所有的元素 :
<强度 > 读取文件 强度>
您可以用 for /f
来用行读取文件行 :
for /f %%a in (myfile.txt) do (
echo %%a
)
<强度 > 替换文本 强度 >
我知道如何取代文字出现:
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%a in (myfile.txt) do (
SET s=%%a
SET s=!s:stringtoreplace=replacementstring!
echo !s!
)
这将输出所有 myfiles.txt
的行, 并替换 string to replace , 替换为 replaceting 。
<强 > 将文件 强> 替换为文件 强>
您可以考虑先将文件写入新的临时文件,然后用新的临时文件替换原始文件。
使用上述示例,它简单到在 echo
语句后添加
文件 强> 的 < strig> Filter 线
如果您只想从文件中删除某些行, 您也可以考虑 findstr
命令 ( help findstr
)。
要添加到
(我还添加了一个 finalfilename
变量, 因为我想要将原始文件保持未动, 并仅生成带有输出的新文件。 如果您想要覆盖原始文件的话, 请设置与源文件名称相同 ) em>
@ECHO OFF
set "sourcefile=sourcefilename.txt"
set "tempfile=filenametemp.txt"
set "finalfilename=outputfile.txt"
ECHO Enter search term to find:
set /p search1=
ECHO Enter replacement string:
set /p replace1=
ECHO Finding %search1% and replacing with %replace1%
(for /f "delims=" %%i in (%sourcefile%) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search1%=%replace1%!"
echo(!line!
endlocal
))>"%tempfile%"
:: Delete potential existing final file to avoid duplicate error when renaming
del %finalfilename%
ECHO Setting filename to %finalfilename%
rename %tempfile% %finalfilename%
ECHO Done
:: Pause to see messages
PAUSE
您可以在文件中进行搜索和替换,或者使用硬编码字符串或正则表达式使用VBScript(可以通过批量脚本执行)进行搜索和替换。
以下是使用硬码搜索标准进行搜索和替换的例子:
<job>
<script language="JavaScript">
var fso = new ActiveXObject("Scripting.FileSystemObject");
var MyFile = fso.GetFile("c:\file.json");
var txt = MyFile.OpenAsTextStream(1).ReadAll();
txt = txt.replace("criteria", "replacing value");
WScript.echo("Replaced File"+txt);
var out = MyFile.OpenAsTextStream(2);
out.write(txt);
</script>
将此脚本保存在名为 替换.wsf 的文件中。 您可以从这样的批量文件中执行脚本( 在 Windows 操作系统中):
pushd %~dp0
cscript c:
eplace.wsf
如果您需要基于一个正则表达式进行搜索和替换, 我建议查看此示例 : < a href="https://stackoverflow.com/ questions/ 7023460/regex- search- replace- in- batch > regex search 替换成批量
I m trying to do some batch replacement for/with a fairly complex pattern So far I find the pattern as: find ( -name *.php -o -name *.html ) -exec grep -i -n hello {} + The string I want ...
I am trying to retrieve the environment variable to detect whether the system is 32 or 64 bit. But on 64 bit server the environment variable %processor_architecture% is returning x86 instead of AMD64. ...
IF %processor_architecture% == AMD64 (SET querypath=hklmsoftwarex) ELSE (SET dsetquerypath=hklmsoftwarey) FOR /F "tokens=* delims=" %%G IN ( REG QUERY "%querypath%" 2^>NUL ) DO ECHO %%G Let ...
I have a batch script that takes arguments from the command line. One of the arguments has a * in it. In spite of putting the argument in quotes, the * gets expanded before the argument gets used in ...
Suppose I have a string "AAA BBB CCC DDD EEE FFF". How can I split the string and retrieve the nth substring, in a batch file? The equivalent in C# would be "AAA BBB CCC DDD EEE FFF".Split()[n]
Binary files have a version embedded in them - easy to display in Windows Explorer. How can I retrieve that file version, from a batch file?
How can I check OS version in a batch file or through a vbs in an Windows 2k/2k3 environment ? You know ... Something like ... : "If winver Win2k then ... or if winver Win2k3 then ....
I don t know how to describe exactly what I m trying to do but here s an example batch file that demonstrates what I can t figure out.: I ve got a batch file. Inside that batch file I m trying to ...