Question

Open and Search a .txt file ( VB6.0 )

Asked by: Carol2009

How can i do the following in visual basic v6?

Open a .txt file ( e.g c:\test.txt )
Start searching for the string <a href=" from the top of the .txt file
If found on .txt file then grab it from <a href=" until the character > appears. ( I need to get the urls back to my visual basic project.
Continue searching for onothers until you reach the end of the .txt file

.txt file example
------------------
Here is some random text..... random text
random text... <a href="http://www.experts-exchange.com/?UID8327">Random Text</a>
Random text....
RandomText -- Random Text /// <a href="http://www.google.com">Random text</a> Random text
------------------

On the example i have to get "http://www.experts-exchange.com/?UID832" and http://www.google.com/ back to my visual basic v6.0 project.

Thanks!

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-09-06 at 22:16:01ID24711869
Tags

search

,

string

,

file

Topic

Visual Basic Programming

Participating Experts
2
Points
500
Comments
20

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. How to convert a .txt to ascii using Visual Basic v6.0
    I have a .txt file which contains non-ascii code like, Y!÷äÝs P!÷äÕY 6!÷äÎi ,!÷ä¸ !÷ä²s !÷ä©y !÷ä¦$ How do i remove all the strange characters from the .txt and remain the normal ones ? Please help
  2. Open "" & (App.Path & "test.txt") For Input As #…
    I am trying to import some the first 6 lines from .txt into my 6 textboxes, i am using already a code but i need an alternative, here is my code. Open "" & (App.Path & "\temp\pptables.txt") For Input As #1 Input #1, stext Text1.Text = stext Inp...
  3. Search .txt file for email addresses - VB6.0
    Hi, is there any code for visual basic v6.0 which i can use to grab all the email addresses from my .txt file? Thank you

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: angelIIIPosted on 2009-09-06 at 22:33:31ID: 25272844

I presume the same answer as your other question can be used:
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_24709691.html

I presume you know how to read a .txt file?

 

by: Carol2009Posted on 2009-09-06 at 23:01:37ID: 25272960

No i don't :(

 

by: Carol2009Posted on 2009-09-06 at 23:02:00ID: 25272961

Could you please help me out?

 

by: angelIIIPosted on 2009-09-06 at 23:16:49ID: 25272998

ok, let's start to read a file, line by line:
http://www.dreamincode.net/code/snippet318.htm

 

by: Carol2009Posted on 2009-09-06 at 23:19:31ID: 25273007

ok, so what's next? search the string <a href=?

 

by: angelIIIPosted on 2009-09-06 at 23:21:50ID: 25273016

as I said, check out the suggestion from your other question.
the regular expression search is exactly what you are looking for (though it is not my strong point...)

 

by: Carol2009Posted on 2009-09-06 at 23:25:22ID: 25273030

Angelll thanks for your time but i tihnk i will not be able to code it my self, i just started learning vb. If you can come across to a solution please let me know

 

by: angelIIIPosted on 2009-09-07 at 00:09:25ID: 25273170

I did understand that you are learning...
so, what exactly do you fail to understand from the link in your other question?
I mean, did you try, at least?

 

by: craisinPosted on 2009-09-07 at 19:28:49ID: 25278328

I have a working code for you and will post it as soon as I get back from work. It is exactly what you are after and is a good learning tool.  :-) (Will post in about 4 hours).

I think you will like it.

 

by: angelIIIPosted on 2009-09-07 at 22:32:00ID: 25278864

craisin,
  please remember: for learning "questions", rather give guidance, and not plain "code".
thanks

 

by: craisinPosted on 2009-09-08 at 01:15:31ID: 25279631

Yes, but this code is for learning purposes since it is well documented and allows the Questioiner to see exactlky what is happening. The example code links are faulty in that they do not allow for multiple-line spanning (HTML code links can span multiple lines).

Stand by....

 

by: craisinPosted on 2009-09-08 at 04:56:18ID: 25280881

This is the sample of the sort of code you need.

1. Create a new project .

2. Add a form as showing in the attached diagrams 9the font size of text on the form is to be
"Tahoma Regular 12"

3. Double click on the form once it is created and add the following code (showing below)

4. Add modules to your project as showing in the NEXT comment

Note! This code will not "span" lines (i.e. will not find lines where the starting text is on one line, and the ending text is on another line). Will leave it up to your intelligence to modify the code to do that (it's tricky but will be fun to solve).

This code will fully documented (as all good code should be) . Its functionality is to search a file and display all the occurrences of certain text in a file to the user (by placing it in a listbox)

The main function here is "CKRFindInFile" which is a function I wrote.
It searches a file line by line and finds if there is an occurrence of text STARTING with
"something" and ENDING with "something".

(By the way, ALL functions I have written myself start with CKR (my initials)...Vanity I suppose :-)

So all you really need to do in your OWN project is make use of the array CKRFindInFile!

Hope this a learning experience for you and solves your problem as well.... :-)

