Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 107,162 VB Programmers for FREE! Ask your question and get quick answers from experts. There are 1,380 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Handlind notepad in VB

 
Reply to this topicStart new topic

Handlind notepad in VB

cindrella_win05
post 6 Aug, 2008 - 12:08 PM
Post #1


New D.I.C Head

*
Joined: 6 Aug, 2008
Posts: 4

Hi I have to solve the following problem

I have a text file say file1.txt

I have to give this file as input & i need to search for particular text.

For exmaple I have to search for the lines starting with ">150" & I need to read the line & i need to find & display the text after the symbol "/" in those lines in the output window.

Could anyone plz send me the full code & steps for this.

I am a fresher so plz explain me in detail.
User is offlineProfile CardPM

Go to the top of the page


cmount
post 6 Aug, 2008 - 12:28 PM
Post #2


New D.I.C Head

*
Joined: 1 Aug, 2008
Posts: 41



Thanked 1 times
My Contributions


QUOTE(cindrella_win05 @ 6 Aug, 2008 - 12:08 PM) *

Hi I have to solve the following problem

I have a text file say file1.txt

I have to give this file as input & i need to search for particular text.

For exmaple I have to search for the lines starting with ">150" & I need to read the line & i need to find & display the text after the symbol "/" in those lines in the output window.

Could anyone plz send me the full code & steps for this.

I am a fresher so plz explain me in detail.


Normally this forum wants you to show an ATTEMPT at code first. Granted you don't know where to start, so I'll give you some pointers.

Searching the forum & the internet can often give you some decent examples of where to start & what code to adapt.

The syntax of OpenTextFile might be a good place to start

The following I copied from part of the VB help file regarding this:

object.OpenTextFile(filename[, iomode[, create[, format]]])

object Required. Always the name of a FileSystemObject.
filename Required. String expression that identifies the file to open.
iomode Optional. Indicates input/output mode. Can be one of two constants, either ForReading or ForAppending.
create Optional. Boolean value that indicates whether a new file can be created if the specified filename doesn't exist. The value is True if a new file is created; False if it isn't created. The default is False.
format Optional. One of three Tristate values used to indicate the format of the opened file. If omitted, the file is opened as ASCII.

Remarks

The following code illustrates the use of the OpenTextFile method to open a file for appending text:

vb
Sub OpenTextFileTest
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("c:\testfile.txt", ForAppending,TristateFalse)
f.Write "Hello world!"
f.Close
End Sub
User is offlineProfile CardPM

Go to the top of the page

cindrella_win05
post 6 Aug, 2008 - 12:34 PM
Post #3


New D.I.C Head

*
Joined: 6 Aug, 2008
Posts: 4

QUOTE(cmount @ 6 Aug, 2008 - 12:28 PM) *

QUOTE(cindrella_win05 @ 6 Aug, 2008 - 12:08 PM) *

Hi I have to solve the following problem

I have a text file say file1.txt

I have to give this file as input & i need to search for particular text.

For exmaple I have to search for the lines starting with ">150" & I need to read the line & i need to find & display the text after the symbol "/" in those lines in the output window.

Could anyone plz send me the full code & steps for this.

I am a fresher so plz explain me in detail.


Normally this forum wants you to show an ATTEMPT at code first. Granted you don't know where to start, so I'll give you some pointers.

Searching the forum & the internet can often give you some decent examples of where to start & what code to adapt.

The syntax of OpenTextFile might be a good place to start

The following I copied from part of the VB help file regarding this:

object.OpenTextFile(filename[, iomode[, create[, format]]])

object Required. Always the name of a FileSystemObject.
filename Required. String expression that identifies the file to open.
iomode Optional. Indicates input/output mode. Can be one of two constants, either ForReading or ForAppending.
create Optional. Boolean value that indicates whether a new file can be created if the specified filename doesn't exist. The value is True if a new file is created; False if it isn't created. The default is False.
format Optional. One of three Tristate values used to indicate the format of the opened file. If omitted, the file is opened as ASCII.

Remarks

The following code illustrates the use of the OpenTextFile method to open a file for appending text:

