使用批处理文件在批处理文件中的特定行之后插入新行(有命令)

发布于 2025-01-07 21:43:13 字数 1823 浏览 0 评论 0原文

我想使用批处理文件在特定行之后插入批处理文件中包含命令(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=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 (
设置 /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=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%"
)
)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晒暮凉 2025-01-14 21:43:13
@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%。

@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

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.

@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%"

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.

望笑 2025-01-14 21:43:13

我相信您遇到的唯一问题是您没有封装 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%"
)

I believe the only problem you're running into is the fact that you're not encapsulating your paths for the COPY and MKDIR 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.

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%"
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文