How can I search a string for a word?

Results 1 to 5 of 5

Thread: How can I search a string for a word?

  1. #1
    Joshua Mann Guest

    Default How can I search a string for a word?

    Im at a loss!!<BR><BR>I&#039m trying to open a text file on my companies fileserver,search for a word, and spit out the results:<BR><BR>Set fso = CreateObject("Scripting.FileSystemObject")<BR>Set t = fso.OpenTextFile(Application("FileServer") & "/AHMasterTextFiles/gift.txt", 1, False)<BR>set rat = t.readall <BR>rfind = //SOME KIND OF SEARCH CODE (SEARCH SPECIFIED BY USER)//<BR>response.write(rfind)<BR><BR>What can I do to search for this..i&#039ve tried everything that i can think of...please have sympothy, my boss may kill me if i cant figure this out!<BR><BR>thanks,<BR>J. Mann

  2. #2
    Join Date
    Dec 1969
    Posts
    2,849

    Default RE: How can I search a string for a word?

    Murder, eh? That&#039s pretty bad.<BR><BR>Well, you&#039ll need to get a string to search on from the user, so you&#039ll need two ASP pages: one that has a FORM which has a TEXT box to collect the user input, and then the second page which is the first page&#039s ACTION in the FORM, and grabs the string they searched on using the Request object. I will explain this in detail if you are lost.<BR><BR>If not, if you know how to use forms and such, then all we need to do is use a VBScript function InStr. (You can read the technical specifics at http://help.activeserverpages.com/iishelp/VBScript/htm/vbs123.htm). Anyway, here is the code you can use to determine if the string is in the text file:<BR><BR>If InStr(rat,rfind,1)=0 then<BR>&#039The String wasn&#039t found<BR>Else<BR>&#039The String was found!<BR>End If<BR><BR>Hope this helps. If you are still thoroughly confused, just let me know what is confusing you and I will try to help you through it. Happy Programming!

  3. #3
    Eric N. Winn Guest

    Default RE: How can I search a string for a word?

    Try this (VBScript)<BR>1st create a dummy file in MyWeb/test/sample.txt, then try the following code:<BR><BR>ForReading = 1<BR>ForAppending = 8<BR><BR>curDir = Server.MapPath("MyWeb est\")<BR>set fso=Server.CreateObject("Scripting.FileSystemObjec t")<BR>&#039 fso.CreateTextFile() will ONLY create a new file<BR>&#039 or overwrite an existing file. <BR>&#039 It doesn&#039t recognize the append flag<BR>set fs = fso.OpenTextFile(curDir+"\"+"sample.txt",ForAppend ing)<BR>fs.WriteLine("&#060;hr>")<BR>fs.WriteLine( "No search string in this line!<BR>")<BR>fs.WriteLine("Entry added on &#060;strong>"+CStr(now)+"&#060;/strong><BR>")<BR>fs.Close()<BR>set fs = fso.OpenTextFile(curDir+"\"+"sample.txt",ForReadin g)<BR>s = ""<BR>Do While Not fs.AtEndOfStream<BR> s = fs.ReadLine()<BR> if InStr(UCase(s),"ENTRY ADDED") then<BR> Response.Write("Found &#060;ENTRY ADDED> in this line: " + s)<BR> End if<BR>Loop<BR>fs.Close()<BR><BR>-Eric

  4. #4
    Eric N. Winn Guest

    Default RE: How can I search a string for a word?

    If you have any questions, drop me a line.<BR><BR>-Eric<BR><BR>enwinn@hti.com<BR>


  5. #5
    Joshua Mann Guest

    Default RE: How can I search a string for a word?

    Thank you so much, I just have one more question. Since the InStr Function returns the byte location how can i get it to return all the text on that "line" after that specified byte location.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •