1.

Solve : Extract specific filename from a text file into a new text file?

Answer»

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 advanceSorry guys... edited output as follows

Expected Output
Inside of abc-out.txt

123456-qwe.sts
223456-qwe.sts
128456-qwe.sts 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
) Quote from: Squashman 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
)


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
Quote from: foxidrive on September 29, 2016, 02:52:24 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. Quote from: Squashman on September 29, 2016, 08:38:09 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?I like when you guys double-team....things get done...then the OP gets abducted by aliens... Quote from: patio on September 29, 2016, 09:04:51 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... 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 ...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 Quote from: hdragon33 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
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.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>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 Quote from: hdragon33 on October 03, 2016, 06:46:09 PM
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.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" ^
[HDragon] this line looks for "received pdf" line and execute a search for the first instance (token=1) it sees the delimiter default of
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 ?
)


Discussion

No Comment Found