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

Author Topic: Use batch file to insert text in a text file after a word i specify?  (Read 36996 times)

0 Members and 1 Guest are viewing this topic.

rytech

    Topic Starter


    Rookie

    To put it in plain terms guys n gals what i would like to know is if this is possible?

    I have say 25 text files that have different information in them but at the bottom there is 2 lines the same in them all and i need to insert a further 2 lines because my database does not add them in while exporting them.

    Would it be possible to create a batch file that would allow me to run it and upon running it edit these text files and insert two lines after a specific word which would save me a great deal of time doing it manually.

    Any help will be much appreciated and thank you in advance.

    Ryan

    devcom



      Apprentice

      Thanked: 37
      Re: Use batch file to insert text in a text file after a word i specify?
      « Reply #1 on: April 21, 2009, 02:37:27 PM »
      post example of this txt file and show us where you want to put them  ;)
      Download: Choice.exe

      rytech

        Topic Starter


        Rookie

        Re: Use batch file to insert text in a text file after a word i specify?
        « Reply #2 on: April 21, 2009, 02:43:49 PM »
        well there is 25 different files all with information like the following:

        20091105,20091105,1501-1502
        20091112,20091112,1501-1502
        20091119,20091119,1501-1502
        20091126,20091126,1501-1502
        20091203,20091203,1501-1502
        20091210,20091210,1501-1502
        20091217,20091217,1501-1502
        20091224,20091224,1501-1502
        ]
        SCHEDULES BAYS [
        ]
        SCHEDULES CLEANUP
        SYS CONFIG 20090427

        I want to insert :

        SYS DUEGATE 60 05:00
        DISPLAY SCHED 00:30 01:30

        after CLEANUP but on a new line so it would look like the following:

        SCHEDULES CLEANUP
        SYS DUEGATE 60 05:00
        DISPLAY SCHED 00:30 01:30
        SYS CONFIG 20090427

        the text at the start is just a few lines from the document there are hundreds of lines. thanks if you can help ;) you would be a LEGEND lol

        devcom



          Apprentice

          Thanked: 37
          Re: Use batch file to insert text in a text file after a word i specify?
          « Reply #3 on: April 21, 2009, 03:37:12 PM »
          Code: [Select]
          @echo off
          setlocal enabledelayedexpansion
          set num = 0

          for /f "tokens=*" %%a in (file.txt) do (
          if !num! lss 12 echo %%a >>output.txt
          if !num! equ 12 set lastline=%%a
          set /a num+=1
          )

          echo.SYS DUEGATE 60 05:00 >>output.txt
          echo.DISPLAY SCHED 00:30 01:30 >>output.txt
          echo %lastline% >>output.txt

          echo.DONE!
          pause

          this works for me ;) after output.txt is created you can del file.txt and rename output.txt to file.txt

          PS done fast, i need to sleep ;)
          Download: Choice.exe

          gh0std0g74



            Apprentice

            Thanked: 37
            Re: Use batch file to insert text in a text file after a word i specify?
            « Reply #4 on: April 21, 2009, 06:01:36 PM »
            if you have Perl and able to use it.
            Code: [Select]
            # perl -i.bak-ne 'print /CLEANUP$/?$_."SYS DUEGATE 60 05:00\nDISPLAY SCHED 00:30 01:30\n":$_' file*
            20091105,20091105,1501-1502
            20091112,20091112,1501-1502
            20091119,20091119,1501-1502
            20091126,20091126,1501-1502
            20091203,20091203,1501-1502
            20091210,20091210,1501-1502
            20091217,20091217,1501-1502
            20091224,20091224,1501-1502
            ]
            SCHEDULES BAYS [
            ]
            SCHEDULES CLEANUP
            SYS DUEGATE 60 05:00
            DISPLAY SCHED 00:30 01:30
            SYS CONFIG 20090427

            Batcher



              Rookie

              Thanked: 5
              Re: Use batch file to insert text in a text file after a word i specify?
              « Reply #5 on: April 22, 2009, 12:01:10 AM »
              1. Create a folder C:\test1
              2. Create a folder C:\test2
              3. Move your 25 different files to C:\test1
              4. Run below batch file
              5. You'll get the result in C:\test2

              Code: [Select]
              @echo off
              set SrcFolder=C:\test1
              set DstFolder=C:\test2
              for %%a in ("%SrcFolder%\*.txt") do (
                (for /f "tokens=1* delims=:" %%h in ('findstr /n .* "%%a"') do (
                  echo.%%i
                  if %%h equ 12 (
                    echo SYS DUEGATE 60 05:00
                    echo DISPLAY SCHED 00:30 01:30
                  )
                ))>"%DstFolder%\%%~nxa"
              )

              rytech

                Topic Starter


                Rookie

                Re: Use batch file to insert text in a text file after a word i specify?
                « Reply #6 on: April 22, 2009, 06:16:19 AM »
                guys wow replies were excellent and very fast thanks very much, this forum rocks.

                i'll try them all and see which one works best for me but i cant express how greatful i am to you all this is gonna save me a shed load of time and i can focus that elsewhere.

                thank yous so much i'm really greatful ;)

                rytech

                  Topic Starter


                  Rookie

                  Re: Use batch file to insert text in a text file after a word i specify?
                  « Reply #7 on: April 22, 2009, 06:22:01 AM »
                  guys wow replies were excellent and very fast thanks very much, this forum rocks.

                  i'll try them all and see which one works best for me but i cant express how greatful i am to you all this is gonna save me a shed load of time and i can focus that elsewhere.

                  thank yous so much i'm really greatful ;)

                  quick question to batcher, i noticed equ 12 does that insert the text i want on line 12 cause there is like hundreds of different lines and it is near the end and it is not on the same line anytime, i like the look of yours best as it seems the simplest so could you edit it to insert the text after the word i originally specified?

                  is that possible?

                  gh0std0g74



                    Apprentice

                    Thanked: 37
                    Re: Use batch file to insert text in a text file after a word i specify?
                    « Reply #8 on: April 22, 2009, 06:24:57 AM »
                    if line 12 always have the word CLEANUP, then the batch solution should be ok. Otherwise, need to change it to something more flexible.

                    devcom



                      Apprentice

                      Thanked: 37
                      Re: Use batch file to insert text in a text file after a word i specify?
                      « Reply #9 on: April 22, 2009, 07:23:48 AM »
                      does my solution work ?  ;)
                      Download: Choice.exe

                      Batcher



                        Rookie

                        Thanked: 5
                        Re: Use batch file to insert text in a text file after a word i specify?
                        « Reply #10 on: April 22, 2009, 10:19:11 AM »
                        quick question to batcher, i noticed equ 12 does that insert the text i want on line 12 cause there is like hundreds of different lines and it is near the end and it is not on the same line anytime, i like the look of yours best as it seems the simplest so could you edit it to insert the text after the word i originally specified?

                        is that possible?

                        Everything is possible. Try this one.

                        Code: [Select]
                        @echo off
                        set SrcFolder=C:\test1
                        set DstFolder=C:\test2
                        for %%a in ("%SrcFolder%\*.txt") do (
                          (for /f "usebackq delims=" %%h in ("%%a") do (
                            echo.%%h
                            if "%%h" equ "SCHEDULES CLEANUP" (
                              echo SYS DUEGATE 60 05:00
                              echo DISPLAY SCHED 00:30 01:30
                            )
                          ))>"%DstFolder%\%%~nxa"
                        )

                        rytech

                          Topic Starter


                          Rookie

                          Re: Use batch file to insert text in a text file after a word i specify?
                          « Reply #11 on: April 23, 2009, 01:55:32 AM »
                          Batcher your the man thanks very much buddy ;)

                          rytech

                            Topic Starter


                            Rookie

                            Re: Use batch file to insert text in a text file after a word i specify?
                            « Reply #12 on: April 24, 2009, 07:03:30 AM »
                            Devcom i never tried it mate cause the line number thing you had in yours but batchers done the job and i edited the batch file to delete the un needed folder once it had done it so it's all sorted but i need to know if it is possible to delete some text in the file and replace it, i need to replace the date with the date i am working with rather than the date set in the files as well it was just i forgot about that so i have:

                            @ECHO OFF
                            set SrcFolder=C:\Test
                            set DstFolder=C:\Test1
                            for %%a in ("%SrcFolder%\*.txt") do (
                              (for /f "usebackq delims=" %%h in ("%%a") do (
                                echo.%%h
                                if "%%h" equ "SYS CONFIG 20090327" (
                                  echo SYS CONFIG 20090427
                                )
                              ))>"%DstFolder%\%%~nxa"
                            )

                            Where SYS CONFIG 20090327 i want that replaced with SYS CONFIG 20090427, is that possible, this question goes to my man batcher :P lol or anyone with the knowledge cause i aint got it :(
                            « Last Edit: April 24, 2009, 11:07:30 AM by rytech »

                            devcom



                              Apprentice

                              Thanked: 37
                              Re: Use batch file to insert text in a text file after a word i specify?
                              « Reply #13 on: April 24, 2009, 07:10:17 AM »
                              try to change this:
                              Code: [Select]
                                  echo.%%h
                                  if "%%h" equ "SYS CONFIG 20090327" (
                                    echo SYS CONFIG 20090427
                                  )
                              to
                              Code: [Select]
                                  if "%%h" equ "SYS CONFIG 20090327" (
                                    echo SYS CONFIG 20090427
                                  ) else (
                                  echo.%%h
                                  )

                              should work ;)
                              Download: Choice.exe

                              rytech

                                Topic Starter


                                Rookie

                                Re: Use batch file to insert text in a text file after a word i specify?
                                « Reply #14 on: April 24, 2009, 07:55:21 AM »
                                devcom sorry to be a bloody pain but it says the system could not find the path specified, here is what i have, can you think of anything that looks wrong?

                                @ECHO OFF
                                set SrcFolder=c:\test
                                set DstFolder=C:\test1
                                for %%a in ("%SrcFolder%\*.txt") do (
                                  (for /f "usebackq delims=" %%h in ("%%a") do (
                                    if "%%h" equ "SYS CONFIG 20090327" (
                                      echo SYS CONFIG 20090427
                                    ) else (
                                    echo.%%h
                                    )
                                  ))>"%DstFolder%\%%~nxa"
                                )

                                ECHO.
                                ECHO Done!
                                ECHO.
                                PAUSE
                                CLS

                                devcom



                                  Apprentice

                                  Thanked: 37
                                  Re: Use batch file to insert text in a text file after a word i specify?
                                  « Reply #15 on: April 24, 2009, 08:19:58 AM »
                                  i dont know...

                                  Code: [Select]
                                  @ECHO OFF
                                  set SrcFolder=c:\test
                                  set DstFolder=C:\test1

                                  for %%a in ("%SrcFolder%\*.txt") do (

                                    for /f "usebackq delims=" %%h in ("%%a") do (
                                      if "%%h" equ "SYS CONFIG 20090327" (echo SYS CONFIG 20090427) else (echo.%%h)
                                    ) >"%DstFolder%\%%~nxa"
                                   
                                  )

                                  ECHO.
                                  ECHO Done!
                                  ECHO.
                                  PAUSE
                                  CLS
                                  Download: Choice.exe

                                  rytech

                                    Topic Starter


                                    Rookie

                                    Re: Use batch file to insert text in a text file after a word i specify?
                                    « Reply #16 on: April 24, 2009, 09:08:36 AM »
                                    still saying the system cannot find the path specified mate any ideas?

                                    devcom



                                      Apprentice

                                      Thanked: 37
                                      Re: Use batch file to insert text in a text file after a word i specify?
                                      « Reply #17 on: April 24, 2009, 12:13:04 PM »
                                      Code: [Select]
                                      for %%a in ("%SrcFolder%\*.txt") do (to
                                      Code: [Select]
                                      for /f "tokens=*" %%a in ('dir /b "%SrcFolder%\*.txt"') do (
                                      and are you sure there are .txt files in c:\test ??
                                      Download: Choice.exe

                                      Aegis



                                        Expert

                                        Thanked: 67
                                        • Yes
                                        • Yes
                                        • Brian's Mess Of A Web Page
                                      • Experience: Experienced
                                      • OS: Windows 10
                                      Re: Use batch file to insert text in a text file after a word i specify?
                                      « Reply #18 on: April 24, 2009, 12:47:26 PM »
                                      Quote
                                      but i cant express how greatful i am to you all


                                      You may express your gratitude by sending money to me.

                                      I am the unofficial project manager.  I am here to do none of the work, but claim all the credit, and reap the rewards!  ;)   ::) 


                                      "For you, a thousand times over." - "The Kite Runner"

                                      ram



                                        Newbie

                                        • Experience: Beginner
                                        • OS: Windows 7
                                        Re: Use batch file to insert text in a text file after a word i specify?
                                        « Reply #19 on: April 24, 2017, 08:44:10 PM »
                                        Hi everyone,
                                        Thanks for this post it helped me a lot in doing my project but i am facing some issue,hope you can suggest me how to get out of this...

                                        As part of a project I need to add text to the middle of many files using batch scripting. I am able to add the text successfully, but after copying the files to a new location I noticed that the HTML tags are missing. I only have this problem in Windows Server 2012/2008; in Windows 7 the HTML tags remain.

                                        My Code snippet:
                                        @echo off

                                        set SrcFolder=C:\Users\emlfilessample
                                        set DstFolder=C:\Users\output

                                        FOR %%f in (%SrcFolder%*.eml) do (
                                         (FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ %%f"`) do (
                                          SETLOCAL EnabledDelayedExpansion
                                          set "var=%%a"
                                           set "var=!var:*:=!"
                                           if "!var:~0,10" == "x-globalre" (
                                           echo X-SUBTYPE=RETURES
                                         )
                                         echo(!var!
                                         ENDLOCAL
                                        )) >> "%DstFolder%\%%~nxf"
                                        )


                                         **Sample input eml:**
                                           Date Mon,20 mar 2017
                                           From:[email protected]
                                           To:[email protected]
                                           Message-ID:<10091223>
                                            Subject:Symphony
                                           x-globalrelay-MsgType: XXXX
                                           x-StreamType:xxxx
                                           x-contentstartdate:XXX
                                         
                                           <html><body>  Message ID:sm9atRNTnMA=Yay1R0QgoH.............. </html>

                                        After executing my script in Server 2012 I am able to successfully inject the required text in the middle, but as I said the HTML tags are missing:

                                         **Sample output eml:**
                                           Date Mon,20 mar 2017
                                           From:[email protected]
                                           To:[email protected]
                                           Message-ID:<10091223>
                                            Subject:Symphony
                                           echo X-SUBTYPE=RETURES
                                           x-globalrelay-MsgType: XXXX
                                           x-StreamType:xxxx
                                           x-contentstartdate:XXX
                                         
                                           < Yay1R0QgoH.............. </html>
                                         as shown above after executing the script the html tags at the begining are missing i am not able to find out why....can any one help me with this