Solved

Open any file to look for a string inside

Posted on 2000-05-17
15
158 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
  • 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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 50 total points
ID: 3529418
thanks for the reminder randy
0
 

Author Comment

by:familine
ID: 4009788
Thanks for help
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

831 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