Hi,
Could anyone send me code for following problem..
I have two text files, one containing list of folder names another is a log file containing error description.
I have to find error description from log file for every names in first text file. And i have to copy the description to the first file.

If you able send code for following scenario that wud be fine.

Finding text in notepad (have to get line number).
copying text in particular line and place from notepad.
writing text to particular line.
looping though each line in notepad.

:) Thanx in Advance.

Recommended Answers

All 12 Replies

Ok, let's try to tackle this step by step....

1. There are a number of ways to find data within a string. What we really need to know, is the format of these files.... for example, does the file look like this: 1. c:\windows\somefile.txt or does it look like this: c:\windows\somefile.txt, 1 This is important, because the code is going to be different based on how the first file looks inside. In the first example, you could get item number 5 like so (this also solves the last question, because it loops through the entire file):

' /* This Assumes format: 1. c:\path\file.txt */
open "c:\windows\somefile.txt" for input as #1
     do until eof(1)
          line input #1, tmpvar
          if left(tmpvar, 1) = "5" then
               ' /* Do something here with line 5 of the file */
               msgbox "Line 5 is: " & tmpvar
          end if
     loop
close #1

2. There are a couple of ways to go about extracting data from a string.... if you know the layout of the file, it's easy enough to do something like instr, or split. So, let's say that we have a file, and inside it looks like this: error: c:\somefile.txt is a duplicate , we can do some fancy code, that will extract c:\newfile.txt from that line, with split:

' /* Extract Path From Line of File */
open "c:\newfile.txt" for input as #1
     do until eof(1)
          line input #1, tmpvar
          partsarray = split(tmpvar, " ")
          path = partsarray(1)
     loop
close #1

Now the variable path would contain the path c:\somefile.txt from the file c:\newfile.txt. Granted, it doesn't do anything much, and it certainly doesn't have any error checking, but you get the general idea.

Basically, your layout needs to look something like this:
open file1 ' /* The Paths File */
loop through file1, and loop through file2 at the same time, and compare them. So, you would need to open file1, and on the first loop of file1, you would loop through the entire file of file2. On the second loop of file1, then you'd loop through the entire file2 again.... over and over.

If you give me the layout of each of the files, I can help you more, but it's going to look something like this:

'/* Look for Something From file 1 in file 2 */
open "c:\file1.txt" for input as #1
     do until eof(1) 
          line input #1, tmpvar
           open "c:\file2.txt" for input as #1
                   do until eof(2)
                         line input #2, errorvar
                         if instr(1, tmpvar, errorvar) then
                              msgbox "Match: " & errorvar
                         end if
                   loop
          close #2
     loop
close #1

or something sick like that.

commented: A blinding example of working with text files... just had to add rep for it. +1

Hi Comat,

Thanx for ur reply. Though I have already found a soluton for my problem this reply gave me quite useful information.

could you pls send me code to read a text in particular line. Fox example if i want to read 200th line of text file how can i do it.

hey comatose i have a question.. umMmmm because i have a txt file for example "C:\Myfile\example.txt" i have these data in my file example.txt

a
b
c
d
e
f

i want to just get lines 4 to 6 which contains d,e,f and disregard a,b,c and put it on a list box any suggestion how can i do it? i tried to use your example 1. but there seems to be no output on a msgbox even on the list box. thanks.

hey guys can you please help me i need some idea on how to error trap using a txt from a notepad. for example i have a text named sample.txt which has values of

12345
23456

then i have a txtbox that will input it in the sample.txt but if it has a duplicate entry on the notepad it will have a msgbox "duplicate entry" thanks.

thank you very much but can anyone have a notepad code for visual basic

Ok, let's try to tackle this step by step....

1. There are a number of ways to find data within a string. What we really need to know, is the format of these files.... for example, does the file look like this: 1. c:\windows\somefile.txt or does it look like this: c:\windows\somefile.txt, 1 This is important, because the code is going to be different based on how the first file looks inside. In the first example, you could get item number 5 like so (this also solves the last question, because it loops through the entire file):

' /* This Assumes format: 1. c:\path\file.txt */
open "c:\windows\somefile.txt" for input as #1
     do until eof(1)
          line input #1, tmpvar
          if left(tmpvar, 1) = "5" then
               ' /* Do something here with line 5 of the file */
               msgbox "Line 5 is: " & tmpvar
          end if
     loop
close #1

2. There are a couple of ways to go about extracting data from a string.... if you know the layout of the file, it's easy enough to do something like instr, or split. So, let's say that we have a file, and inside it looks like this: error: c:\somefile.txt is a duplicate , we can do some fancy code, that will extract c:\newfile.txt from that line, with split:

' /* Extract Path From Line of File */
open "c:\newfile.txt" for input as #1
     do until eof(1)
          line input #1, tmpvar
          partsarray = split(tmpvar, " ")
          path = partsarray(1)
     loop
close #1

Now the variable path would contain the path c:\somefile.txt from the file c:\newfile.txt. Granted, it doesn't do anything much, and it certainly doesn't have any error checking, but you get the general idea.

Basically, your layout needs to look something like this:
open file1 ' /* The Paths File */
loop through file1, and loop through file2 at the same time, and compare them. So, you would need to open file1, and on the first loop of file1, you would loop through the entire file of file2. On the second loop of file1, then you'd loop through the entire file2 again.... over and over.

If you give me the layout of each of the files, I can help you more, but it's going to look something like this:

