set /a a0=0,b=0,c=1,e=1 for /f "delims=: tokens=1*" %%i in ('findstr /n .* in.txt') do ( set "a%%i=%%j" if defined a%%i set /a b=%%i set /a c=%%i+1 ) setlocal enabledelayedexpansion if not "!a%b%!"=="!a%b%:hoge=!" set /a c=b set "a%c%=hoge" del out.txt :y echo.!a%e%!>>out.txt set /a e+=1 & if !e! leq !c! goto y