|
-
Mar 18th, 2007, 02:54 PM
#1
Thread Starter
Lively Member
Some questions about using notepad file
Hi everybody,
1- Suppose I have a notepad, and I want to know what is the number of the line which contains the word "Test"... how can I do that?
and what if more than line contain the same word? How can I show no. of all these lines??
2- Can I copy the text in the line no. (20) of the notepad to a text or label?!
Thanks,
-
Mar 18th, 2007, 04:01 PM
#2
Hyperactive Member
Re: Some questions about using notepad file
Sorry XShadow, you completly misunderstand file I/O.
If you have no idea how to read and write to files then look it up (or google it)!
Computers do not work in line numbers, they work on position. for example:
Code:
open test.txt for binary as ifile
' <- The programs is at line 1 here
get blah,,ifile
' <- After the get statement the program is at line 2
close ifile
To answer your question rather than trying to read each line and check if it is "Test" you could instead read the whole file into an array with ease and then check against that. you could pretend the array index is the line number if you want
-
Mar 18th, 2007, 04:05 PM
#3
Re: Some questions about using notepad file
This answers both of your questions:
Code:
Option Explicit
Private Sub Form_Load()
Dim intFF As Integer, lngN As Long, lngCount As Long, strLines() As String
'change these... or not
Dim strFileName As String: strFileName = "c:\test.txt"
Dim strSearchedWord As String: strSearchedWord = "Test"
Dim blnIgnoreCase As Boolean: blnIgnoreCase = True
Dim lngLineToCopy As String: lngLineToCopy = 20
'searching for the word...
intFF = FreeFile
Open Trim(strFileName) For Input As #intFF
strLines() = Split(Input(LOF(intFF), #intFF), vbCrLf)
Close #intFF
Debug.Print "Lines from '" & Trim(strFileName) & "' that contains word '" & strSearchedWord & "':"
For lngN = LBound(strLines) To UBound(strLines)
If blnIgnoreCase Then
If InStr(1, LCase(strLines(lngN)), LCase(strSearchedWord)) Then
Debug.Print (lngN + 1): lngCount = lngCount + 1
End If
Else
If InStr(1, strLines(lngN), strSearchedWord) Then
Debug.Print (lngN + 1): lngCount = lngCount + 1
End If
End If
Next lngN
If lngCount = 0 Then
Debug.Print "None!"
End If
'displaying the line...
Label1.Caption = strLines((lngLineToCopy - 1))
End Sub
-
Mar 18th, 2007, 04:11 PM
#4
Thread Starter
Lively Member
Re: Some questions about using notepad file
If you have no idea how to read and write to files then look it up (or google it)!
Computers do not work in line numbers, they work on position. for example:
I read many topics about reading text from notepad file... but all of these use the code instr to find a word...
suppose that i found the word and I want to copy the whole line that contains this word into a label... how can I do that?!
gavio... thanks... i will try it
-
Mar 18th, 2007, 04:14 PM
#5
Re: Some questions about using notepad file
 Originally Posted by singularis
Sorry XShadow, you completly misunderstand file I/O.
If you have no idea how to read and write to files then look it up (or google it)!
Computers do not work in line numbers, they work on position. for example:
Code:
open test.txt for binary as ifile
' <- The programs is at line 1 here
get blah,,ifile
' <- After the get statement the program is at line 2
close ifile
To answer your question rather than trying to read each line and check if it is "Test" you could instead read the whole file into an array with ease and then check against that. you could pretend the array index is the line number if you want 
You can read a file line-by-line, using a loop till end-of-file and reading a line using Line Input filenumber, string into which you want it to read
-
Mar 18th, 2007, 04:18 PM
#6
Re: Some questions about using notepad file
 Originally Posted by XShadow
Computers do not work in line numbers, they work on position. for example:
I read many topics about reading text from notepad file... but all of these use the code instr to find a word...
suppose that i found the word and I want to copy the whole line that contains this word into a label... how can I do that?!
gavio... thanks... i will try it
In the gavio's example, when you get the word, just do:
Code:
Label1.Caption = strLines(lngN)
'or if you want to append to current caption
Label1.Caption = Label1.Caption & " " & strLines(lngN)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|