insert line in batch file

March 2, 2010 at 06:54:26
Specs: Windows XP
hi, i'm writing a batch file to insert a line into a .txt file and i want to do it multiple times so i made a loop construction.
it doesnt work and i dont know how to fix it.
::== inbefore.bat
@echo off
setLocal EnableDelayedExpansion
set name=myfile.txt
set numbr=2
set loopnr=1
set finditem1=regel
set finditem=test1

set showitem= test 1234
set showitem1=4321 test

for /f "tokens=1 delims=][" %%a in ('find /n "%finditem%" ^< %name%') do (
set /a i=%%a
set /a i-=1
for /f "tokens=* delims= " %%a in (%name%) do (
echo %%a >> newfile.txt
set /a n+=1
if !i! equ !n! echo %showitem% >> newfile.txt
Del %name%
rename newfile.txt %name%
goto loop1

set /a numbr=numbr+1
set finditem=%finditem1%
set showitem=%showitem1%
set /a loopnr=loopnr+1
if %numbr% leq 20 goto punt

i hope you guys can help me.

See More: insert line in batch file

Report •

March 2, 2010 at 19:01:23
it looks like it should work, except you didn't reset "n" to zero. i think you should set n to zero here:
set /a i-=1
:: here, reset var. "n" to zero
set /a n=0
for /f "tokens=* delims= " %%a in (%name%) do (

other than that, it should work, looks like to me. maybe if you post what it is or isn't doing, and errormessages if any.
also, if values aren't coming out right, place echos and pauses at "strategic" points to see what's going on. H.t.h.

Report •

March 3, 2010 at 12:49:29
If by means of the above proposed fix, it doesnt work, let us know exactly what happens.

Report •

Related Solutions

Ask Question