how do i insert 2 lines to many batch files after skipping 1st line?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
nnnmmm
Posts: 122
Joined: 26 Aug 2017 06:11

how do i insert 2 lines to many batch files after skipping 1st line?

#1 Post by nnnmmm » 08 Jan 2022 13:27

how do i insert 2 lines below to many batch files after skipping 1st line of each batch?

Code: Select all

SET K0="..\GAMEPATH.TXT"
FOR /F "Tokens=*" %%V IN ('TYPE %K0%') DO (SET K1=%%V)
before AA001.bat has
1
2
3
4
5
6

after AA001.bat has
1
SET K0="..\GAMEPATH.TXT"
FOR /F "Tokens=*" %%V IN ('TYPE %K0%') DO (SET K1=%%V)
2
3
4
5
6

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how do i insert 2 lines to many batch files after skipping 1st line?

#2 Post by aGerman » 08 Jan 2022 14:49

(I'll refrain from giving you the same advices repeatedly...)

You can't insert lines without writing the file new.

Code: Select all

@echo off &setlocal

set "file=AA001.bat"

setlocal EnableDelayedExpansion
<"!file!" >"!file!.~tmp" (
  for /f %%i in ('type "!file!"^|find /c /v ""') do (
    set "line="& set /p "line="
    echo(!line!

    echo SET K0="..\GAMEPATH.TXT"
    echo FOR /F "Tokens=*" %%%%V IN ('TYPE %%K0%%'^) DO (SET K1=%%%%V^)

    for /l %%j in (2 1 %%i) do (
      set "line="& set /p "line="
      echo(!line!
    )
  )
)
>nul move /y "!file!.~tmp" "!file!"
endlocal
Steffen

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: how do i insert 2 lines to many batch files after skipping 1st line?

#3 Post by Aacini » 08 Jan 2022 23:39

Code: Select all

@echo off
setlocal EnableDelayedExpansion

for %%f in (*.bat) do (
   < "%%f" (
      set /P "line1="
      echo(!line1!
      echo SET K0="..\GAMEPATH.TXT"
      echo FOR /F "Tokens=*" %%%%V IN ('TYPE %%K0%%'^) DO (SET K1=%%%%V^)
      findstr "^"
   ) > temp.tmp
   move /Y temp.tmp "%%f" > NUL
)
Antonio

nnnmmm
Posts: 122
Joined: 26 Aug 2017 06:11

Re: how do i insert 2 lines to many batch files after skipping 1st line?

#4 Post by nnnmmm » 09 Jan 2022 03:49

zttag - skip insert rest 1.BAT has

Code: Select all

::skip 4 lines, inject 2 lines, do the rest
::if inject more, add echo more.
::works for SKIP=0
::
::echo per 1    - no error for ABC AB!C  "AB C"  "AB !C"  "AB&C"  "AB &C" auto-quotes space &
::echo per~1    - no error for ABC AB!C   AB C    AB !C        NO auto-quotes on space
::echo per~1    -    error for AB&C  AB &C
::echo "=per~1" - no error for ABC  AB!C  AB C  AB !C          NO auto-quotes on space
::echo "=per~1" -    error for AB&C  AB &C
::----------------------------------------------
::but inside setlocal EnableDelayedExpansion
::per 1    - no error for ABC   AB!C
::per 1    -    error for AB C  AB !C AB&C  AB &C
::per~1    - no error for ABC   AB C  AB!C  AB !C
::per~1    -    error for AB&C   AB &C
::
::"=per~1" - no error for ABC AB!C  AB C  AB !C   AB&C  AB &C
:: shows error outside, no error inside
REM ****************************************************************
@echo off &setlocal

set "file=%~1"
set /A SKIP=4

setlocal EnableDelayedExpansion
<"!file!" >"!file!.~tmp" (
   for /f %%i in ('type "!file!"^|find /c /v ""') do (
      set "line1="& set /p "line1="
      echo(!line1!

      set "line2="& set /p "line2="
      echo(!line2!

      set "line3="& set /p "line3="
      echo(!line3!

      set "line4="& set /p "line4="
      echo(!line4!

      REM -------------------------------------------
      echo SET K0="..\GAMEPATH.TXT"
      echo FOR /F "Tokens=*" %%%%V IN ('TYPE %%K0%%'^) DO (SET K1=%%%%V^)
      REM -------------------------------------------

      for /l %%j in (1+SKIP 1 %%i) do (
         set "line="& set /p "line="
         echo(!line!
      )
   )
)
>nul move /y "!file!.~tmp" "!file!"
endlocal
i fed the file "zttag - skip insert rest 1.BAT" to the ztree version of batch file below, aGerman worked so well (including & and !), now i can skip where i want and insert how many lines i want. i will ask about operators like <"!file!" >"!file!.~tmp" some other time until i get to read some for it

Code: Select all

REM #ZTTag #ZTTemp\~ZTTag.bat -fCALL #ZTTemp\ZTW_MENU.BAT SUB %1
IF [%%1]==[SUB] GOTO SUB
   CALL #ZTTemp\~ZTTag.bat
   DEL #ZTTemp\~ZTTag.bat
GOTO :END

:SUB
   "C:\ZTREE\BATCH2\zttag - skip insert rest 1.BAT" %%2
GOTO :END

:END
time to study aacini's batch

Post Reply