# Want to add few lines under specific section of Win.ini thorugh batch file



## saxenaman (Feb 10, 2011)

HI,

Want to add few lines under specific section like mentioned below:

[test]
Line=1
Line=2

And if this section already exists in win.ini file, then want to simply add lines just under this section name, else need to create / add this section in the win.ini file. And that should be done through batch file.

Does anyone know how to add the above thing through batch file. Please help!!

Thanks in advance,


----------



## TheOutcaste (Aug 8, 2007)

What OS?
Do you need to check if the lines have already been added?
Do any other sections also contain the lines you are adding?

Also, does the file contain any of these special characters *!%^&<>;*


----------



## saxenaman (Feb 10, 2011)

OS is Windows XP but can be compatible with Vista OS also. 

Yes, I want to check two thing first whether this specific section is there or not. If this section is not there then need to add both section and lines one by one. But if section is already there then want to set specific value say 2 ( it doesn't matters whats the earlier value but new value should be 2 ) for that particular line. and if that line is not there then ONLY need to create line not section.

No, any other section will not contain these lines. 

There line doesn't containing any special characters. Its just Line=2 .

Thanks in Advance,


----------



## TheOutcaste (Aug 8, 2007)

Real question is do any of the _other_ lines contain any special characters? If so, they may be removed. This code will remove every ! that is in the file. If there are two on the same line, everything in between them will also be removed:


```
@Echo Off
SetLocal EnableDelayedExpansion
Set _FileName=C:\Windows\Win.ini
Set _FindSec=[test]
Set _Item1=Line1
Set _Item2=Line2
Set _Value1=1
Set _Value2=2
Set _Num=2
:::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Do not Edit below this line
:::::::::::::::::::::::::::::::::::::::::::::::::::::
Set _Tfile1=%temp%\temp}1{
Set _First=1
If Exist "%_Tfile1%" Del "%_Tfile1%"
>Nul Findstr /I /C:"%_FindSec%" "%_FileName%"
If %ErrorLevel%==0 Goto _Rep
(Echo.
Echo.%_FindSec%
For /L %%L In (1,1,%_Num%) Do Echo.!_Item%%L!=!_Value%%L!)>>"%_FileName%"
Goto :EOF
:_Rep
For /F "Tokens=1* eol= Delims==" %%I In ('Type "%_FileName%"') Do (
Set _Temp=%%I
If "!_Temp:~0,1!"=="[" (
  If !_First!==0 (Echo.>>"%_Tfile1%") Else Set _First=0
  >>"%_Tfile1%" Echo.%%I
) Else (
If "%%I"=="%_Item1%" (
  >>"%_Tfile1%" Echo.%_Item1%=%_Value1%
) Else (
If "%%I"=="%_Item2%" (
  >>"%_Tfile1%" Echo.%_Item2%=%_Value2%
) Else (
>>"%_Tfile1%" Echo.%%I=%%J))))
>Nul Copy /Y "%_Tfile1%" "%_FileName%"
If Exist "%_Tfile1%" Del "%_Tfile1%"
```


----------

