[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Open any file to look for a string inside

Posted on 2000-05-17
15
Medium Priority
?
182 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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month8 days, 17 hours left to enroll

590 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