Batch file for editing a line in an ini file

I have an ini file that is auto-generated.

The second line is always: Version = W.XX.Y.ZZ

Where W is the major version number, XX is the minor version, Y is the string, and ZZ is the version.

I need to open this ini file and edit this line using a batch file to remove the build and revision numbers in this version. Therefore, the line should end like this: Version = W.XX

The main number will always be one character, and the lowest number will always be two, so the whole line is 14 characters long (including spaces).

I was hoping I could get a line that is LEFT 14 characters from this line and replace this line with this line.

+4
source share
1 answer

The syntax "LEFT" you are requesting is to use the variable substring extension: %var:~,14%

The following code will execute "LEFT 14" on each line containing the line "Version"

 setlocal enabledelayedexpansion del output.ini for /f "tokens=*" %%a in (input.ini) do ( set var=%%a if not {!var!}=={!var:Version=!} set var=!var:~,14! echo.!var! >> output.ini ) endlocal 

If there are other lines with the word "Version", you can also change the loop to use the counter.

 setlocal enabledelayedexpansion del output.ini set counter=0 for /f "tokens=*" %%a in (input.ini) do ( set var=%%a set /a counter=!counter!+1 if !counter! EQU 2 set var=!var:~,14! echo.!var! >> output.ini ) endlocal 

Please note that in both cases, you may need more work if your file contains special characters, such as |, <, or>

+6
source

All Articles