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 5909 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 ?
              )