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

Author Topic: Insert text to a new last line  (Read 4073 times)

0 Members and 1 Guest are viewing this topic.

atturhari

    Topic Starter


    Greenhorn

    • Experience: Familiar
    • OS: Windows 7
    Insert text to a new last line
    « on: July 17, 2014, 06:17:05 AM »
    Hi,

    I am using the following code to insert the filename to the last line.
    FOR %G in (*.txt) do echo.>>file.txt %~nG>>%G

    However sometime it gets appended to the existing last line.
    I want just the filename to be in the last line. How do we insert a CRLF in DOS and add a text?

    Thankyou
    Atturhari


    Lemonilla



      Apprentice

    • "Too sweet"
    • Thanked: 70
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: Insert text to a new last line
    « Reply #1 on: July 17, 2014, 08:10:32 AM »
    Hi,

    I am using the following code to insert the filename to the last line.
    FOR %G in (*.txt) do echo.>>file.txt %~nG>>%G

    However sometime it gets appended to the existing last line.
    I want just the filename to be in the last line. How do we insert a CRLF in DOS and add a text?

    Thankyou
    Atturhari

    I have almost no idea what you mean, but I think you want the text ">>file.txt %~nG" At the end of the file %G.  In that case, you need to escape the first two '>'.  To do this simply prefix them with a carrot '^>'.
    Quote from: patio
    God Bless the DOS Helpers...
    Quote
    If it compiles, send the files.

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: Insert text to a new last line
    « Reply #2 on: July 17, 2014, 08:19:47 AM »
    Your problem seems to be that the last line doesn't always have a trailing CF/LF

    This is a solution to that - test it on some sample .txt files:
    remove - This is the last line as it's just to help see where it echos

    Code: [Select]
    @echo off
    for /f "delims=" %%a in ('dir *.txt /a:-d /b') do (
       findstr /v ".*$" "%%a" >nul && (>>"%%a" echo()
       >>"%%a" echo(%%a - This is the last line
    )

    atturhari

      Topic Starter


      Greenhorn

      • Experience: Familiar
      • OS: Windows 7
      Re: Insert text to a new last line
      « Reply #3 on: July 17, 2014, 10:47:08 PM »
      As pointed out, the problem is the last line doesn't always have a trailing CF/LF causing the text (or filename) to be appended to the same line.

      I tried foxidrive's code, unfortunately it does not work.
      Also my files are on a network, so want to know if it is possible to tweak the below line of code to add a CF/LF

      FOR %G in (*.fex) do echo FILENAME:%~nG>>%G

      Thankyou

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      Re: Insert text to a new last line
      « Reply #4 on: July 18, 2014, 01:27:21 AM »
      I tried foxidrive's code, unfortunately it does not work.

      It works fine here.  How does it fail for you?

      atturhari

        Topic Starter


        Greenhorn

        • Experience: Familiar
        • OS: Windows 7
        Re: Insert text to a new last line
        « Reply #5 on: July 18, 2014, 04:06:50 AM »
        It works fine here.  How does it fail for you?

        Was executing your program in CMD and just realized that i should use a single % instead of %%.

        Your program does the job perfectly. Thank you very very much  :)

        foxidrive



          Specialist
        • Thanked: 268
        • Experience: Experienced
        • OS: Windows 8
        Re: Insert text to a new last line
        « Reply #6 on: July 18, 2014, 04:22:58 AM »
        Was executing your program in CMD and just realized that i should use a single % instead of %%.

        You have to be kidding me... 

        Who taught you to paste batch files into a cmd prompt?

        kiswersiddique



          Newbie

          • Experience: Experienced
          • OS: Windows 7
          Re: Insert text to a new last line
          « Reply #7 on: July 22, 2014, 01:54:00 AM »
          Your problem seems to be that the last line doesn't always have a trailing CF/LF

          This is a solution to that - test it on some sample .txt files:
          remove - This is the last line as it's just to help see where it echos

          Code: [Select]
          @echo off
          for /f "delims=" %%a in ('dir *.txt /a:-d /b') do (
             findstr /v ".*$" "%%a" >nul && (>>"%%a" echo()
             >>"%%a" echo(%%a - This is the last line
          )

          atturhari

            Topic Starter


            Greenhorn

            • Experience: Familiar
            • OS: Windows 7
            Re: Insert text to a new last line
            « Reply #8 on: July 22, 2014, 05:50:33 AM »
            You have to be kidding me... 

            Who taught you to paste batch files into a cmd prompt?

            Hmm.. my honest answer,
            In cmd prompt, the code can be edited quickly and best for trail and error.
            Running as .bat file is time consuming involving multiple steps  ::)

            Squashman



              Specialist
            • Thanked: 134
            • Experience: Experienced
            • OS: Other
            Re: Insert text to a new last line
            « Reply #9 on: July 22, 2014, 08:19:03 AM »
            Hmm.. my honest answer,
            In cmd prompt, the code can be edited quickly and best for trail and error.
            Running as .bat file is time consuming involving multiple steps  ::)
            You should try using Notepad++.  You can edit and run your batch files within Notepad++.

            Lemonilla



              Apprentice

            • "Too sweet"
            • Thanked: 70
            • Computer: Specs
            • Experience: Experienced
            • OS: Windows 7
            Re: Insert text to a new last line
            « Reply #10 on: July 22, 2014, 06:20:43 PM »
            Hmm.. my honest answer,
            In cmd prompt, the code can be edited quickly and best for trail and error.
            Running as .bat file is time consuming involving multiple steps  ::)
            Or you could always just not maximize everything.
            Quote from: patio
            God Bless the DOS Helpers...
            Quote
            If it compiles, send the files.