Option Explicit
 
Private Sub cmdClose_Click()
  'always UserControl "Unload" rather than simply "End" so that any
  'processes stored in the "Form_Unload() function will be processed
  'first
  Unload Me
End Sub
Private Sub Form_Unload(Cancel As Integer)
  'save the values showing on the form, for re-use next time!
  SaveSetting App.EXEName, "Setup", "txtStart.text", txtStart.Text
  SaveSetting App.EXEName, "Setup", "txtEnd.text", txtEnd.Text
  SaveSetting App.EXEName, "Setup", "txtFileName.text", txtFileName.Text
End Sub
 
Private Sub cmdCommand1Process_Click()
  Dim bProcess As Boolean     'A flag indicating if we can proceed with the
                              'process
  Dim cFindInFile() As String 'an array in which to store the lines found
  Dim x As Integer            ' an iterative counter used to process each element in the array
  
  'assume all will go well and we can process the file
  bProcess = True
  'Clear the values from the Listbox
  lstStringsFound.Clear
    
  'check the user has some text value for "starting with"
  If Len(Trim(txtStart.Text)) = 0 Then
    MsgBox "Please input " + cHr(34) + "starting" + cHr(34) + " string"
    bProcess = False
  Else
    'check the user has some text value for "end with"
    If Len(Trim(txtEnd.Text)) = 0 Then
      MsgBox "Please input " + cHr(34) + "ending" + cHr(34) + " string"
      bProcess = False
    End If
  End If
  'If no problems found.....
  If bProcess Then
  
    'store the found strings in the array
    cFindInFile = CKRFindInFile(Trim(txtFileName.Text), _
                                Trim(txtStart.Text), _
                                Trim(txtEnd.Text))
    'stores the values in the array into the listbox
    For x = 0 To UBound(cFindInFile)
      lstStringsFound.AddItem cFindInFile(x)
    Next
  End If
End Sub
 
Private Sub cmdFile_Click()
  'Open up a "file selection" dialogue
  'Set a few default values
  CommonDialog1.InitDir = GetSetting(App.EXEName, "Setup", "CommonDialog1", "C:\")
  CommonDialog1.FileName = "*.txt"
  CommonDialog1.ShowOpen
  
  'Store values for next time
  SaveSetting App.EXEName, "Setup", "CommonDialog1", CKRFileDirectory(CommonDialog1.InitDir)
  
  If FileExists(CommonDialog1.FileName) Then
    'store the filename in thmain form of the project
    txtFileName.Text = CommonDialog1.FileName
  End If
 
End Sub
 
Private Sub Form_Load()
  'load any values we have previous stored in the Registry
  'via the "SaveSetting" function
  txtStart.Text = GetSetting(App.EXEName, "Setup", "txtStart.text", "")
  txtEnd.Text = GetSetting(App.EXEName, "Setup", "txtEnd.text", "")
  txtFileName.Text = GetSetting(App.EXEName, "Setup", "txtFileName.text", "")
End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:

Select allOpen in new window

 

by: craisinPosted on 2009-09-08 at 05:06:06ID: 25280947

And now the modules to be added to your project as per the attached diagram.
The modules all appear together in the attached code snipett. They will need to be seperated and placed in their own module files (.bas files) within the project. (as per attached diagram)

Have fun....I still think Visual Basic is a fun way to program! :-)

'===============================================================
'CKRFindInFile Module
'
Option Explicit
'Always use the "explicit" option in any module.....
'we want the compiler to force us to define all
'variable types (avoids errors in finished product)
 
