Batch file 使用批处理文件编辑文件
我有一个小脚本来编辑.conf文件中的文本Batch file 使用批处理文件编辑文件,batch-file,edit,Batch File,Edit,我有一个小脚本来编辑.conf文件中的文本 SETLOCAL=ENABLEDELAYEDEXPANSION rename c:\users\administrator\desktop\httpd.conf text.tmp for /f %%a in (text.tmp) do ( set foo=%%a if !foo!=="### Section 3: Virtual Hosts" set foo="SSL Compression off"
SETLOCAL=ENABLEDELAYEDEXPANSION
rename c:\users\administrator\desktop\httpd.conf text.tmp
for /f %%a in (text.tmp) do (
set foo=%%a
if !foo!=="### Section 3: Virtual Hosts" set foo="SSL Compression off"
echo !foo! >> c:\users\administrator\desktop\httpd.conf)
del text.tmp
它没有达到预期效果,因为它似乎从文件中删除了大量数据。有没有其他方法可以做到这一点
我只需要将第3节:虚拟主机替换为关闭SSL压缩,同时保持文件的完整性。当前脚本似乎也删除了空格:
非常感谢试试这个:
@echo off
setlocal
call :FindReplace "### Section 3: Virtual Hosts" "SSL Compression off" httpd.conf
exit /b
:FindReplace <findstr> <replstr> <file>
set tmp="%temp%\tmp.txt"
If not exist %temp%\_.vbs call :MakeReplace
for /f "tokens=*" %%a in ('dir "%3" /s /b /a-d /on') do (
for /f "usebackq" %%b in (`Findstr /mic:"%~1" "%%a"`) do (
echo(&Echo Replacing "%~1" with "%~2" in file %%~nxa
<%%a cscript //nologo %temp%\_.vbs "%~1" "%~2">%tmp%
if exist %tmp% move /Y %tmp% "%%~dpnxa">nul
)
)
del %temp%\_.vbs
exit /b
:MakeReplace
>%temp%\_.vbs echo with Wscript
>>%temp%\_.vbs echo set args=.arguments
>>%temp%\_.vbs echo .StdOut.Write _
>>%temp%\_.vbs echo Replace(.StdIn.ReadAll,args(0),args(1),1,-1,1)
>>%temp%\_.vbs echo end with
我已将您的路径名和文件名替换为适合我的系统的名称。您需要替换分配给originalpath和originalname的值以适合您的系统
请注意,ECHO是故意的-此字符序列中的字符不影响语句分组。这使用了一个名为repl.bat的助手批处理文件,来自- 将repl.bat与批处理文件放在同一文件夹中,或放在路径上的文件夹中 搜索项是一个正则表达式,但您的项应该可以正常工作
@echo off
type text.tmp | repl "### Section 3: Virtual Hosts" "SSL Compression off" > c:\users\administrator\desktop\httpd.conf
首先将text.tmp do中的FOR行更改为:FOR/f delims=%%a
@echo off
type text.tmp | repl "### Section 3: Virtual Hosts" "SSL Compression off" > c:\users\administrator\desktop\httpd.conf