Error in script for modifying text file

Discussion in 'Scripting' started by sacarias, Jul 18, 2019.

  1. sacarias

    sacarias MDL Junior Member

    Nov 21, 2018
    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:
    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!
    echo Finished
    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?
  2. BAU

    BAU MDL Addicted

    Feb 10, 2009
    Not sure you need this anymore, but maybe others are curious too, so here's a 3-lines pure batch :trim_file snippet that can skip x lines from top and/or y lines from end of a file.
    Then is just a matter of calling it with TOP=0 and END=2, echo the string, add 2 to the ] variable and use more to print the remaining lines (wrapped as :insert_at_end)

    @echo off
    set origfile=text.txt
    set tempfile=text2.txt
    set thestring=[AveYo]
    find "%thestring%" %origfile% > nul || call :insert_at_end %origfile% 2 %thestring%
    pause &cls
    find "%thestring%" %origfile% > nul || call :insert_at_end %origfile% 2 %thestring% > %tempfile% &type %tempfile%
    :insert_at_end 1=FILE 2=POS_FROM_END 3=STRING
    rem print all lines except last X
    call :trim_file "%~1" 0 %~2
    rem insert your string
    rem ] is the insertion point - 2, so so add 2 (1 for itself, 1 for crlf eof) to get the 2nd line from end
    set/a ]+=2
    rem print lines starting at insertion point until the end
    more /e +%]% "%~1"
    :trim_file usage: call :trim_file  "FILE" TOP END       = Prints FILE skipping TOP lines and END lines - A batch snippet by AveYo
    set/a [=%~2-1 &(for /f "delims=:" %%s in ('findstr/n "^" "%~1"') do set/a ]=%%s-%~3-2) &setlocal enabledelayedexpansion
    <"%~1" ((for /l %%i in (0 1 %[%) do set /p =)&for /l %%i in (%[% 1 %]%) do (set txt=&set /p txt=&echo(!txt!)) &endlocal &exit/b