I have a text file that groups different subsystems using [ ] and then contains item flags in each subgroup. Here is a snippet of the file such that you can get an understanding for what it looks like (notice each subgroup can have the same items):
[EV]
Verbosity=0
Alignment=123
[FluidLevelControl]
BufferTypeLastUsed=TWEEN
Enable Dip Tube=no
Alignment=456,efg
[PressureLevelControl]
Enabled=yes
Alignment=789,abc
Calibration Date=1280919634
[BufferFrontValve]
Log=yes
Alignment=987
Note, the above file is in excess of 2000 lines. I imagine the script is going to take a little while to execute. I also know that there is a better framework to do this but in our application we need it to run from a flash drive and be able to be plugged into our instrument which run WinXP without a .NET frameworks etc.
What I would like to do is use a .bat file to search the document for a specific subsystem (ie. [DesiredSubsystem]) and desired item within the subsystem then modify the item data. For example, in the above text I may want to change the Alignment from 789 to 12345 in the PressureLevelControl subgroup.
I understand there is no way to effective replace / update a text file using a bat file. I've created a function to read in a file and write it to a new file, now I'm trying to develop a clean way to identify the line items and what subgroup they are in as to replace the desired text as needed.
Here is what I have commented with my plan: Update: I spent the afternoon writing some code that seems to work as shown below, there is most def better methods.
::SET VARS
set "varDebugFP=\\svchafile\Teams\Test Engineering\Productivity Tools\MFG BAT Files\SpecificTest\"
set varSource=%varDebugFP%Debug\
set varDestination=%varDebugFP%Debug\
set varFileName=specific.ini
::Do Text File Editing
setlocal enableDelayedExpansion
set "LastGroup=NONE"
::preserve blank lines using FINDSTR, even if a line may start with :
for /f "usebackq delims=*" %%A in (`type "%srcFile%" ^| findstr /n "^"`) do (
set "strLine=%%A"
set "strLine=!strLine:*:=!"
::Check to see if the is defined and greater than 2 characters inidicating a good line
if defined strLine if NOT "!strLine:~2,1!"=="" if "!strLine:~0,1!!strLine:~-1!"=="[]" (set "LastGroup=!strLine!")
::Set the paramaters looking to match
set "DesiredGroup=[TestGroup]"
set "DesiredItem=TestItem"
set "ReplaceLineWith=NewTestItemLine=NewData"
::Look for match on current line
if defined strLine if "!LastGroup!"=="!DesiredGroup!" if NOT "!strLine!"=="!strLine:TestItem=Mod!" (set "strLine=!ReplaceLineWith!")
::Note, in the above line I would like 'TestItem' to be the 'DesiredItem' variable but I can't get it working due to the DelayedExpansion
::Set the additonal paramaters looking to match
::Note, there are multiple items I want to change at once without having to reitterate through the org long (2000+lines) file
set "DesiredGroup=[TestGroup2]"
set "DesiredItem=TestItem2"
set "ReplaceLineWith=NewTestItemLine2=NewData2"
if defined strLine if "!LastGroup!"=="!DesiredGroup!" if NOT "!strLine!"=="!strLine:TestItem=Mod!" (set "strLine=!ReplaceLineWith!")
::I plan to copy and paste the above section as many times as needed to capture all the lines I need to edit (at this point about ~10)
::I don't really understand why the "(" in the below line, I found it in an example on stackoverflow and it seems to work.
echo(!strLine!>>"%newFile%"
)
endlocal
::Replace org file with new file, delete org file (this part I have figured out)
Is there a better way of doing this? Can anyone help complete the code as I'm having a lot of trouble parsing this correctly.
Update: Thanks for the two methods proposed in the answers below. They are very long and I learned a lot from them. Not entirely sure how to implement the functions however and my biggest concern is that using the function will make repetitive reads from the file slowing it down dramatically. I'm very new to this bat file thing but I know its very powerful if you know the commands and are creative.
Thanks in advance for any and all help. -Dan