I'd advise against using batch files for parsing files. It just doesn't play nicely with such things.
If you are absolutely sure that your file can never contain the following characters: &
, |
, >
, <
, "
then you can use a batch file. But catering for those characters is hard and and—in some cases—downright impossible.
In such cases you would be better off either using VBScript to process the file or using various UNIX tools to perform that task. This might be possible using awk
.
Note that Windows 7 includes Windows PowerShell where such a task is really trivial. And it can be installed separately on Windows XP and higher.
However, I think I'd go with a VBScript solution here.
If your requirements match above constraints, you can do it with a batch file. The one below should work.
First of all, we need delayed expansion, so this has to be one of the very first lines in the batch:
setlocal enableextensions enabledelayedexpansion
You can iterate over the lines in a file using for /f
:
for /f "delims=" %%x in (my_file) do call :process "%%x"
goto :eof
:process
...
goto :eof
This will call the subroutine process
for each line of the file, handing over the line as argument. The delims=
part specifies that we don't want tokenizing on that line. We now look at the contents of that routine.
Fist we need to know whether the line even contains the string we're looking for (Note that the loop variable, containing the line is only %%x
inside of the loop, in the subroutine it becomes %1
):
echo %1 | findstr "COUNTER:int" >nul 2>&1
if not errorlevel 1 (
...
) else (
echo %~1>>new_file
)
Inside, where now the ...
are, we can handle that line in case it contains the search string. We first need to dissect it. The easiest way would be to split it up at the =
character and then we increment the number and output everything again.
for /f "tokens=1,2 delims==" %%a in (%1) do (
set /a number=%%b+1
echo %%a= !number!>>new_file
)
So, putting it all together, it looks like this:
@echo off
setlocal enableextensions enabledelayedexpansion
del new_file
for /f "delims=" %%x in (my_file) do call :process "%%x"
goto :eof
:process
echo %1 | findstr "COUNTER:int">nul 2>&1
if not errorlevel 1 (
for /f "tokens=1,2 delims==" %%a in (%1) do (
set /a number=%%b+1
echo %%a= !number!>>new_file
)
) else (
echo %~1>>new_file
)
goto :eof
Code can be found in my SVN.