本帖最后由 WHY 于 2023-12-1 11:17 编辑
另一种办法:- gawk -v IGNORECASE=1 "NR==FNR{a[NR]=$0;next}!n{print}!n&&/^:abcd$/{for(i in a)print a[i];n=1}n&&/^goto :EOF$/{print;n=0}" NewTxt.txt 1.bat > 2.bat
复制代码 也练习下纯批
改一下,少一个endlocal- @echo off
- for /f "delims=" %%i in ('findstr /n .* 1.bat') do (
- set "s=%%i"
- setlocal enabledelayedexpansion
- set "s=!s:*:=!"
- if not defined flag (
- echo;!s!
- if /i "!s!" == ":abcd" (
- endlocal
- type NewTxt.txt | find /v ""
- set flag=1
- ) else (
- endlocal
- )
- ) else if /i "!s!" == "goto :EOF" (
- echo;!s!
- endlocal
- set "flag="
- ) else (
- endlocal
- )
- )
- pause
复制代码 Test.js- var txtFileName = 'NewTxt.txt'; //txt文件名
- var batFileName = '1.bat'; //修改前bat文件名
- var dstFileName = '2.bat'; //修改后bat文件名
-
- var fso = new ActiveXObject('Scripting.FileSystemObject');
-
- var s1 = fso.OpenTextFile(txtFileName, 1).ReadAll(); //读txt文本内容
- var s2 = fso.OpenTextFile(batFileName, 1).ReadAll(); //读bat文本内容
-
- s1 = s1.replace(/(?!\r\n).$/, '$&\r\n'); //txt结尾添加回车换行
- s2 = s2.replace(/^(:abcd\r\n)(?:(?!goto :EOF)[\s\S])+(?=goto :EOF)/mi, '$1' + s1); //替换内容
-
- fso.OpenTextFile(dstFileName, 2, true).Write(s2);
-
- WSH.Echo('Done')
复制代码
|