Package: insert lines from a .txt file into a .txt file

I am completely new to batch files and do simple commands. I figured out how to move text to another file, find a line, etc., but I want to add a few lines of text to an existing text file. Here is what I still have:

@ECHO OFF
CD C:\Documents and Settings\SLZ1FH\Desktop\New Folder
FOR /F "tokens=*" %%A IN (Examples.txt) DO (
  ECHO %%A
  IF %%A=="Ex3 3"(
    TYPE Line_to_add.txt >> Examples.txt
  )
)

if Example.txt contains:

  • Ex1 1
  • Ex2 2
  • Ex3 3
  • Ex4 4
  • Ex5 5

and Line_to_add.txt contains:

  • This is a string
  • This is another line for kicks!

I would like the result to be:

  • Ex1 1
  • Ex2 2
  • Ex3 3
  • This is a string
  • This is another line for kicks!
  • Ex4 4
  • Ex5 5

tia :)

Decision

@ECHO OFF
FOR /F "tokens=*" %%A IN (Examples.txt) DO (
  ECHO %%A
  IF "%%A" EQU "Ex3" (
    TYPE Line_to_add.txt
  )
) >> temp.txt
move /y temp.txt Examples.txt
+3
source share
3 answers

You never bothered to ask a question, but I see several problems with your code.

1) Your IF statement is broken because the left paren is treated as part of the string to be compared. You must have at least one place in front of your boyfriend.

2) , . , .

3) , . . , temp .

, .

@ECHO OFF
FOR /F "tokens=*" %%A IN (Examples.txt) DO (
  ECHO %%A
  IF "%%A" EQU "Ex3 3" (
    TYPE Line_to_add.txt
  )
)

.

@ECHO OFF
(
  FOR /F "tokens=*" %%A IN (Examples.txt) DO (
    ECHO %%A
    IF "%%A" EQU "Ex3 3" (
      TYPE Line_to_add.txt
    )
  )
) >temp.txt
move /y temp.txt Examples.txt

:

1) . /? . , DIR /?

2)

http://www.dostips.com/
http://judago.webs.com/
http://www.robvanderwoude.com/batchfiles.php

+6

FOR/F , SET/a, .

CALL , . - , , .

script, . . , .

@ECHO OFF
set INFILE=Examples.txt
set OUTFILE=Output.txt
set MIXFILE=Line_to_add.txt

set OUTFILEOLD=%OUTFILE%
if NOT "%INFILE%"=="%OUTFILE%" got no_tmp1
set OUTFILE=%OUTFILE%.tmp
:no_tmp1

set counter=0
FOR /F "tokens=*" %%A IN (%INFILE%) do call :process_line %%A

set OUTFILE=%OUTFILEOLD%
if NOT "%INFILE%"=="%OUTFILE%" goto no_tmp2
copy %OUTFILETMP% %OUTFILE%    
:no_tmp2

goto exit

:process_line
set /a counter=counter+1
echo Line %counter% = %*
REM ##IF "%counter%"=="3" goto :adding_line 
IF /i "%1"=="Ex3" goto :adding_line
echo %* >> %OUTFILE%
goto :EOF

:adding_line
echo Adding line
type %MIXFILE% >> %OUTFILE%
goto :EOF

:exit
echo DONE
0
1) create new temp file
2) output to temp
3) delete original
4) rename temp to original

.

0

All Articles