I'm trying to just add some few lines to a text file 2 lines before the end of file, and I'm already having serious issues... This is what I have: Code: rem @echo off REM Routine to insert desired lines in text file; however do nothing if they're already there find "mystring" text.txt > nul || ( set origfile=text.txt set tempfile=text2.txt for /f %%c in ('type %origfile% ^| find /c /v ""') do (set /a totalines = %%c) set /a insert=%totalines%-2 REM I don't want to merely copy each line to the new file, but also to keep space formats and empty lines set line=0 setlocal DisableDelayedExpansion for /f "delims=" %%L in ('findstr /n "^" %origfile%') do ( set "linestr=%%L" set /a line+=1 setlocal EnableDelayedExpansion set linestr=!linestr:*:=! if !line! == !insert! ( echo(!linestr!>> !tempfile! echo.>> !tempfile! echo Here inserting the new strings>> !tempfile! echo the mystring>> !tempfile! ) else ( echo(!linestr!>> !tempfile! ) endlocal ) endlocal ) echo Finished pause But it doesn't work. I uncommented the initial echo off on purpose as a way of "debugging", and found that just before the first iteration (or after?) of the findstr loop I get "Command syntax is incorrect" And the script gets hanged at that point and I have to Ctrl+C. Only works by removing the find "mystring" line and its corresponding closing parentheses, but then it doesn't check whether "mystring" was already present... Any help by chance, please? Thanks.