Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Open any file to look for a string inside

Posted on 2000-05-17
15
Medium Priority
?
171 Views
Last Modified: 2010-05-02
I need to open any kind of file to look for a string inside. I tried it with open but in the case of .doc (Word) documents are rare characters that do not allow me to read the complete document. So is it any way to open the file as ascii mode?
0
Comment
Question by:familine
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +6
15 Comments
 
LVL 9

Expert Comment

by:GivenRandy
ID: 2818054
Read a file "YourFileName":
---
    On Error Resume Next
    Open "YourFileName" For Input As #1
    If (Err.Number = 0) Then
        Do While (Not EOF(1))
            Line Input #1, S
            ' do something with S
        End If
    End If
    Close #1
    On Error GoTo 0
---
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2818074
you can cheat and load the text into a richtextbox control and use its find method
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2818079
You can open the file in binary mode, this way you can get all the characters in one step and search for the string using code like this...

Dim Filenr As Integer
Dim Content As String

Filenr = FreeFile
Open filename For Binary As #Filenr
Content = Space$(LOF(Filenr))
Get #Filenr, , Content
Close #Filenr

Now the complete file is inside the Content string, and you can use Instr(Content, search_to_string_for) to search the content.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 14

Expert Comment

by:wsh2
ID: 2818086
Dim strBuffer as String
Open "MyFile" For Binary As #1
strBuffer = Space$(LOF(#1))
Get #1,,strBuffer
Close #1

If INSTR$(strBuffer, "MyString", 1) > 0 _
Then
   MsgBox "String Found"
Else
   MsgBox "String NOT Found"
End If
0
 
LVL 1

Expert Comment

by:j3877
ID: 2818117
Given - you are bracketing things that do not need 2 b bracketed:

do while not eof(1)
    'DO SOMETHING!!!!!!!
loop

will work as well as:

do while (not eof(1))
    'DO SOMETHING!!!!!!!
loop


Also, putting in so many brackets make code look messy, and less like VB code - or is that your idea? Ashamed of being a VB programmer?
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 2818385
(OffTopic)
I bracket conditional statements for readability.  I probably wouldn't have done so here, but I see it as a  personal preference kind of thing.  I don't think it looks messy.
0
 
LVL 15

Expert Comment

by:StingRaY
ID: 2818386
Yup!! familine.
I agree with AzraSound.
Try his suggestion. 8-)
0
 
LVL 15

Expert Comment

by:StingRaY
ID: 2818403
familine:
Because .DOC is not a plain text file.
It's a Word-Formatted file. You cannot read it as a text file.
Rich Text Box can read a simple .DOC file. So you can do as AzraSound mentioned above.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2818412
opening the file as binary is acceptable here as well...my method is pretty lame to be truthful, though i appreciate your vote of confidence in me stingray!  =)

even a richtextbox doesnt handle all the formatting of a word document.  plus it would add more unwanted weight to the project.
0
 
LVL 1

Expert Comment

by:csameer1
ID: 2832058
if you open a .doc file in ascii mode you see a lot of strange chars.but you can also read all the text unless it is password protected


search for file system object in vbhelp

It provides a neat way to open files(don't open .bmp or .jpg files with it ,it has some bug it seems)

user instr keyword to search for  a test
0
 

Author Comment

by:familine
ID: 2903904
Not valid, sorry... l did finally with the richtext trick :-)) it works quite well, as l can use also the find method. l will give the points to AzraSound
0
 
LVL 9

Expert Comment

by:GivenRandy
ID: 2906651
To do that, click on the "Accept..." button to the right of his comment.
0
 
LVL 9

Expert Comment

by:GivenRandy
ID: 3527493
If AzraSound posts it as an answer, you can just accept it then.
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 200 total points
ID: 3529418
thanks for the reminder randy
0
 

Author Comment

by:familine
ID: 4009788
Thanks for help
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question