crwncad702 Posted October 3, 2008 Posted October 3, 2008 Hello everyone... Forgive me, but it's been a while since I've done any coding that really works with text files, but I was curious how I could search an entire text file (256 lines plus...) for a specific piece of text that has already been supplied through a variable? I know that I can go line by line, but I am not sure how to get it to stop and basically read the entire line on which the variable has been found. Any ideas or help? Quote
borgunit Posted October 3, 2008 Posted October 3, 2008 In vb it would go like this Dim sNextLine As String dim sSearchFor as string ''''''''''''''''''''''''''''''''''''''' Open sFilePath For Input As #1 Do Until EOF(1) ' read one line at a time from the file, assign to sNextLine Line Input #1, sNextLine If InStr(sNextLine, sSearchFor) > 0 Then "DO WHAT YOU WANT HERE WITH sNextLine" End If Loop Close #1 Quote
crwncad702 Posted October 3, 2008 Author Posted October 3, 2008 I would love it being that simple, but I don't really know VB or VBNet... I haven't been able to take the leap over yet any ideas for lisp? Quote
rocheey Posted October 3, 2008 Posted October 3, 2008 Hello everyone... Forgive me, but it's been a while since I've done any coding that really works with text files, but I was curious how I could search an entire text file (256 lines plus...) for a specific piece of text that has already been supplied through a variable? I know that I can go line by line, but I am not sure how to get it to stop and basically read the entire line on which the variable has been found. Any ideas or help? ' heres a little code to get you started. It comes with a test sub. You pass it the string you wish to find, the full file/path name of the TEXT file you want to search, and whether the search is case sensitive or not. Theres plenty here that it DOESNT do, but hey, what do you want, for free? '---- snip------------------------------------------ Sub test() Dim retStr As String retStr = LineFromText("timeout", "c:\boot.ini", False) MsgBox "*" & retStr & "*" End Sub Function LineFromText(strSearchText As String, strFileSpec As String, MatchCase As Boolean) As String ' Opens a TEXT file (only!) and searches line by line for a line of text ' that contains the search string. Quits on the first line it finds. ' Returns the entire line of text, or "" if none found. If MatchCase is True, ' the capitalization must be exact. Dim FileExists As String Dim NextFile As Integer Dim FileString As String Dim txtSearchStr As String FileExists = Dir$(strFileSpec) ' see if file actually exists on disk If FileExists = "" Then Exit Function ' Put our search string into a variable txtSearchStr = strSearchText If MatchCase = True Then txtSearchStr = UCase$(txtSearchStr) NextFile = FreeFile ' get next available File handle Open strFileSpec For Input As #NextFile Do Line Input #NextFile, FileString If MatchCase = True Then FileString = UCase$(FileString) ' now check to see if our search string is in this line... If InStr(FileString, txtSearchStr) Then LineFromText = FileString: Exit Do Loop Until EOF(NextFile) Close #NextFile End Function Quote
crwncad702 Posted October 3, 2008 Author Posted October 3, 2008 any other ideas that use lisp? Sorry, it is kinda a retro fit...on top of my lack of vba. Quote
ASMI Posted October 4, 2008 Posted October 4, 2008 Lisp solution: (defun c:tfind(/ fTxt tFile fDscr lCnt mCnt cLn *error*) (defun *error*() (if fDscr(close fDscr)) (princ) ); end of *error* (if (and (/= ""(setq fTxt(getstring T "\nSpecify text to find: "))) (setq tFile(getfiled "Select text file" "" "txt" 4)) ); end and (if (setq fDscr(open tFile "r")) (progn (setq lCnt 1 mCnt 0) (while(setq cLn(read-line fDscr)) (if(wcmatch cLn(strcat "*" fTxt "*")) (progn (textscr) (setq mCnt(1+ mCnt)) (princ(strcat "\nText specified found in line ["(itoa lCnt)"] ")) (princ(strcat "\n\"" cLn "\" ")) (getkword "\nPress Enter to continue... ") ); end progn );end if (setq lCnt(1+ lCnt)) ); end while (princ(strcat "\n"(itoa mCnt) " strings found. ")) (close fDscr) ); end progn (princ "\n<!> Can't to open file specified! <!> ") ); end if ); end if (princ) ); end of c:tfind Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.