我想在特定行之后的批处理文件中插入一行 输入:
set MTBBankpath=C:\InstallerOutput\QuickBooks-Sync\MTB
echo To copy QuickBooks-Sync COM components into resultant folder
copy /Y %ConnectorExecutionPath%\%outqbsyncpath%
输出:
set MTBBankpath=C:\InstallerOutput\QuickBooks-Sync\MTB
set IndbBankpath=C:\InstallerOutput\QuickBooks-Sync\indb
echo To copy QuickBooks-Sync COM components into resultant folder
copy /Y %ConnectorExecutionPath%\%outqbsyncpath%
在特定行之后插入新行set IndbBankpath=C:\InstallerOutput\QuickBooks-Sync\indb
。
答案 0 :(得分:3)
SETLOCAL ENABLEDELAYEDEXPANSION
set inputFile=%userprofile%\desktop\testSO.bat
set outputFile=%userprofile%\desktop\testSOout.bat
set _strInsert=set IndbBankpath=C:\InstallerOutput\QuickBooks-Sync\indb
set _strFind=set MTBBankpath=C:\InstallerOutput\QuickBooks-Sync\MTB
FOR /F "usebackq delims=" %%A IN ("%inputFile%") DO (
Echo %%A | Find "%_strFind%" && ECHO %%A>>"%outputFile%" && ECHO %_strInsert%>>"%outputFile%"
IF [!errorlevel!] == [1] ECHO %%A>>"%outputFile%"
)
基本上,对于文件中的每一行,对其中的字符串执行FIND
,如果找到它,将字符串回显到out文件中,然后回显要插入到的文件中OUTFILE。如果它没有在该字符串中找到您要查找的内容,则只会将该字符串回显到输出文件中。
编辑:如果文件相当大......这可能会快一点
SETLOCAL ENABLEDELAYEDEXPANSION
set inputFile=%userprofile%\desktop\testSO.bat
set outputFile=%userprofile%\desktop\testSOout.bat
set _strInsert=set IndbBankpath=C:\InstallerOutput\QuickBooks-Sync\indb
set _strFind=set MTBBankpath=C:\InstallerOutput\QuickBooks-Sync\MTB
set i=0
FOR /F "usebackq tokens=1 delims=[]" %%A IN (`FIND /N "%_strFind%" "%inputFile%"`) DO (set _strNum=%%A)
FOR /F "usebackq delims=" %%A IN ("%inputFile%") DO (
set /a i = !i! + 1
ECHO %%A>>"%outputFile%"
IF [!i!] == [%_strNum%] (ECHO %_strInsert%>>"%outputFile%")
)
如果你想“假装”它正在写入同一个文件,请将其添加到脚本的末尾。
MOVE /Y "%outputFile%" "%inputFile%" && DEL /F /Q "%outputFile%"
编辑:我可以有更多的LYNES!?
SETLOCAL ENABLEDELAYEDEXPANSION
set inputFile=%userprofile%\desktop\testSO.bat
set outputFile=%userprofile%\desktop\testSOout.bat
set _strInsert=set IndbBankpath=C:\InstallerOutput\QuickBooks-Sync\indb
set _strFind=set MTBBankpath=C:\InstallerOutput\QuickBooks-Sync\MTB
set i=0
FOR /F "usebackq tokens=1 delims=[]" %%A IN (`FIND /N "%_strFind%" "%inputFile%"`) DO (set _strNum=%%A)
FOR /F "usebackq delims=" %%A IN ("%inputFile%") DO (
set /a i = !i! + 1
ECHO %%A>>"%outputFile%"
IF [!i!] == [%_strNum%] (
ECHO %_strInsert%>>"%outputFile%"
ECHO I WANT TO ADD THIS LINE ALSO>>"%outputFile%"
ECHO OOOO THIS LiNE TOO>>"%outputFile%"
ECHO ZOMGBBQSAUCE ADD THIS LINE ALSO>>"%outputFile%"
)
)