Sample input:
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;NEWCODE;% (PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
Intended output:
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_LARGE_;WIN32;_WINDOWS;_DEBUG;NEWCODE;% (PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
Current output when running batch multiple times:
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_LARGE;_LARGE_;WIN32;_WINDOWS;_DEBUG;NEWCODE;% (PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
Batch file:
#echo off
setlocal enableextensions enabledelayedexpansion
set "textFile="C:\"
set "file2=%textFile%\sample.vcxproj"
set list=%file2%
set "WIN32=^<PreprocessorDefinitions^>"
set "LARGE_=_LARGE_;"
set "replace=%WIN32%_LARGE_;"
set "cmd=%1"
set "append=enable"
set "remove=disable"
set "empty="
echo %replace% !WIN32!
(for %%a in (%list%) do (
IF "%cmd%"=="%remove%" (
echo removed
for /f "delims=" %%i in ('type %%a ^& break ^> %%a ') do (
set "line=%%i"
setlocal enabledelayedexpansion
>>"%%a" echo(!line:%LARGE_DICOM%=%empty%!
endlocal
)
)
IF "%cmd%"=="%append%" (
echo append
echo %%a
for /f "delims=" %%i in ('type %%a ^& break ^> %%a ') do (
set "line=%%i"
Echo "%%i" |findstr /R "%replace%" 1>nul
if %errorlevel%==0 (
echo eexist
)
setlocal enabledelayedexpansion
>>"%%a" echo(!line:%WIN32%=%replace%!
endlocal
)
)
echo %%a
))
I am trying to find LARGE in preprocessor, if not exist add it, but it is adding multiple times
Find and, search is not working. I need to append only if it not exist.
Append to file if particular sting not found, string should append once, even if we run batch multiple times.
Related
I have written a batch file which I want to overwrite key strings with strings from another .txt file.
currently it copies the new File.txt file perfectly but does not replace the strings with the strings from OldFile.txt file.
example of strings in File.txt file:
...
# Password
Pword=
# AccountName
Account=
# TownName
Town=
# Postcode
Postcode=
# LocationChangedDate
LocationChanged=
example of strings in OldFile.txt file I want to replace from:
...
# Password
Pword=ABC
# AccountName
Account=123
# TownName
Town=LDN
# Postcode
Postcode=WS77TP
# LocationChangedDate
LocationChanged=01/01/2015
Can someone please point me in the right direction or explain where I have made a mistake?
#echo off
setlocal disableDelayedExpansion
::Variables
set InputFile=F:\EXCHANGE\3\Machine\File.txt
set OutputFile=F:\EXCHANGE\3\File-New.txt
set CopyFile=F:\EXCHANGE\3\OldMachine\OldFile.txt
set _strFindPword=Pword=.*
for /F "delims=" %%A in ('findstr /x "Pword=.*" %CopyFile%') do set _strInsertPword=%%A
echo.%_strInsertPword%
set _strFindAccount=Account=.*
for /F "delims=" %%B in ('findstr /x "Account=.*" %CopyFile%') do set _strInsertAccount=%%B
echo.%_strInsertAccount%
set _strFindTown=Town=.*
for /F "delims=" %%C in ('findstr /x "Town=.*" %CopyFile%') do set _strInsertTown=%%C
echo.%_strInsertTown%
set _strFindLocationChanged=LocationChanged=.*
for /F "delims=" %%D in ('findstr /x "LocationChanged=.*" %CopyFile%') do set _strInsertLocationChanged=%%D
echo.%_strInsertLocationChanged%
set _strFindPostcode=Postcode=.*
for /F "delims=" %%E in ('findstr /x "Postcode=.*" %CopyFile%') do set _strInsertPostcode=%%E
echo.%_strInsertPostcode%
(
for /F "delims=" %%L in ('findstr /n "^" "%InputFile%"') do (
set "line=%%L"
setlocal EnableDelayedExpansion
set "line=!line:*:=!"
if "%%L" equ "_strFindPword" (echo.!_strInsertPword!) else (
if "%%L" equ "%_strFindAccount%" (echo.!_strInsertAccount!) else (
if "%%L" equ "%_strFindTown%" (echo.!_strInsertTown!) else (
if "%%L" equ "%_strFindLocationChanged%" (echo.!_strInsertLocationChanged!) else (
if "%%L" equ "%_strFindPostcode%" (echo.!_strInsertPostcode!) else (echo.!line!)
)
)
)
)
endlocal
)
) > "%OutputFile%"
del %InputFile%
ren %OutputFile% File.txt
pause
I think I finally got it...
What it does:
It goes through the OldFile.txt content, searching for markers, if found they are stored into environment variables to be used in the nest step (e.g. for _PWD marker (variable) which has a value of Pword=, it will create a _PWDCONTENTS variable with the content of Pword=ABC).
It goes through File.txt content, searching for the same markers, if one marker found, the corresponding CONTENTS variable is dumped in the OutFile.txt, else the original line. Because that happens in the inner for loop, I had to add some extra logic (the _WROTE var) to avoid writing the same lines more than once.
Notes:
It is supposed (well, besides doing what it's supposed to) to be "configurable" (the code is complicated, it's heading towards meta :) if you will), meaning that if there are changes between the markers the code shouldn't change (well there would be code changes, but not in the functional part only in variable definitions). Let me detail:
If you no longer need to replace the Town= string, then all you have to do is removing _TOWN from _ALL: set _ALL=_PWD _ACCT _POST _LOC.
The reverse: if you want to add some other tag (let's call it Name), you have to create a new environment variable: set _NAME=Name= and add it to _ALL: set _ALL=_PWD _ACCT _TOWN _POST _LOC _NAME.
As an indirect consequence, I didn't focus on performance, so it might run slow. Anyway I tried to keep the disk accesses (which are painfully slow) to a minimum (one example is when having 2 for loops the one that iterates on a file contents - assuming that each iteration takes a disk access; this might not be true, and Win has IO buffering - it's the outer one).
I "commented" out the last line in the file, to avoid overwriting the original file. If that behavior is needed, simply remove the rem at the beginning.
Here's the batch code:
#echo off
setlocal enabledelayedexpansion
set _INFILE="File.txt"
set _OUTFILE="NewFile.txt"
set _OLDFILE="OldFile.txt"
set _PWD=Pword=
set _ACCT=Account=
set _TOWN=Town=
set _POST=Postcode=
set _LOC=LocationChanged=
set _ALL=_PWD _ACCT _TOWN _POST _LOC
echo Parsing old file contents...
for /f "tokens=*" %%f in ('type !_OLDFILE!') do (
for %%g in (!_ALL!) do (
echo %%f | findstr /b /c:!%%g! 1>nul
if "!errorlevel!" equ "0" (
set %%gCONTENTS=%%f
)
)
)
copy nul %_OUTFILE%
echo Merging the old file contents into the new file...
set _WROTE=0
for /f "tokens=*" %%f in ('findstr /n "^^" !_INFILE!') do (
set _TMPVAR0=%%f
set _TMPVAR0=!_TMPVAR0:*:=!
for %%g in (!_ALL!) do (
echo !_TMPVAR0! | findstr /b /c:!%%g! 1>nul
if "!errorlevel!" equ "0" (
echo.!%%gCONTENTS!>>!_OUTFILE!
set _WROTE=1
)
)
if "!_WROTE!" equ "0" (
echo.!_TMPVAR0!>>!_OUTFILE!
) else (
set _WROTE=0
)
)
rem copy /-y %_OUTFILE% %_INFILE%
#EDIT0: Using #StevoStephenson suggestion (as part of the question snippet), I replaced the (2nd) outer for loop to ('findstr /n "^^" !_INFILE!') in order to include the empty lines, so the 3rd remark no longer applies (deleting). Also did some small changes to allow files that contain SPACE s in their paths.
Maybe it works like this
set CopyFile=oldfile.txt
set InputFile=newfile.txt
set str_search="Pword"
for /f "delims=" %%i in ('findstr %str_search% %copyfile%') do set str_replace=%%i
set str_replace="%str_replace%"
echo %str_search%
echo %str_replace%
pause
CALL :far %InputFile% %str_search% %str_replace%
EXIT /B 0
:far
setlocal enableextensions disabledelayedexpansion
set "search=%2"
set "replace=%3"
::remove quotes
set search=%search:"=%
set replace=%replace:"=%
echo %search%
echo %replace%
set "textFile=%1"
for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
>>"%textFile%" echo(!line!
endlocal
)
EXIT /B 0
At for /f "delims=" %%i in ('findstr %str_search% %copyfile%') do set str_replace=%%i you write the line with the variable that has the needed info to str_replace.
After that you the program calls an embeded find-and-replace-function (:far) whitch i shemelessly stole from Batch script to find and replace a string in text file without creating an extra output file for storing the modified file
This function finds the string "Pword" and replaces it by the line find in the old file.
Attention:
This doesn't solve your problem completely since your new file has to be s.th like this.
#Password
Pword
so if you loose the = it works otherwise it doesn't. I hope this helps you with your problem.
It's not perfect but this may be okay for you:
#Echo Off
Setlocal EnableExtensions DisableDelayedExpansion
(Set InputFile=F:\EXCHANGE\3\Machine\File.txt)
(Set OutputFile=F:\EXCHANGE\3\File-New.txt)
(Set CopyFile=F:\EXCHANGE\3\OldMachine\OldFile.txt)
For /F "Delims=" %%I In (
'FindStr/B "Pword= Account= Town= LocationChanged= Postcode=" "%CopyFile%"'
) Do Set %%I
(For /F "Tokens=1-2* Delims=]=" %%I In ('Find /V /N ""^<"%InputFile%"') Do (
Echo(%%J|FindStr/B # || (If Defined %%J (Call Echo=%%J=%%%%J%%) Else (
If "%%J" NEq "" (Echo=%%J=%%K) Else (Echo=)))))>%OutputFile%
Timeout -1
EndLocal
Exit/B
I've left the delete and rename for you to add at the end.
This solution should be much faster than the other solutions.
It will also preserve empty lines and lines containing ! and ^.
It only needs one findstr call for collecting the old values for all words.
A second findstr determines all lines (by line number) in the infile which needs an update.
#echo off
setlocal EnableDelayedExpansion
set "_INFILE=File.txt"
set "_OUTFILE=NewFile.txt"
set "_OLDFILE="OldFile.txt"
set "_WORDS=Pword= Account= Town= Postcode= LocationChanged="
REM *** get all values for the key words
for /F "tokens=1,* delims==" %%L in ('findstr "!_WORDS!" "!_OLDFILE!"') do (
for /F %%S in ("%%L") do (
set "word[%%S]=%%M"
)
)
REM *** Find all lines which needs an update
set wordIdx=0
for /F "tokens=1,2,* delims=:= " %%1 in ('findstr /n "!_WORDS!" "!_INFILE!"') do (
set "lines[!wordIdx!].line=%%1"
set "lines[!wordIdx!].word=%%2"
set "replace=!word[%%2]!"
set "lines[!wordIdx!].replace=!replace!"
set /a wordIdx+=1
)
REM *** copy the infile to the outfile
REM *** Replace only the lines which are marked by line numbers
echo Parsing old file contents...
set nextWordIdx=0
set /a searchLine=lines[!nextWordIdx!].line
set lineNo=0
setlocal DisableDelayedExpansion
(
for /f "tokens=*" %%L in ('findstr /n "^" "%_INFILE%"') do (
set "line=%%L"
set /a lineNo+=1
setlocal EnableDelayedExpansion
set "line=!line:*:=!"
if !lineNo! equ !searchLine! (
(echo(!line!!lines[0].replace!)
set /a nextWordIdx+=1
for /F %%R in ("!nextWordIdx!") do (
endlocal
set /a nextWordIdx=%%R
set /a searchLine=lines[%%R].line
)
) ELSE (
(echo(!line!)
endlocal
)
)
) > "!_OUTFILE!"
I'm trying to set the contents of a file to my variable 'reportPath'. The variable 'latestReport' holds the path to the file. When I run this command
for /F %%z in (%latestReport%) do set "reportPath=%%z"
I get the error mentioned in the title of this question ("The system cannot find file"). However, if I replace %latestReport% with the path to the file, the code executes perfectly.
How do I get the code to read my %latestReport% variable as a filename?
Here is the whole script:
#echo off
setLocal Enabledelayedexpansion
set "subject="
set "reportPath="
if "%1"=="" (
for /F "delims== tokens=2" %%a in ('findstr /B test_report_directory my_file_here.properties') do set testReportDir=%%a
set "subject=RentalMan Test Execution Results"
goto :runemaildriver
)
set testReportDir=%~f1
if not exist %testReportDir% (
:: finds string test_report_directory in config file
for /F "delims== tokens=2" %%a in ('findstr /B test_report_directory my_file_here.properties') do set "testReportDir=%%a"
) else (
shift
)
if "%1"=="" (
set "subject=RentalMan Test Execution Results"
) else (
for %%a in (%*) do (
if not exist %%a (
set "subject=!subject!%%a "
)
)
set "subject=!subject:~0,-1!"
)
:runemaildriver
echo testReportDir: %testReportDir%
set latestReport=%testReportDir%\currrentReportPath.txt
echo latestReport: %latestReport%
for /F %%z in (%latestReport%) do set "reportPath=%%z"
echo reportPath: %reportPath%
for /F "delims=: tokens=2" %%d in ('findstr \B Environment %reportPath%') do set "env=%%d"
echo env: %env%
echo Emailing latest TestReport in %testReportDir% with subject line "%subject%"...
How can i search a string and replace it with a variable.
i would like to search version="1.37.0" but the version number could be anything. And there are two "version=" string in package.xml but i would like to replace second one.
how can i search version="x.x.x" and replace it with version="$variable"?
is there any one liner?
i did try to use something like this to search:
findstr "version="[0-9].[0-9].[0-9]" package.xml
and also same thing for desrciption="$variable1"
package.xml
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest>
<Package name="audio"
description="something. . .."
version="1.37.0"
comment="">
</Package>
</PackageManifest>
ad hoc solution, but ...
Edited to adapt to comments
#echo off
setlocal enableextensions disabledelayedexpansion
rem Check input parameters. Needed the input file, the version and the description
if "%~3"=="" goto :eof
set "file=%~1"
set "newVersion=%~2"
set "newDescription=%~3"
rem Read the file into memory
for /f "tokens=1,* delims=:" %%a in ('findstr /n "^" "%file%"') do (
set /a "line=1000+%%a*10"
setlocal enabledelayedexpansion
for %%c in (!line!) do endlocal & set "l_%%c=|%%b"
)
rem %%a will search the required lines
rem %%c remove blanks at the start of the line
rem %%d get the key name
for /f "tokens=2,* delims=_=|" %%a in (
'set l_1 ^| findstr /i /r /c:"^[^<]*version=" /c:"description=" /c:"^[^<]*<Package"'
) do for /f %%c in ("%%b") do for /f "delims==" %%d in ("%%c") do (
if /i "%%d"=="description" ( set "value=%newDescription%" & set "newDescription="
) else if /i "%%d"=="version" ( set "value=%newVersion%" & set "newVersion="
) else if /i "%%d"=="<Package" ( set "packageLine=%%a" & set "value="
) else set "value="
if defined value ( setlocal enabledelayedexpansion
for /f "delims=" %%z in ("!value!") do ( endlocal
for /f tokens^=1^,2^,^*^ delims^=^" %%e in ("%%b") do set "l_%%a=|%%e"%%z"%%g"
)
)
)
rem Include the missing values
set /a "packageLine+=1"
if defined newDescription set "l_%packageLine%=| description="%newDescription%""
set /a "packageLine+=1"
if defined newVersion set "l_%packageLine%=| version="%newVersion%""
rem Output the changed information to console
for /f "tokens=1,* delims=|" %%a in ('set l_1') do echo(%%b
rem Save to file
>"%file%" (for /f "tokens=1,* delims=|" %%a in ('set l_1') do echo(%%b)
Try this :
#echo off
set $FindStr=Version="x.x.x"
set $ReplString=Version="y.y.y"
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('type test.xml') do (
set $Ver=%%a
set $Ver=!$Ver: =!
If /i !$Ver!==%$FindStr% set $Ver=%$ReplString%
echo !$Ver! ) >> Output.xml
Very simplist but a good base
Edit :
This will ask for the version value of the second matched version=
#echo off
set "$FindStr=Version="
set $c=1
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('type test.xml') do (
set $Ver=%%a
set $Ver=!$Ver: =!
If /i "!$Ver:~0,8!"=="%$FindStr%" (
if !$c! GTR 1 (
set /p "$NewVer=Enter New version : "
set $Ver=%$FindStr%!$NewVer!)
set /a $c+=1)
echo !$Ver! >> Output.xml)
The input file is text.xml and the ouputFile Output.xml
I'm trying to make a loop that goes through a file with filenames on each line, set the first filename as a variable and execute the rest if the script. Then take the second line and do the same.
etc. etc.
The problem is that it only does the first line of filenames.txt
#echo off
for /F "tokens=*" %%G in (filenames.txt) do (
set filename=%%G
script
script
script
)
pause
It has be a batch file.
The whole script:
#ECHO OFF
for /F "tokens=*" %%G in (filenames.txt) do (
SET FileName=%%G
SET Word1="ts_confirmImplicitSAMM.gram"
SET Word2="SWIrcnd"
for /f "tokens=3" %%f in ('find /c /i %Word1% %FileName%') do set PairsToShow=%%f
SET /a Lines1=0, Lines2=0
FOR /f "delims=" %%a IN ('findstr "%Word1%" "%FileName%"') DO (
SET "str=%%a"
SET /a Lines1+=1
SETLOCAL enabledelayedexpansion
SET "$1!Lines1!=!str!"
FOR /f "tokens=1*delims==" %%b IN ('set "$1"') DO (IF "!"=="" endlocal)&SET "%%b=%%c"
)
FOR /f "delims=" %%a IN ('findstr "%Word2%" "%FileName%"') DO (
SET "str=%%a"
SET /a Lines2+=1
SETLOCAL enabledelayedexpansion
SET "$2!Lines2!=!str!"
FOR /f "tokens=1*delims==" %%b IN ('set "$2"') DO (IF "!"=="" endlocal)&SET "%%b=%%c"
)
SET /a Lines=Lines1+Lines2
ECHO(%Lines% lines read from %FileName%.
IF %Lines1% leq %Lines2% (SET /a MaxPairs=Lines1) ELSE SET /a MaxPairs=Lines2
IF %PairsToShow% gtr %MaxPairs% (
ECHO only text for %MaxPairs% pairs NOT %PairsToShow% :/
GOTO :END
)
(FOR /l %%a IN (1,1,%PairsToShow%) DO (
SETLOCAL ENABLEDELAYEDEXPANSION
CALL SET "Line1=%%$1%%a%%"
CALL SET "Line2=%%$2%%a%%"
<NUL SET /p "=!Line1!"
ECHO !Line2!
ENDLOCAL
))>> result1.txt
ENDLOCAL
TYPE result1.txt| FINDSTR /V EVNT=SWIgrld >> result.txt
DEL result1.txt
PAUSE
)
Without seeing the rest of your script... you probably need to do 1 of 2 things:
Use SETLOCAL ENABLEDELAYEDEXPANSION (as 2nd line of your script) and then reference the variable filename as !filename! instead of %filename% to use the run-time value instead of the load-time value. But that could cause other problems, depending on what goes on in "script".
Just use %%G instead of filename
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.
#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.
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%"
)