Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7914757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:13:19+00:00 2026-06-03T14:13:19+00:00

I am completely new to Batch files and have been tinkering around with simple

  • 0

I am completely new to Batch files and have been tinkering around with simple commands. I have figured out how to move text to another file, find a line etc., but I want to be able to add a few lines of text into an already existing text file. Here is what I have so far:

@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 Examples.txt contains:

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

and Line_to_add.txt contains:

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

I would like the output to be:

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

tia 🙂

Solution

@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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:13:21+00:00Added an answer on June 3, 2026 at 2:13 pm

    You never bothered to ask a question, but I can see a few 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 space before your paren.

    2) Your comparison will never find a match because the quotes are included in the comparison. Your values have spaces, so both sides of the comparison should be enclosed in quotes.

    3) You are attempting to insert into the existing file, which cannot work. Your code would simply append the text to the end of the file. You must write the entire desired result to a new temp file, and then move the temp file to the original name.

    First I will ignore the update of the file and show how to get the correct output to the screen.

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

    It is then simple to direct the output to a file and move the file to the desired name.

    @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
    

    Good resources for learning batch syntax and techniques:

    1) Every command has online help. Simply add the /? option after the command name. For example, DIR /?

    2) Here are some sites with good info

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Completely new to java and I have been playing around with regex in a
Completely new to using simple XML library in PHP, and have been using the
I am completely new to HTML5 and have been reading about it for the
Completely new to most of this stuff, but basically Im playing around with the
I'm completely new with IFC files. For my company I need to read IFC
I am completely new to Xcode and iOS developing in general. Basically I have
I'm completely new to C++ and am breaking my head over a simple problem.
I'm relatively new to OpenGL and have been using the GLTools library it provides.
Im currently updating and replacing old outdated nightly scheduled batch files with new self
Completely new to asp.net mvc... completely new to web apps so bear with me...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.