Sub OpenTextFileTest
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("c:\testfile.txt", ForAppending,TristateFalse)
f.Write "Hello world!"
f.Close
End Sub


Thanks for your guidance...but i don need to append any text,i jus have to search for coresponding text & display it...so plz explain me dat...
User is offlineProfile CardPM

Go to the top of the page

cmount
post 6 Aug, 2008 - 12:55 PM
Post #4


New D.I.C Head

*
Joined: 1 Aug, 2008
Posts: 41



Thanked 1 times
My Contributions


QUOTE

Thanks for your guidance...but i don need to append any text,i jus have to search for coresponding text & display it...so plz explain me dat...


The example in the previous is a generic example; I'm not entirely sure how to do exactly what you want; others can help you more. I have a feeling the sentiment of this forum is they want you to show some (even a very awful) attempt at code. I'm trying to get you to the point where you can show something you are thinking of doing--so others can pick up what I'm not sure how to do.

Take a look at this thread http://www.dreamincode.net/forums/showtopic4091.htm

[edit]: i realize now this isn't all that similar to what you're looking for

You might also take a look at
http://www.dreamincode.net/code/snippet318.htm
http://www.dreamincode.net/forums/showtopic51698.htm

Again, these aren't really what you're looking for either, but they might give you a start to post at least a couple lines of code to show enough effort for the other forum experts to help.

Sorry I'm working on my project right now & just taking a short break to try to help post a couple links

A lot of times you can find code sort of similar & then do a little researching to figure out how to adapt it to do what you want.

This post has been edited by cmount: 6 Aug, 2008 - 01:03 PM
User is offlineProfile CardPM

Go to the top of the page

cindrella_win05
post 6 Aug, 2008 - 01:02 PM
Post #5


New D.I.C Head

*
Joined: 6 Aug, 2008
Posts: 4

QUOTE(cmount @ 6 Aug, 2008 - 12:55 PM) *

QUOTE

Thanks for your guidance...but i don need to append any text,i jus have to search for coresponding text & display it...so plz explain me dat...


The example in the previous is a generic example; I'm not entirely sure how to do exactly what you want; others can help you more. I have a feeling the sentiment of this forum is they want you to show some (even a very awful) attempt at code. I'm trying to get you to the point where you can show something you are thinking of doing--so others can pick up what I'm not sure how to do.

Take a look at this thread http://www.dreamincode.net/forums/showtopic4091.htm

A lot of times you can find code sort of similar & then do a little researching to figure out how to adapt it to do what you want.

ok lemme try with this exampleee...

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' /* This Assumes format: 1. c:\path\file.txt */

By this above code i can open & read a file & print the correspondin line in the message box...Now my question is wat shud i do if i need to search for a particular line say for example the line starts with ">150" & to print the text after the "/" symbol in those lines...
User is offlineProfile CardPM

Go to the top of the page

cmount
post 7 Aug, 2008 - 07:44 AM
Post #6


New D.I.C Head

*
Joined: 1 Aug, 2008
Posts: 41



Thanked 1 times
My Contributions


Sorry I'm not an expert at VB =( I started learning about 2 weeks ago--teaching myself searching internet forums like this & just messing around. I don't have a quick solution right off hand.

Did you see the 2 other links I added into my post via an edit (the code snippet)?

Personally, my first instinct was to use a for loop to count through the lines & perform the search like that.

I found another link that should help you a bit more!

http://www.vbforums.com/showthread.php?t=415639

You aren't wanting to delete the lines with that text, but if you adapt that code to count through the lines of the text file, I think you can get it to work.

Inside your for loop, you'd probably want to use an If statement to limit which lines to perform the search.

I just re-read your first post, and I think I might have missed a major detail.

You said you wanted to find all text after /
Does this have anything to do with a file list/directory? Because chances are--if you're wanting a PLAIN file list (without paths, there's some command prompt code you could execute via VB to write a .bat file that would do this very easily)

Also, have you thought about having the VB code write a NEW text file by replacing the text BEFORE the last / with [nothing]?

