text file strings help

Reply

Join Date: Aug 2006
Posts: 1
Reputation: galaxies is an unknown quantity at this point 
Solved Threads: 0
galaxies galaxies is offline Offline
Newbie Poster

text file strings help

 
0
  #1
Aug 5th, 2006
I ask for simple help. I'm at a part of my application where I need it to open a text file, and search for id numbers within the text. There will be several instances of "id=######"s that I will need to grab from the text file, and put them line by line into another box so that I'm able to save just those id's that i want.

so... here's what I need for my program to do: Open .txt, find just the number part of the "id=xxxxxx", and put them one by one into another text field so that i'm able to save them as another text file,
keeping just the ripped strings that contains the ids, ya know? I hope i'm making sense.. any help would be greatly appreciated.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: text file strings help

 
0
  #2
Aug 5th, 2006
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Open .txt file
  2. Start a loop:
  3. read each line into a string
  4. if the line starts with "id="
  5. take the rest of the string and do what you need with it
  6. End loop
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: text file strings help

 
0
  #3
Aug 6th, 2006
I have a pretty good idea of what you mean..... you don't want the new text file to just have ID's right? You want it to have ID's AND the information that is attached to those ID's..... but only the one's that have been selected by the GUI. right?

Please also post a portion of the .txt file that contains the ID's, so I can see the best method to go about parsing the data.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 45
Reputation: maheshsayani is an unknown quantity at this point 
Solved Threads: 2
maheshsayani's Avatar
maheshsayani maheshsayani is offline Offline
Light Poster

Re: text file strings help

 
0
  #4
Aug 7th, 2006
Open <File Path/Name> for input as #1
while not EOF(1)
Line Input #1,Str1

take the rest of the string and do what you need with it
End loop
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 45
Reputation: maheshsayani is an unknown quantity at this point 
Solved Threads: 2
maheshsayani's Avatar
maheshsayani maheshsayani is offline Offline
Light Poster

Re: text file strings help

 
0
  #5
Aug 7th, 2006
Hi
Try With this Code

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim ReqStr as string
  2. Open <File Path/Name> for input as #1
  3. while not EOF(1)
  4. Line Input #1,Str1
  5. If UCase(Mid(Str1,1,3)) = "ID=" Then
  6. ReqStr = Mid(str1,4,len(str1))
  7. End if
  8. Wend
Last edited by Comatose; Aug 7th, 2006 at 2:46 pm. Reason: Pointless Bold Tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: text file strings help

 
0
  #6
Aug 7th, 2006
The above post may or may not work, depending on if the ID= portion of the string is NOT the first portion of the line... for example:
hello;this is alineID=54678 and some stuff. If the line starts with the ID, then it will work to extract the ID portion and save the rest in a variable, but it won't extract the actual ID number. If you could post a portion of the .txt file, we could put together a much more efficient function for you.
Last edited by Comatose; Aug 7th, 2006 at 2:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1
Reputation: fidodidoss is an unknown quantity at this point 
Solved Threads: 0
fidodidoss fidodidoss is offline Offline
Newbie Poster

Re: text file strings help

 
0
  #7
Jul 6th, 2008
lets say that this is a line in your input text file;
this is aline"ID=54678" and some stuff

the code will be
******************
dim x() as string
dim y() as string

open "C:\output_file.txt" for append as #2    'open an output file where your IDs are stored

open "c:\testfile.txt" for input as #1    'open the file where are the IDs exist
do while not EOF(1)        'loop on line inside the file
line input #1,linee        'store this line in variable linee (don't write it line as it is a reserved word)

if instr(linee,"ID=")<>0 then  'check if the string "ID=" exists in the line,if it doesn't exist it will give ZERO , else it gives the order of the first character , <> means not equal
x=split(linee,"ID=")          'split the line containing "ID=" into 2 parts , one before "ID=" and another after it
second_part=x(1)             'you want the part after it , but it also contains the rest of the line " and some stuff"
y=split(second_part,"""")     'so split it into 2 parts one before the quotation """" and others after 
part_needed=y(0)            'your part is the first part before the quotation """"
print #2,part_needed  'write this part in your result file
end if                'end of if

loop                  'here the rest of the do while command
close #1              'closing input file
close #2              'closing output file




there may be another problem here , lets make line moe complex like;
line1"ID=54678" sdf dfgdfg dgdf "ID=5555"ad aewrre"ID=567"
asa"ID=5566" iiiiiiiiiiii"ID=9999"


here the line may have more than one ID and not a fixed number (as you can see the 1st line has 3 IDs while the 2nd only 2)

so what to do???

here you want to take values between each "ID=" then remove the unwanted string by splitting according
to the quotation """",but the number of "ID=" not always the same.so you have to use the Ubound function
which returns the number of cells in your array x as you will see in the code below

You take x(1),x(3),x(5),........till you reach the upper bound of x or less (you cant exceed than the upper bound)
But why to begin with x(1) not x(0)?? that is because you want what is between "ID=" and next "ID=" so if you take
x(0) you will get wrong terms



the code will be
******************
dim x() as string
dim y() as string

open "c:\output_file.txt" for append as #2    
open "c:\testfile.txt" for input as #1    
do while not EOF(1)        
line input #1,linee       

if instr(linee,"ID=")<>0 then   
x=Split(linee,"ID=")

counterr = 1
Do While 1

y=split(x(counterr),"""")     
part_needed=y(0)            
print #2,part_needed        
counterr = counterr + 2

If counterr > UBound(x) Then
Close #2
Close #1
Exit Sub
End If

Loop

End If
Close #1
Close #2


Hope this helps solving the problem
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC