Alright first things first, your current for loop needs some tweaking; There are a few things we can do here. If we just wanted to grab what ListOfItems equals from the seconened line and ignore the fact there may be other objects in this file, then the proper way to grab this data will the the following:
FOR /F "skip=1 tokens=2,*" %%A IN ('type File.ini') DO (set "ListOfItems=%%B")
skip=1 - Will skip the first line
tokens=2,* - Having 2,* will cause %%A to be first two objects, and %%B will be everything after 2.
To be more proper however, the correct way will be to use a find /i statment to look for the ListOfItems object within the .ini file. Lets take the following text file bellow:
[Items]
ListOfItems = Item01, Item02, Item03
[Armor]
ListOfArmor = Iron Helmet, Iron Brestplate, Iron Pants
[Weapons]
ListOfWeapons = Sword, Dagger, Holy Water
If we used that basic for loop on this statement we would get the last line of the text file:
Sword, Dagger, Holy Water
Bellow is using the find statement along with another loop we can combine them to only extract data after ListOfItems from the whole document.
Rem | Get .ini To String
FOR /F "tokens=*" %%A IN ('type File.ini') DO (
Rem | Look For Line With Items "ListOfItems"
FOR /F "tokens=2,*" %%B IN ('echo %%A^| find /i "ListOfItems"') DO (
Rem | Echo Result
echo %%C
)
)
However, for correcting only that one line and adding a new object to the end, this is where it gets tricky! Keep in mind that batch is primitive and loosing support, it's limitation's are well, limited compared to its successor, powershell. In raw batch there is not a true command for editing just one line in the middle of a document. However, this does not make it impossible.
To get around this we will have to take the entire .ini file and use the type command to break down the document line by line and save it as a string. From there we can use syntax-replace to edit the "String" and save it to a new document. From there we just delete and rename.
To further expand on this we will need to check if ListOfItems is actually populated. The basic if exists statement will work great here. Now because your statement has an = in the equation, simple syntax-replace will not work without further complications. From my previous edit, I changed the simple function to a script that we will call too. This script will be called Replace.bat. All you need to do is make a new .bat file and paste it in from bellow. This file will never need modified.
Bellow is the entire project that should solve all your issues:
Replace.Bat:
(This entire script is the equivalent of a single 15 character command in powershell lol!)
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FILE_I=%~1"
set "SEARCH=%~2"
set "REPLAC=%~3"
set "FILE_O=%~4"
set "FLAG=%~5"
if not defined FILE_I exit /B 1
if not defined SEARCH exit /B 1
if not defined FILE_O set "FILE_O=con"
if defined FLAG set "FLAG=#"
for /F "delims=" %%L in ('
findstr /N /R "^" "%FILE_I%" ^& break ^> "%FILE_O%"
') do (
set "STRING=%%L"
setlocal EnableDelayedExpansion
set "STRING=!STRING:*:=!"
call :REPL RETURN STRING SEARCH REPLAC %FLAG%
>> "%FILE_O%" echo(!RETURN!
endlocal
)
endlocal
exit /B
:REPL rtn_string ref_string ref_search ref_replac flag
setlocal EnableDelayedExpansion
set "STR=!%~2!"
set "SCH=!%~3!"
set "RPL=!%~4!"
if not defined SCH endlocal & set "%~1=" & exit /B 1
set "SCH_CHR=!SCH:~,1!"
if not "%~5"=="" set "SCH_CHR="
if "!SCH_CHR!"=="=" set "SCH_CHR=" & rem = terminates search string
if "!SCH_CHR!"==""^" set "SCH_CHR=" & rem " could derange syntax
if "!SCH_CHR!"=="%%" set "SCH_CHR=" & rem % ends variable expansion
if "!SCH_CHR!"=="^!" set "SCH_CHR=" & rem ! ends variable expansion
call :LEN SCH_LEN SCH
call :LEN RPL_LEN RPL
set /A RED_LEN=SCH_LEN-1
set "RES="
:LOOP
call :LEN STR_LEN STR
if not defined STR goto :END
if defined SCH_CHR (
set "WRK=!STR:*%SCH_CHR%=!"
if "!WRK!"=="!STR!" (
set "RES=!RES!!STR!"
set "STR="
) else (
call :LEN WRK_LEN WRK
set /A DFF_LEN=STR_LEN-WRK_LEN-1,INC_LEN=DFF_LEN+1,MOR_LEN=DFF_LEN+SCH_LEN
for /F "tokens=1,2,3 delims=," %%M in ("!DFF_LEN!,!INC_LEN!,!MOR_LEN!") do (
rem set "RES=!RES!!STR:~,%%M!"
if defined WRK set "WRK=!WRK:~,%RED_LEN%!"
if "!STR:~%%M,1!!WRK!"=="!SCH!" (
set "RES=!RES!!STR:~,%%M!!RPL!"
set "STR=!STR:~%%O!"
) else (
set "RES=!RES!!STR:~,%%N!"
set "STR=!STR:~%%N!"
)
)
)
) else (
if "!STR:~,%SCH_LEN%!"=="!SCH!" (
set "RES=!RES!!RPL!"
set "STR=!STR:~%SCH_LEN%!"
) else (
set "RES=!RES!!STR:~,1!"
set "STR=!STR:~1!"
)
)
goto :LOOP
:END
if defined RES (
for /F delims^=^ eol^= %%S in ("!RES!") do (
endlocal
set "%~1=%%S"
)
) else endlocal & set "%~1="
exit /B
:LEN rtn_length ref_string
setlocal EnableDelayedExpansion
set "STR=!%~2!"
if not defined STR (set /A LEN=0) else (set /A LEN=1)
for %%L in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if defined STR (
set "INT=!STR:~%%L!"
if not "!INT!"=="" set /A LEN+=%%L & set "STR=!INT!"
)
)
endlocal & set "%~1=%LEN%"
exit /B
Main.Bat:
@ECHO OFF
@setlocal EnableDelayedExpansion
Rem | Configuration
set "CustomINI=File.ini"
set "ListHeader=[Items]"
set "Object=ListOfItems"
set "ReplaceScript=Replace.bat"
SET "ItemList=List.txt"
Rem | Check If "CustomINI" Exists
if not exist "%CustomINI%" (
echo File "%CustomINI%" Not Found!
pause
goto :EOF
)
Rem | Check If "ItemList" Exists
if not exist "%ItemList%" (
echo File "%ItemList%" Not Found!
pause
goto :EOF
)
goto StartFunction
:StartFunction
Rem | Generate the list of items from textfile
FOR /F "delims=" %%A IN (%ItemList%) DO (
set "ListAddition=!ListAddition!%%A, "
)
set "ListAddition=!ListAddition:~0,-2!"
Rem | Get .ini To String
set HeaderFound=false
FOR /F "tokens=*" %%A IN ('type !CustomINI!') DO (
Rem | First Find The Header "[Items]" & Extract "ListOfItems" Line Data
for /f "tokens=*" %%B in ('echo %%A') do (
set "item=%%B"
if /i "!item!"=="!ListHeader!" (
set HeaderFound=true
) else if not "!item!"=="!item:ListOfItems=!" if "!HeaderFound!"=="true" (
Rem | Turn Items For Line "ListOfItems" To String
for /f "tokens=2,*" %%C in ('echo %%B') do (
Rem | Set String
set "SEARCHTEXT=%%D"
)
set HeaderFound=false
)
)
)
Rem | Check If "ListOfItems" Is Actually Populated
If "%SEARCHTEXT%"=="" (
Rem | Not Populated
set "SEARCHTEXT=!Object! = "
set "REPLACETEXT=!Object! = !ListAddition!"
goto EditString
) ELSE (
Rem | Populated
set "REPLACETEXT=!SEARCHTEXT!, !ListAddition!"
goto EditString
)
:EditString
Rem | Edit Only "ListOfItems" Line
Rem | Usage: call "1" "2" "3" "4" "5"
Rem | call - Calls external script
Rem | "1" - Name of External script
Rem | "2" - File to Edit
Rem | "3" - Text to replace ex: red apple
Rem | "4" - Text to replace to ex: green apple
Rem | "5" - Output file
call "%ReplaceScript%" "%CustomINI%" "%SEARCHTEXT%" "%REPLACETEXT%" "%CustomINI%.TEMP"
Rem | Delete Original File, Restore New
del "%CustomINI%"
rename "%CustomINI%.TEMP" "%CustomINI%"
goto :EOF
PS - Keep note of the following: The above script expects that when ListOfItems = is not populated, it has a space after the =. If this is not how it is in your .ini file then change set "SEARCHTEXT=!OBJECT! = " to set "SEARCHTEXT=!OBJECT! =" from in the for statement.
EDIT: Since recent requests, The following was updated:
Firstly, since I was unsure of the OP's meaning of ListOfItems = being "Blank", I assumed that he/she was referring to it being ListOfItems =. - Not it being actually missing from the ListHeader it's self. In the example bellow.
My Vision - File.ini:
[Items]
ListOfItems =
[Armor]
ListOfArmor = Iron Helmet, Iron Brestplate, Iron Pants
[Weapons]
ListOfWeapons = Sword, Dagger, Holy Water
OP's Vision - File.ini
[Items]
[Armor]
ListOfArmor = Iron Helmet, Iron Brestplate, Iron Pants
[Weapons]
ListOfWeapons = Sword, Dagger, Holy Water
Since then, I have now updated the script to find [Items] (String) then add a new line under it. This was done using a script by Magoo.
Since there is nothing to replace, we simply can just "Add" onto the the .ini thus we call a different function.
:EditMissingString
Rem | Export SearchString
echo !ListHeader!>> %~dp0ListHeader.txt
Rem | Add Text Under %ListHeader%
(
FOR /f "delims=" %%i IN (ListHeader.txt) DO (
SET AddAfter=%%i
FOR /f "delims=" %%n IN ('findstr /n "^" %CustomINI%') DO (
SET line=%%n
SET line=!line:*:=!
ECHO(!line!
IF "!line!"=="!AddAfter!" ECHO(%AddTEXT%
)
)
)>>%CustomINI%.TEMP
Rem | Remove ListHeader.txt
DEL %~dp0ListHeader.txt
Rem | Delete Original File, Restore New
DEL %CustomINI%
REN %CustomINI%.TEMP %CustomINI%
goto :EOF
Being that we are no longer editing ListOfArmor = alone but rather adding onto it, we no longer will need the Replace.bat script. I also fixed the original script to properly reserve empty lines!
New replace function W/H Line preserve.
:EditExistingString
REM | Make sure we only edit the ListOfItems line.
FOR /F "delims=" %%n IN ('findstr /n "^" %CustomINI%') DO (
SET line=%%n
SET Modified=!line:%SearchText%=%ReplaceText%!
SET Modified=!Modified:*:=!
REM | Output the entire edited INI to a temporary file.
>> %CustomINI%.TEMP ECHO(!Modified!
)
Rem | Delete Original File, Restore New
DEL %CustomINI%
REN %CustomINI%.TEMP %CustomINI%
goto :EOF
Result In:
[Items]
ListOfItems = Item1, Item2, Item3
[Armor]
ListOfArmor = Iron Helmet, Iron Brestplate, Iron Pants
[Weapons]
ListOfWeapons = Sword, Dagger, Holy Water
Result Out:
[Items]
ListOfItems = Item1, Item2, Item3, Item4, Item5, Item6
[Armor]
ListOfArmor = Iron Helmet, Iron Brestplate, Iron Pants
[Weapons]
ListOfWeapons = Sword, Dagger, Holy Water
Final Batch Script:
@ECHO OFF
@setlocal EnableDelayedExpansion
Rem | Configuration
set "CustomINI=File.ini"
set "ListHeader=[Items]"
set "Object=ListOfItems"
SET "ItemList=List.txt"
Rem | Check If "CustomINI" Exists
if not exist "%CustomINI%" (
echo File "%CustomINI%" Not Found!
pause
goto :EOF
)
Rem | Check If "ItemList" Exists
if not exist "%ItemList%" (
echo File "%ItemList%" Not Found!
pause
goto :EOF
)
goto StartFunction
:StartFunction
Rem | Generate the list of items from textfile
FOR /F "delims=" %%A IN (%ItemList%) DO (
set "ListAddition=!ListAddition!%%A, "
)
if "%ListAddition%"=="" (
echo ERROR: File "%ItemList%" Is Empty!
pause
goto :EOF
) ELSE (set "ListAddition=!ListAddition:~0,-2!")
Rem | Get .ini To String
set HeaderFound=false
FOR /F "tokens=*" %%A IN ('type !CustomINI!') DO (
Rem | First Find The Header "[Items]" & Extract "ListOfItems" Line Data
for /f "tokens=*" %%B in ('echo %%A') do (
set "item=%%B"
if /i "!item!"=="!ListHeader!" (
set HeaderFound=true
) else if "!HeaderFound!"=="true" (
Rem | Turn Items For Line "ListOfItems" To String
for /f "tokens=2,*" %%C in ('echo %%B') do (
Rem | Set String
set "SearchText=%%D"
)
Rem | Header Was Found, End Loop & goto HeaderContinue
set HeaderFound=false
goto HeaderContinue
)
)
)
Rem | Header Was Not Found
echo ERROR: The Header "%ListHeader%" Was Not Found!
pause
goto :EOF
:HeaderContinue
Rem | Check If "ListOfItems" Is Actually Populated
If "%SearchText%"=="" (
Rem | Not Populated
set "SearchText=!ListHeader!"
set "AddTEXT=!Object! = !ListAddition!"
goto EditMissingString
) ELSE (
Rem | Populated
set "REPLACETEXT=!SearchText!, !ListAddition!"
goto EditExistingString
)
:EditExistingString
REM | Make sure we only edit the ListOfItems line.
FOR /F "delims=" %%n IN ('findstr /n "^" %CustomINI%') DO (
SET line=%%n
SET Modified=!line:%SearchText%=%ReplaceText%!
SET Modified=!Modified:*:=!
REM | Output the entire edited INI to a temporary file.
>> %CustomINI%.TEMP ECHO(!Modified!
)
Rem | Delete Original File, Restore New
DEL %CustomINI%
REN %CustomINI%.TEMP %CustomINI%
goto :EOF
:EditMissingString
Rem | Add Text Under %ListHeader%
(
FOR /f "delims=" %%i IN ('Echo !ListHeader!') DO (
SET AddAfter=%%i
FOR /f "delims=" %%n IN ('findstr /n "^" %CustomINI%') DO (
SET line=%%n
SET line=!line:*:=!
ECHO(!line!
IF "!line!"=="!AddAfter!" ECHO(%AddTEXT%
)
)
)>>%CustomINI%.TEMP
Rem | Delete Original File, Restore New
DEL %CustomINI%
REN %CustomINI%.TEMP %CustomINI%
goto :EOF
PS: I know your find command is in a different location or something just change the command find to %WINDIR%\System32\FIND.exe in the script.
DEBUG/CHANGES:
Scraped.
For help on any of the commands do the following:
call /?
set /?
for /?
if /?
find /?
- So on.