使用批处理文件在批处理文件中的特定行之后插入新行(有命令)
我想使用批处理文件在特定行之后插入批处理文件中包含命令(mkdir/copy)的行。(mkdir/copy 命令应被视为单词而不是命令)
输入:
设置 MTBBankpath=C:\InstallerOutput\QuickBooks-Sync\MTB
复制 /Y %QBprovisionpath%\x86\Debug %ConnectorExecutionPath%\x86\Debug
输出:
设置 MTBBankpath=C:\InstallerOutput\QuickBooks-Sync\MTB
复制 /Y %ConnectorExecutionPath%\%outqbsyncpath%
mkdir /Y %ConnectorExecutionPath%\%outqbsyncpath%
复制 /Y %QBprovisionpath%\x86\Debug %ConnectorExecutionPath%\x86\Debug
新行 copy /Y %ConnectorExecutionPath%\%outqbsyncpath% - 其中包含复制命令和 mkdir /Y %ConnectorExecutionPath%\%outqbsyncpath% 具有 mkdir 命令,在特定行设置后插入MTBBankpath=C:\InstallerOutput\QuickBooks-Sync\MTB
设置本地启用延迟扩展
设置 inputFile=%userprofile%\desktop\testSO.bat
设置outputFile=%userprofile%\desktop\testSOout.bat
设置 _strInsert=设置 IndbBankpath=C:\InstallerOutput\QuickBooks-Sync\indb
设置 _strFind=设置 MTBBankpath=C:\InstallerOutput\QuickBooks-Sync\MTB
设置 i=0FOR /F "usebackq tokens=1 delims=[]" %%A IN (
FIND /N "%_strFind%" "%inputFile%"
) DO (set _strNum=%%A )
FOR /F "usebackq delims=" %%A IN ("%inputFile%") DO (
设置 /ai = !i! + 1
ECHO %%A>>"%outputFile%"
IF [!i!] == [%_strNum%] (
ECHO %_strInsert%>>“%outputFile%”
ECHO 我也想添加此行>>"%outputFile%"
ECHO OOOO 这行太>>“%outputFile%”
ECHO ZOMGBBQSAUCE 还添加此行>>“%outputFile%”
)
)
如果我更改
set _strInsert=copy /Y %ConnectorExecutionPath%\%outqbsyncpath% 或
set _strInsert=mkdir %ConnectorExecutionPath,上述代码将不起作用%\%outqbsyncpath%
请为此提出解决方案。
I want to insert a line which has a command(mkdir/copy) in a batch file after a particular line using a batch file.(mkdir/copy command should be considered as a word rather than command)
Input:
set MTBBankpath=C:\InstallerOutput\QuickBooks-Sync\MTB
copy /Y %QBprovisionpath%\x86\Debug %ConnectorExecutionPath%\x86\Debug
Output:
set MTBBankpath=C:\InstallerOutput\QuickBooks-Sync\MTB
copy /Y %ConnectorExecutionPath%\%outqbsyncpath%
mkdir /Y %ConnectorExecutionPath%\%outqbsyncpath%
copy /Y %QBprovisionpath%\x86\Debug %ConnectorExecutionPath%\x86\Debug
A New line copy /Y %ConnectorExecutionPath%\%outqbsyncpath% - which has a copy command and mkdir /Y %ConnectorExecutionPath%\%outqbsyncpath% which has mkdir command, get inserted after a particular line set MTBBankpath=C:\InstallerOutput\QuickBooks-Sync\MTB
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=0FOR /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%"
)
)
The above code doesn't work if I change
set _strInsert=copy /Y %ConnectorExecutionPath%\%outqbsyncpath% or
set _strInsert=mkdir %ConnectorExecutionPath%\%outqbsyncpath%
Please suggest a solution for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以前的批处理文件有几个缺点:它会删除空行,如果该行包含引号,则可能会失败。
编辑:添加了新版本
如果输入文件很大,下面的批处理文件运行速度会更快;它还修复了几个细节,例如不删除空行。
请注意,您必须将命令中的百分号加倍才能插入;否则插入的是变量的当前值,而不是变量的%name%。
Previous Batch file has several drawbacks: it remove empty lines and may fail if the line contain quotes.
EDIT: New version added
The Batch file below run faster if the input file is large; it also have several details fixed, like not removing empty lines.
Please note that you must double the percent signs in the commands to insert; otherwise what is inserted is the current value of the variables instead of the %name% of the variables.
我相信您遇到的唯一问题是您没有封装 COPY 和 MKDIR 命令的路径:
set _strInsert=mkdir % ConnectorExecutionPath%\%outqbsyncpath%
尝试:
set _strInsert=mkdir "%ConnectorExecutionPath%\%outqbsyncpath%"
我也会做一个在创建文件夹之前也要检查该文件夹。
编辑:
现在,如果您要添加一堆不同的行,只需将它们添加到代码中即可。在上一篇文章中,您要求在代码中添加一行。如果您要执行多行操作,只需将它们直接回显到输出文件中即可。
I believe the only problem you're running into is the fact that you're not encapsulating your paths for the
COPY
andMKDIR
commands:set _strInsert=mkdir %ConnectorExecutionPath%\%outqbsyncpath%
Try:
set _strInsert=mkdir "%ConnectorExecutionPath%\%outqbsyncpath%"
I would also do a check for the folder before creating it as well.
EDIT:
Now if you're adding in a bunch of different lines, just add them to the code. In your previous post you asked to add a single line to the code. If you're doing multiple lines, just echo them directly into the output file.