'/* Look for Something From file 1 in file 2 */
open "c:\file1.txt" for input as #1
     do until eof(1) 
          line input #1, tmpvar
           open "c:\file2.txt" for input as #1
                   do until eof(2)
                         line input #2, errorvar
                         if instr(1, tmpvar, errorvar) then
                              msgbox "Match: " & errorvar
                         end if
                   loop
          close #2
     loop
close #1

or something sick like that.

Hi

I am having a problem.

I want to give a text file say file1.txt as an input
I want to read a text file say "file1.txt" line by line
I need to search for the lines starting with ">150"
I need to find the text after the "/" in those lines
I need to print the texts in a text box.

Anybody cud help me out in this???plz post the complete code for this...

could you pls send me code to read a text in particular line. Fox example if i want to read 200th line of text file how can i do it.

Anybody cud help me out in this???plz post the complete code for this...

Comatose has provided an excellent example of reading through files, having a good read through this (and asking questions if you dont understand a part of it), you can get your application reading the text in line by line.
Then, each line you read, increment an integer variable (to be used as a counter in this case) by 1. Now you would do an IF statement to find if the counter is equal or greater than 150 - in this IF statement, just include code to print the lines of text out to an output file.

Comatose has provided an excellent example of reading through files, having a good read through this (and asking questions if you dont understand a part of it), you can get your application reading the text in line by line.
Then, each line you read, increment an integer variable (to be used as a counter in this case) by 1. Now you would do an IF statement to find if the counter is equal or greater than 150 - in this IF statement, just include code to print the lines of text out to an output file.

Hi

Thanks for ur reply. Now i have tried based on it.I am able to read the file & search for the line starts with ">150" & able to print it in messge box as per the below code

Private Sub Command1_Click()


Dim tmpvar As String
Open "c:\test.txt" For Input As #1
     Do Until EOF(1)
          Line Input #1, tmpvar
          If Left(tmpvar, 4) = ">150" Then
          MsgBox "Files are: " & tmpvar
          End If
          
     Loop
Close #1

End Sub

Now My question is i don need to print the entire line,i need to print the text after "/" in the corresponding lines. I don want to display it in message box. I want the text to be displayed in multiline text box...could u plz help me out in this???

My text file text.txt contains the following data

>220 gembrm1.vodafone.com.au FTP server (Version 1.1.214.8 Fri Apr 20 07:27:42 GMT 2001) ready.
>Remote system type is UNIX.
>Using binary mode to transfer files.
>331 Password required for ebftp.
>230 User ebftp logged in.
>200 Type set to I.
>Local directory now /pin/p31102/archive/portal/7.0/apps/vf_au_ebill_extract/OUTPUT
>250 CWD command successful.
>200 PORT command successful.
>150 Opening BINARY mode data connection for ./MrsDONNACAMILLERI-07-08-622289378.TXT.trx.
>226 Transfer complete.
>3479 bytes sent in 0.00 seconds (49962.66 Kbytes/s)
>350 File exists, ready for destination name.
>250 RNTO command successful.
>221 Goodbye.

Here i need to print MrsDONNACAMILLERI-07-08-622289378.TXT.trx.I hope now u mite get it

Hi

I am having a problem.

I want to give a text file say file1.txt as an input
I want to read a text file say "file1.txt" line by line
I need to search for the lines starting with ">150"
I need to find the text after the "/" in those lines
I need to print the texts in a text box.

Anybody cud help me out in this???plz post the complete code for this...

You can't, text files are linear and you have to read the lines sequentially.

Just count to 150 on an input loop and then stop, remember to trap EOF.

Const ForReading = 1

Dim fso, myfile
Dim i, j As Long
Dim inLine As String
Dim charLoc As Long

Set fso = CreateObject("Scripting.FileSystemObject")
Set myFile = fso.OpenTextFile("file1.txt", ForReading)

' check for empty file
If myFile.AtEndOfStream Then
    myfile.Close
    End
End If

i = 0
Do
    inLine = myFile.ReadLine
    ' do nothing until the 150th line reached
    If i >= 150 then
       ' locate the first / character
       charLoc = Instr(1, inLine, "/",vbTextCompare)
       ' load a text form field with everything to the right of the first / character
       UserForm1.Text1.Text = Right(inLine, charLoc + 1)
       ' locate the last / character
       For j = len(inLine) To 1 Step -1
          If StrComp(Mid(inLine,j,1),"/",vbTextCompare) = 0 Then
               UserForm1.Text1.Text = Right(inLine, j + 1)
          End If
       Next j
    End If
    i = i + 1
Loop Until myFile.AtEndOfStream

myFile.Close

from an input word "brain" all possible letter combination from 5 letter combination down to 2 combination and display it in listbox..

and all the valid words are in the notepad..

here is my problem..

how to compare the possible words in a listbox to the valid words in notepad and if the possible word combination is a valid words display it to another listbox.,,.


plz help me,,.,.

You are guilty of two counts.

1. Digging a dead thread / Hijacking others thread.
2. Not showing any effort.

Suggestion :-

1. Start a new thread for your own question.
2. shows some effort and post the code that you are working on.

@Das, it seems we have another trawler on our hands. He obviously did not reply back on this, showed no effort and hijacked this thread. He then went and posted on the other thread which we both rated with a remark like "Yeah".

I'll keep an eye on him and see if he continue with trawling or whether he is a genuine poster.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.