Computer Hope
Microsoft => Microsoft DOS => Topic started by: hdragon33 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
-
Sorry guys... edited output as follows
Expected Output
Inside of abc-out.txt
123456-qwe.sts
223456-qwe.sts
128456-qwe.sts
-
@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
)
-
@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.
@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
-
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.
@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.
-
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...
-
...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
-
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
-
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.
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
-
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" ^<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 ?
)
-
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/
-
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
-
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?
-
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?
-
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
-
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.
-
Thank you once again for the response. Will re-compose new question if the need arises
-
Re-compile... 8)