Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch File Find and Insert Line In A Text File  (Read 12174 times)

0 Members and 1 Guest are viewing this topic.

IanC

    Topic Starter


    Starter

    • Experience: Beginner
    • OS: Unknown
    Batch File Find and Insert Line In A Text File
    « on: September 28, 2017, 03:32:01 AM »
    Hi,

    I'm looking for some help. I want to create a batch file that can find a particular line inside a text file and then insert a line below it and enter some text from the batch file.

    Example
    I want to insert a line after "2"

    Text file
    1
    2
    3

    The batch would find "2" create a new line and enter the word "Two". The number one would be within the text file, this could be any word.

    The text file would be store on the C:\

    DaveLembke



      Sage
    • Thanked: 662
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Batch File Find and Insert Line In A Text File
    « Reply #1 on: September 28, 2017, 11:17:08 AM »
    Is this a homework project?

    IanC

      Topic Starter


      Starter

      • Experience: Beginner
      • OS: Unknown
      Re: Batch File Find and Insert Line In A Text File
      « Reply #2 on: September 28, 2017, 03:14:24 PM »
      Is this a homework project?

      You could say that! Trying to find a quick win to include in my current batch file.

      DaveLembke



        Sage
      • Thanked: 662
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Batch File Find and Insert Line In A Text File
      « Reply #3 on: September 28, 2017, 03:35:56 PM »

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Batch File Find and Insert Line In A Text File
      « Reply #4 on: September 28, 2017, 04:09:43 PM »
      IanC, may I ask a question.

      Is there a school that wants students to write scripts in DOS command lines?
      Is so, why would any school use DOS commands as a learning project?

      Here is why I ask.
      Years ago Microsoft pulled away from doing more stuff with the command line and introduced variations of the  BASIC language as a script tool.  There was Visual basic for DOS, Visual Basic for applications and  Quick Basic. An later they gave the world VBScript, which is easy to use from the command line.

      https://www.computerhope.com/jargon/v/vbscript.htm
      Quote
      VBScript is a scripting language developed by Microsoft and released to the public in 1996 that gives Microsoft Internet Explorer enhanced capabilities beyond those available through HTML. Below is a basic example of a JavaScript that prints "Hello World!".

      Why don't schools teach these improved Microsoft methods to solve simple takes? Why focus on DOS based tools? The DOS FOR command is the worst thing Microsoft ever made!
      https://www.computerhope.com/jargon/f/for.htm

      Of course, sometimes a command line script is best, but it depends on what you want to do. 

      Just asking.    ;D

      IanC

        Topic Starter


        Starter

        • Experience: Beginner
        • OS: Unknown
        Re: Batch File Find and Insert Line In A Text File
        « Reply #5 on: September 29, 2017, 04:41:09 AM »

        I'm still struggling with this. I tried what was on that link but it doesn't seem to work and this is difficult to debug.

        Any help?

        Code: [Select]
        @echo off > newfile & setLocal enableDELAYedeXpansion
        FOR /R C:\in %%a IN (*.txt) DO (
            for /f "tokens=1* delims= " %%i in ("%%a") do (
                >> newfile echo.%%i %%j
                if "%%i" equ "2" >> newfile echo Two
            )
            move /y newfile "%%a"
        )