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

Author Topic: Extract specific filename from a text file into a new text file  (Read 5910 times)

0 Members and 1 Guest are viewing this topic.

hdragon33

    Topic Starter


    Greenhorn

    • Experience: Beginner
    • OS: Windows 7
    Extract specific filename from a text file into a new text file
    « on: September 28, 2016, 12:29:29 AM »
    Hi all,

    Input text filename : abc.txt
    Output text filename : abc-out.txt

    Inside abc.txt

    connect

    connection established; transferring PDF123456-qwe.sts
    Received PDF123456-qwe.sts

    connection established; transferring PDF223456-qwe.sts
    Received PDF223456-qwe.sts

    connection established; transferring PDF128456-qwe.sts
    Received PDF128456-qwe.sts

    Connection stopped

    Expected Output
    Inside of abc-out.txt

    PDF123456-qwe.sts
    PDF223456-qwe.sts
    PDF128456-qwe.sts

    I tried using

    findstr /i "PDF" abc.txt > abc-out.txt

    but I get the following which not really what I want

    connection established; transferring PDF123456-qwe.sts
    Received PDF123456-qwe.sts
    connection established; transferring PDF223456-qwe.sts
    Received PDF223456-qwe.sts
    connection established; transferring PDF128456-qwe.sts
    Received PDF128456-qwe.sts

    Thanks for the help in advance

    hdragon33

      Topic Starter


      Greenhorn

      • Experience: Beginner
      • OS: Windows 7
      Re: Extract specific filename from a text file into a new text file
      « Reply #1 on: September 28, 2016, 01:58:30 AM »
      Sorry guys... edited output as follows

      Expected Output
      Inside of abc-out.txt

      123456-qwe.sts
      223456-qwe.sts
      128456-qwe.sts

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: Extract specific filename from a text file into a new text file
      « Reply #2 on: September 28, 2016, 06:47:40 AM »
      Code: [Select]
      @echo off
      FOR /F "tokens=1* delims= " %%G IN ('find /I "received pdf" ^<abc.txt') DO (
      set "fn=%%H"
      setlocal enabledelayedexpansion
      set "fn=!fn:~3!"
      echo !fn!>>abc-out.txt
      endlocal
      )

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      Re: Extract specific filename from a text file into a new text file
      « Reply #3 on: September 29, 2016, 02:52:24 AM »
      Code: [Select]
      @echo off
      FOR /F "tokens=1* delims= " %%G IN ('find /I "received pdf" ^<abc.txt') DO (
         set "fn=%%H"
         setlocal enabledelayedexpansion
         set "fn=!fn:~3!"
         echo !fn!>>abc-out.txt
         endlocal
      )


      It's always easy to fiddle with code that's already written.   
      This is a little bit of a hack, given the characteristics of the text.

      Code: [Select]
      @echo off
      (
      FOR /F "tokens=1,*" %%G IN ('find /I "received pdf" ^<abc.txt') DO (
      FOR /F "delims=PDF" %%J IN ("%%H") DO echo %%%J)
      )>abc-out.txt

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: Extract specific filename from a text file into a new text file
      « Reply #4 on: September 29, 2016, 08:38:09 AM »

      It's always easy to fiddle with code that's already written.   
      This is a little bit of a hack, given the characteristics of the text.

      Code: [Select]
      @echo off
      (
      FOR /F "tokens=1,*" %%G IN ('find /I "received pdf" ^<abc.txt') DO (
      FOR /F "delims=PDF" %%J IN ("%%H") DO echo %%%J)
      )>abc-out.txt

      Yes Foxidrive.  I had that idea as well but thought it might not be bullet proof.  So I just stuck with the substring.

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      Re: Extract specific filename from a text file into a new text file
      « Reply #5 on: September 29, 2016, 09:01:07 AM »
      Yes Foxidrive.  I had that idea as well but thought it might not be bullet proof.  So I just stuck with the substring.

      It's pretty likely that the example isn't representative of the actual filenames and so your code is the only one that will work in the real situation Squashman.

      I amused myself by fiddling with your code in that way. :) 

      I had noted at that point that it had been over 24 hours since his last post and the OP hadn't been back to say 'thanks' to you.   Doesn't that grind your gears?

      patio

      • Moderator


      • Genius
      • Maud' Dib
      • Thanked: 1769
        • Yes
      • Experience: Beginner
      • OS: Windows 7
      Re: Extract specific filename from a text file into a new text file
      « Reply #6 on: September 29, 2016, 09:04:51 AM »
      I like when you guys double-team....things get done...then the OP gets abducted by aliens...
      " Anyone who goes to a psychiatrist should have his head examined. "

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      Re: Extract specific filename from a text file into a new text file
      « Reply #7 on: September 29, 2016, 09:22:58 AM »
      ...then the OP gets abducted by aliens...

      That's the answer!  How stoopid of me, and I used to watch the X-Files documentary too!



      Triple Tag team matches are fun here... :D

      hdragon33

        Topic Starter


        Greenhorn

        • Experience: Beginner
        • OS: Windows 7
        Re: Extract specific filename from a text file into a new text file
        « Reply #8 on: October 03, 2016, 02:46:44 AM »
        sorry Guys... I was just swamped by the issue... Just a quick word of thanks and I am trying to fiddle so there is a prompt of reply on my email

        Thanks again... I will run the code and see ...

        hdragon33

          Topic Starter


          Greenhorn

          • Experience: Beginner
          • OS: Windows 7
          Re: Extract specific filename from a text file into a new text file
          « Reply #9 on: October 03, 2016, 02:59:29 AM »
          Hi Guys, I am back from running the codes...

          I tried saving both Squash and Fox version into a-test.bat and ran it (I hope this is correct way)... but have different results

          Squash code did not output abc-out.txt
          Fox code output abc-out.txt but 0kb inside

          will it help if I paste the redacted sample here ? I think we are in different timezone probably a minimum 8hrs different. so delay will be experienced unless... that is what those greenies wants me to think.....

          cheers guys

          Squashman



            Specialist
          • Thanked: 134
          • Experience: Experienced
          • OS: Other
          Re: Extract specific filename from a text file into a new text file
          « Reply #10 on: October 03, 2016, 06:50:53 AM »
          Hi Guys, I am back from running the codes...

          I tried saving both Squash and Fox version into a-test.bat and ran it (I hope this is correct way)... but have different results

          Squash code did not output abc-out.txt
          Fox code output abc-out.txt but 0kb inside

          will it help if I paste the redacted sample here ? I think we are in different timezone probably a minimum 8hrs different. so delay will be experienced unless... that is what those greenies wants me to think.....

          cheers guys
          Well I tested my code with your exact examples before I posted my code.  So if you changed it at all trying to fit it into some existing code then you need to post all your code.
          « Last Edit: October 03, 2016, 07:10:32 AM by Squashman »

          Squashman



            Specialist
          • Thanked: 134
          • Experience: Experienced
          • OS: Other
          Re: Extract specific filename from a text file into a new text file
          « Reply #11 on: October 03, 2016, 07:01:08 AM »
          As they say.  Proof is in the pudding.
          Code: [Select]
          C:\Users\squashman\Desktop>type so.bat
          @echo off
          FOR /F "tokens=1* delims= " %%G IN ('find /I "received pdf" ^<abc.txt') DO (
                  set "fn=%%H"
                  setlocal enabledelayedexpansion
                  set "fn=!fn:~3!"
                  echo !fn!>>abc-out.txt
                  endlocal
          )
          C:\Users\squashman\Desktop>type abc.txt
          connect

          connection established; transferring PDF123456-qwe.sts
          Received PDF123456-qwe.sts

          connection established; transferring PDF223456-qwe.sts
          Received PDF223456-qwe.sts

          connection established; transferring PDF128456-qwe.sts
          Received PDF128456-qwe.sts

          Connection stopped
          C:\Users\squashman\Desktop>so.bat

          C:\Users\squashman\Desktop>type abc-out.txt
          123456-qwe.sts
          223456-qwe.sts
          128456-qwe.sts

          C:\Users\squashman\Desktop>

          hdragon33

            Topic Starter


            Greenhorn

            • Experience: Beginner
            • OS: Windows 7
            Re: Extract specific filename from a text file into a new text file
            « Reply #12 on: October 03, 2016, 06:46:09 PM »
            Hi Squashman,

            I just realised that you are using "received pdf" as the search key... I made some modification. it is working somewhat.

            I am trying to learn coding, if it is not too much can you help explain the code line by line ?

            Thanks a million

            Squashman



              Specialist
            • Thanked: 134
            • Experience: Experienced
            • OS: Other
            Re: Extract specific filename from a text file into a new text file
            « Reply #13 on: October 04, 2016, 10:51:26 AM »
            I just realised that you are using "received pdf" as the search key... I made some modification. it is working somewhat.
            This tells me you are not providing an accurate example of your input text file.  Not going to bother explaining the code if it is not working 100% because if you change the examples of your input and output, the code could change drastically and then I am wasting my time explaining even more code.

            hdragon33

              Topic Starter


              Greenhorn

              • Experience: Beginner
              • OS: Windows 7
              Re: Extract specific filename from a text file into a new text file
              « Reply #14 on: October 04, 2016, 06:28:17 PM »
              Hi Squashman,

              I play around with few fiddles, and manage to get it work... below are some comments of how it works, correct me if I am wrong

              @echo off
              FOR /F "tokens=1* delims= " %%G IN ('find /I "received pdf" ^<abc.txt') DO (     

              [HDragon] this line looks for "received pdf" line and execute a search for the first instance (token=1) it sees the delimiter default of <space>
              question -> how to specify a delimiter of a full stop <.>

                      set "fn=%%H"
                      setlocal enabledelayedexpansion
                      set "fn=!fn:~3!"
                      echo !fn!>>abc-out.txt
                      endlocal

              [Hdragon] can you explain the 5 lines above ? I know the <~3 > does some sort of spacing count
              I have been trying to look for a URL that give tutorials into all these command, do you have one to recommend ?
              )

              Squashman



                Specialist
              • Thanked: 134
              • Experience: Experienced
              • OS: Other
              Re: Extract specific filename from a text file into a new text file
              « Reply #15 on: October 04, 2016, 06:47:23 PM »
              how to specify a delimiter of a full stop <.>
              No idea what this means.

               
              I have been trying to look for a URL that give tutorials into all these command, do you have one to recommend ?
              1) Open up a cmd prompt and type the command name followed by a forward slash and question mark ex: FOR /?
              2) http://ss64.com/nt/

              hdragon33

                Topic Starter


                Greenhorn

                • Experience: Beginner
                • OS: Windows 7
                Re: Extract specific filename from a text file into a new text file
                « Reply #16 on: October 06, 2016, 01:30:58 AM »
                I mean if I want to use delimiter that recognise .

                for example
                abc.123

                I want to use the . as delimiter to start read after it and out put 123

                Squashman



                  Specialist
                • Thanked: 134
                • Experience: Experienced
                • OS: Other
                Re: Extract specific filename from a text file into a new text file
                « Reply #17 on: October 06, 2016, 06:20:29 AM »
                I mean if I want to use delimiter that recognise .

                for example
                abc.123

                I want to use the . as delimiter to start read after it and out put 123
                What if there are multiple delimiters in your data?  What should we do then?

                foxidrive



                  Specialist
                • Thanked: 268
                • Experience: Experienced
                • OS: Windows 8
                Re: Extract specific filename from a text file into a new text file
                « Reply #18 on: October 06, 2016, 11:50:46 PM »
                I mean if I want to use delimiter that recognise .

                for example
                abc.123


                A program does one thing very well but it usually fails when you try to make it do other things.  You've found this out the hard way.   Your comments make it clear that your task is different to the task you described.


                Your further questions are still using inaccurate information - do you realise that you are wasting the time of the person who gave you an answer in the first place?

                hdragon33

                  Topic Starter


                  Greenhorn

                  • Experience: Beginner
                  • OS: Windows 7
                  Re: Extract specific filename from a text file into a new text file
                  « Reply #19 on: October 09, 2016, 09:17:12 PM »
                  Hi Squash and Fox,

                  Guys, please don't take this the wrong way. I am trying to understand how this delimiter works and at the same time learn. True ... it does deviates somewhat from the original needs but I am trying not to start all news threads that is related somewhat.

                  I am new here. But if the norm here is to post new thread although may related I am ok to start a new one.

                  Not trying to waste people's time or undermine the effort


                  foxidrive



                    Specialist
                  • Thanked: 268
                  • Experience: Experienced
                  • OS: Windows 8
                  Re: Extract specific filename from a text file into a new text file
                  « Reply #20 on: October 10, 2016, 11:40:36 PM »
                  please don't take this the wrong way. I am trying to understand how this delimiter works and at the same time learn.

                  That's clear hdragon33 and it's the reason this forum exisits.  Ask your questions by all means.

                  The problem with batch programming is that the code in the solution often changes dramatically if the question/task changes.

                  A person can respond and give the answer but then the question changes and so the answer has to change.  This tends to waste the time that has already been spent to give the answer.   


                  The upshot is to ask a precise question about what you need to do, OR ask about a specific batch technique or command and the readers can chat about that.

                  The need to give precise details is more significant in batch scripting than some other languages.

                  hdragon33

                    Topic Starter


                    Greenhorn

                    • Experience: Beginner
                    • OS: Windows 7
                    Re: Extract specific filename from a text file into a new text file
                    « Reply #21 on: October 11, 2016, 01:30:02 AM »
                    Thank you once again for the response. Will re-compose new question if the need arises

                    patio

                    • Moderator


                    • Genius
                    • Maud' Dib
                    • Thanked: 1769
                      • Yes
                    • Experience: Beginner
                    • OS: Windows 7
                    Re: Extract specific filename from a text file into a new text file
                    « Reply #22 on: October 11, 2016, 07:36:55 AM »
                    Re-compile... 8)
                    " Anyone who goes to a psychiatrist should have his head examined. "