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.

Open .txt file
Start a loop:
    read each line into a string
    if the line starts with "id="
        take the rest of the string and do what you need with it
End loop

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.

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

Try With this Code

Dim ReqStr as string
Open <File Path/Name> for input as #1
 while not EOF(1)
  Line Input #1,Str1
  If  UCase(Mid(Str1,1,3)) = "ID=" Then
   ReqStr = Mid(str1,4,len(str1))
  End if 

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.

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   

counterr = 1
Do While 1

print #2,part_needed        
counterr = counterr + 2

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


End If
Close #1
Close #2

Hope this helps solving the problem :)

This article has been dead for over six months. Start a new discussion instead.