Public Function CKRFindInFile(cFile As String, cStart As String, _
                     cEnd As String) As String()
  'This function (made public so any module in a project will be able to
  'use it) searches a text file from first line to last and returns an
  'array of strings found in the file.
  'The strings start with a certain "Starting characters" and end with
  'certain "ending characters" as passed to the function.
  
  'Parameters: cFile: The name of the file (including path).
  '                   If the File does not exist, this function will
  '                   simply return an empty array (i.e.no values)
  '            cStart: The starting characters of the string
  '            cEnd  : The ending characters of the string
                      
  
  'Define a few things
  Dim nFile As Integer       'A file "handle" for the text file we are opening
  Dim cLine As String        'Variable storing the value of the last line read
  Dim nFind1 As Integer      'The starting position of the "starting characters" within the line
  Dim nFind2 As Integer      'The starting position of the "ending characters" within the line
  Dim nLen As Integer        'The length of the found string (calculated from nFind1 and nFind2)
  Dim FindInFile() As String 'A temporary array holding the values found (no size assigned yet)
  
  'Initialize the array
  'You can "redemsion" (make bigger/smaller) an array which was defined
  'with NO array size as per the Dim statement for FindInFile()
  ReDim FindInFile(0)
  
  'Only open and read in a file if it exists (of course)
  'Refer to the function "FileExists" found in this project
  If FileExists(cFile) Then
    'Open the file for reading only
    nFile = FreeFile
    Open cFile For Input As #nFile
    
    'Read in the first line
    Line Input #nFile, cLine
    
    'Until the end of the file, do the following
    Do While Not EOF(nFile)
      'Find the position of the "starting with" text
      nFind1 = InStr(cLine, cStart)
      
      'If we find the starting text
      If nFind1 > 0 Then
        'find the ending text in the same line starting at the end of the "starting text"
        nFind2 = InStr(nFind1 + Len(cStart) + 1, cLine, cEnd)
        
        'If we find the ending text too (and the ending text occurs after the starting text)
        If nFind2 > nFind1 Then
          'add the found string to our array
          'We retain previous values in the array by using the option "Preserve"
          'so first of all expand the array by 1 element if it already has a value
          If UBound(FindInFile) > 0 Then
            ReDim Preserve FindInFile(UBound(FindInFile) + 1)
          End If
          
          'calculate the length of the found string
          nLen = nFind2 - nFind1 + Len(cEnd)
          
          'store the value in the last element of the array
          FindInFile(UBound(FindInFile)) = _
                 Mid$(cLine, nFind1, nLen)
        End If
        'we must search the rest the string after any occurrence,
        
        If nLen > 0 Then
          'so pick up the resst of the line, starting at the end of the
          'string just found.
          cLine = Mid$(cLine, nLen)
        Else
          'If no string was found, just read in another line!
          Line Input #nFile, cLine
        End If
      Else
        'No starting text found, so read in another line
        Line Input #nFile, cLine
      End If
    Loop
  End If
  'Store the entire array into the return value of the function
  CKRFindInFile = FindInFile
End Function
 
Public Function FileExists(cFile As String) As Boolean
  Dim fs As Object
  Set fs = CreateObject("Scripting.FileSystemObject")
  FileExists = fs.FileExists(cFile)
End Function
 
 
 
'=========================================================================
'CKRFileDirectory Module
'
'force declarations to ensure optimized code
Option Explicit
 
'this function returns the directory in which a file exists
'(i.e. strips out the file name itself.)
 
'Note: The file does NOT have to necessarily exist!
 
'-------------------------------------------------------------------------------------------------
'Parameters:        cFile as String             - Destination directory
'Return Value:      CKRFileDirectory as string  - name of directory in which file exists
'-------------------------------------------------------------------------------------------------
 
Public Function CKRFileDirectory(cFile As String) As String
 
Dim cDir     As String
 
Dim nAt      As Integer
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
CKRFileDirectory = fs.GetparentFoldername(cFile) + "\"
 
End Function
 
'==========================================================================
' The Max function module
Option Explicit
 
Public Function Max(nVal1 As Variant, nVal2 As Variant) As Variant
'This function returns the maximum of two items being compared
 
If nVal1 > nVal2 Then
  Max = nVal1
Else
  Max = nVal2
End If
 
End Function

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:

Select allOpen in new window

 

by: Carol2009Posted on 2009-09-09 at 01:24:35ID: 25289174

I just need the code to bring all the URLS back to my vb 6.0 project. Nothing academic lol

Craisin, please post a solution please

 

by: Carol2009Posted on 2009-09-09 at 01:24:46ID: 25289175

And i am using VB6.0 btw

 

by: craisinPosted on 2009-09-10 at 06:29:02ID: 25299913

Carol2009,

I have pointed out to the administrator that although the code has now become "unhidden" they have not restored the screen images I had attached. (There are 11 of them!)

I have asked then to rectify this, but if they can't I will have to attach them again.
Let's just see if they can "unhide" the 11 images. (fingers crossed...it takes ages to attach 11 files with comments....sigh!)

 

by: craisinPosted on 2009-09-12 at 00:26:40ID: 25315512

Carol2009....

I am off to Church now, but when I return I will re--load the images, since Admin advises that once they "hide" the comments and images they can only ":unhide" the comments
(goodness me!) There are qite a few images and it will take a while, but hang in there. I'll post them shortly.

 

by: craisinPosted on 2009-09-12 at 13:20:23ID: 25317756

The files are re-attached.

If you have any queries just ask.....

Once you have experimented creating this Test Project, you will be able to simply insert the code for the function CKRFindInFile() into your own project, make a call to the function to received back an array of values as required.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...