Everything I've been doing recently is for VBA in excel, so a simple replace text might accomplish this.

However, if you're wanting a file listing--that could be VERY much simplified.

QUOTE(cindrella_win05 @ 6 Aug, 2008 - 01:02 PM) *

QUOTE(cmount @ 6 Aug, 2008 - 12:55 PM) *

QUOTE

Thanks for your guidance...but i don need to append any text,i jus have to search for coresponding text & display it...so plz explain me dat...


The example in the previous is a generic example; I'm not entirely sure how to do exactly what you want; others can help you more. I have a feeling the sentiment of this forum is they want you to show some (even a very awful) attempt at code. I'm trying to get you to the point where you can show something you are thinking of doing--so others can pick up what I'm not sure how to do.

Take a look at this thread http://www.dreamincode.net/forums/showtopic4091.htm

A lot of times you can find code sort of similar & then do a little researching to figure out how to adapt it to do what you want.

ok lemme try with this exampleee...

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' /* This Assumes format: 1. c:\path\file.txt */

By this above code i can open & read a file & print the correspondin line in the message box...Now my question is wat shud i do if i need to search for a particular line say for example the line starts with ">150" & to print the text after the "/" symbol in those lines...


User is offlineProfile CardPM

Go to the top of the page

Ken Halter
post 7 Aug, 2008 - 09:01 PM
Post #7


New D.I.C Head

*
Joined: 18 Nov, 2007
Posts: 29



Thanked 4 times
My Contributions


QUOTE

ok lemme try with this exampleee...

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' /* This Assumes format: 1. c:\path\file.txt */

By this above code i can open & read a file & print the correspondin line in the message box...Now my question is wat shud i do if i need to search for a particular line say for example the line starts with ">150" & to print the text after the "/" symbol in those lines...


Well, you're already searching for a line that starts with "5", so searching for ">150" should be just as easy.

When it comes to grabbing that line and doing something with its contents, starting with a certain character, either InStr or InStrRev will work, depending on if you want to start the search at the beginning (InStr) or the end (InStrRev) of the string.

So... you have:

if left(tmpvar, 1) = "5" then

Searching for ">150" would be exactly the same:

if left(tmpvar, 4) = ">150" then

Now, say that string is ">150 This is test X of Y" and you want to change the X to "6" for example.

vb


anotherTmp = InStr(1, tmpvar, "X") 'first, find the X

If anotherTmp > 0 Then 'if the return is non-zero, the target was found
'Mid$ can read (as a function) or write (as a sub) to any part of a string
'it needs the variable that holds the string, the replacement value and
'the number of characters to replace. It can get quite complex and trash your
'string if not careful... no big deal, just fix and go on.

Mid$(tmpvar, anotherTmp, 1) = "6"

End If

'Now, if you show that string it should look like:

">150 This is test 6 of Y"

User is offlineProfile CardPM

Go to the top of the page

jens
post 8 Aug, 2008 - 04:54 AM
Post #8


New D.I.C Head

Group Icon
Joined: 9 May, 2008
Posts: 49



Thanked 1 times

Dream Kudos: 50
My Contributions


Hello!

Yet a way to do what you are after. Not a complete solution but hopefully some help.

vb
 
While Not EOF(fileId)
Line Input #fileId, temp
'Here I look for the string find within the string temp.
'If found the integer posFunnen will contain the index
'where find starts within temp. N.B: First position is 1.
'In my case I want upper/lower case to be igored.
'That's why I do the "UCase" stuff.
posFunnen = InStr(UCase(LTrim(temp)), UCase(find))
If posFunnen > 0 Then
'If you land here you have found the string and you know
'what position it starts in. In my case I check some other
'stuff and then add the complete row to an other string.
'You'd have to find a way to only get part of the string.
'Look at e.g: http://www.example-code.com/vb/string.asp
'and especially at "string length" and "extract substring"
If posFunnen = 1 Or (posFunnen > 1 And Form1.Check1.Value = Unchecked) Then texten = texten & temp & vbCrLf
End If
Wend


Hope this helps some.

/Jens
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/27/08 09:27PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month