我想在使用批处理文件的特定行之后在批处理文件中插入一行命令(mkdir / copy)。(mkdir / copy命令应该被视为一个单词而不是命令)
输入:
设置MTBBankpath = C:\ InstallerOutput \ QuickBooks-Sync \ MTB
copy / Y%QBprovisionpath%\ x86 \ Debug%ConnectorExecutionPath%\ x86 \ Debug
输出:
设置MTBBankpath = C:\ InstallerOutput \ QuickBooks-Sync \ MTB
copy / Y%ConnectorExecutionPath%\%outqbsyncpath%
mkdir / Y%ConnectorExecutionPath%\%outqbsyncpath%
copy / Y%QBprovisionpath%\ x86 \ Debug%ConnectorExecutionPath%\ x86 \ Debug
新行 copy / Y%ConnectorExecutionPath%\%outqbsyncpath% - 具有复制命令和 mkdir / Y%ConnectorExecutionPath%\%outqbsyncpath%,其中包含mkdir命令,在特定行之后插入设置MTBBankpath = C:\ InstallerOutput \ QuickBooks-Sync \ MTB
SETLOCAL ENABLEDELAYEDEXPANSION
设置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
设置i = 0
FOR / F“usebackq tokens = 1 delims = []”%% A IN(
FIND /N "%_strFind%" "%inputFile%"
)DO(设置_strNum = %% A)
FOR / F“usebackq delims =”%% A IN(“%inputFile%”)DO(
设置/ a i =!i! + 1
ECHO %% A>>“%outputFile%”
如果[!i!] == [%_strNum%](
ECHO%_strInsert%>>“%outputFile%”
ECHO我也想添加这条线>>“%outputFile%”
ECHO OOOO此LiNE TOO>>“%outputFile%”
ECHO ZOMGBBQSAUCE还添加此行>>“%outputFile%”
)
)
如果我更改了设置_strInsert = copy / Y%ConnectorExecutionPath%\%outqbsyncpath%或者设置_strInsert = mkdir%,则上述代码无效ConnectorExecutionPath%\%outqbsyncpath%
请为此提出解决方案。
答案 0 :(得分:1)
@echo off
set "particularLine=set MTBBankpath=C:\InstallerOutput\QuickBooks-Sync\MTB"
setlocal DisableDelayedExpansion
if exist output.bat del output.bat
for /F "delims=" %%a in (input.bat) do (
echo %%a
set "line=%%a"
setlocal EnableDelayedExpansion
if "!line!" == "!particularLine!" (
echo copy /Y %%ConnectorExecutionPath%%\%%outqbsyncpath%%
echo mkdir /Y %%ConnectorExecutionPath%%\%%outqbsyncpath%%
)
endlocal
) >> output.bat
以前的批处理文件有几个缺点:它会删除空行,如果该行包含引号,则可能会失败。
编辑:添加了新版本
如果输入文件很大,下面的批处理文件运行得更快;它还修复了一些细节,比如不删除空行。
@echo off
setlocal EnableDelayedExpansion
set "inputFile=%userprofile%\desktop\testSO.bat"
set "outputFile=%userprofile%\desktop\testSOout.bat"
set "particularLine=set MTBBankpath=C:\InstallerOutput\QuickBooks-Sync\MTB"
for /F "usebackq delims=:" %%a in (`findstr /N /C:"!particularLine!" "%inputFile%"`) do set theLine=%%a
if exist "%outputFile%" del "%outputFile%"
if not defined theLine echo The particular line doesn't exist in Input file & exit /B
setlocal DisableDelayedExpansion
set i=0
for /F "usebackq delims=" %%a in (`findstr /N "^" "%inputFile%"`) do (
set "line=%%a"
set /A i+=1
setlocal EnableDelayedExpansion
echo(!line:*:=!
if !i! eql %theLine% goto exitLoop
endlocal
) >> "%outputFile%"
:exitLoop
rem Insert here all the lines to insert, each one preceeded by ECHO
(
echo copy /Y "%%ConnectorExecutionPath%%\%%outqbsyncpath%%"
echo mkdir /Y "%%ConnectorExecutionPath%%\%%outqbsyncpath%%"
) >> "%outputFile%"
setlocal DisableDelayedExpansion
for /F "skip=%theLine% usebackq delims=" %%a in (`findstr /N "^" "%inputFile%"`) do (
set "line=%%a"
setlocal EnableDelayedExpansion
echo(!line:*:=!
endlocal
) >> "%outputFile%"
请注意,您必须将要插入的命令中的百分号加倍;否则插入的是变量的当前值,而不是变量的%name%。
答案 1 :(得分:0)
我相信您遇到的唯一问题是您没有封装COPY
和MKDIR
命令的路径:
set _strInsert=mkdir %ConnectorExecutionPath%\%outqbsyncpath%
尝试:
set _strInsert=mkdir "%ConnectorExecutionPath%\%outqbsyncpath%"
我也会在创建之前检查文件夹。
编辑:
现在,如果您要添加一堆不同的行,只需将它们添加到代码中即可。在上一篇文章中,您要求在代码中添加一行。如果你要做多行,只需将它们直接回显到输出文件中即可。
SETLOCAL ENABLEDELAYEDEXPANSION
set inputFile=%userprofile%\desktop\testSO.bat
set outputFile=%userprofile%\desktop\testSOout.bat
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 set IndbBankpath=C:\InstallerOutput\QuickBooks-Sync\indb
ECHO copy /Y "%ConnectorExecutionPath%\%outqbsyncpath%"
ECHO mkdir /Y "%ConnectorExecutionPath%\%outqbsyncpath%"
ECHO copy /Y "%QBprovisionpath%\x86\Debug %ConnectorExecutionPath%\x86\Debug"
)>>"%outputFile%"
)