PDA

View Full Version : Solved: vb.6 code to read and edit particular line in notepad



shamsam1
11-28-2008, 05:34 AM
i want to read line no 5 form notepad and edit it
looking for vb.6 code

fetching value from notepad end editing it

Oorang
11-29-2008, 02:36 PM
Note: Make sure you set the reference (or it won't work).

Option Explicit
Public Sub Example()
MsgBox GetLineFromFile("C:\Test\MyFile.txt", 5)
End Sub
Public Function GetLineFromFile(ByVal filePath As String, ByVal line As Long) As String
'For these variables to work you must set a reference to scrrun.dll:
Dim fso As Scripting.FileSystemObject
Dim ts As Scripting.TextStream
Dim lngIndx As Long
On Error GoTo Err_Hnd
Set fso = New Scripting.FileSystemObject
Set ts = fso.OpenTextFile(filePath, ForReading, False)
For lngIndx = 1 To line - 1
ts.SkipLine
Next
Exit_Proc:
On Error Resume Next
GetLineFromFile = ts.ReadLine
ts.Close
Set ts = Nothing
Set fso = Nothing
Exit Function
Err_Hnd:
MsgBox Err.Description
Resume Exit_Proc
End Function

Bob Phillips
11-30-2008, 12:29 PM
Basic was invented in the days of text files, it comes naturally



Const FileName As String = "c:\test\test.txt"
Dim FileNumber As Long
Dim myData As String
Dim counter As Long

FileNumber = FreeFile ' Get unused file
' number.
Open FileName For Input As #FileNumber

Do While Not EOF(FileNumber) And counter < 5

Line Input #FileNumber, myData
counter = counter + 1
Loop
If counter = 5 Then MsgBox myData
Close #FileNumber

shamsam1
11-30-2008, 11:24 PM
Hello OOrang

The code is capturing the required line from the notepad. I want to replace the selected line (marked in red colour) with new line from vb6.
(defun c:dwin () (c:DWGINSERT)) ; Program Shortcut
(defun c:DWGINSERT (/ dwglist oldlay pt1)
(setq dwglist ("C:\\13027167-GX2.dwg";
))
(setvar "cmdecho" 0)
(setq oldlay (getvar "clayer"))
(if (not (tblsearch "LAYER" "DWGS"))
(command "-layer" "m" "DWGS" "C" "1" "DWGS" ""))
(foreach dwg dwglist
(if (and
(setq pt1 (getpoint "\nSpecify Insertion Point for Dwg."))
(vl-cmdf "-insert" dwg pt1 "" "" ""))
(princ (strcat "\n" dwg " Inserted."))
(alert "\nDrawing Not Found.")
) ; end if
) ; end foreach
(setvar "cmdecho" 1 )
(setvar "clayer" oldlay)
(princ)
)


Help me with the vb code.
Regards
Sham

shamsam1
12-01-2008, 01:47 AM
i am able to replace
Public Sub Example()
Text1.Text = GetLineFromFile("C:\lisp copy.lsp", 3)
End Sub
Public Function GetLineFromFile(ByVal filePath As String, ByVal line As Long) As String
'For these variables to work you must set a reference to scrrun.dll:
Dim fso As Scripting.FileSystemObject
Dim ts As Scripting.TextStream
Dim lngIndx As Long
On Error GoTo Err_Hnd
Set fso = New Scripting.FileSystemObject
Set ts = fso.OpenTextFile(filePath, ForReading, False)
For lngIndx = 1 To line - 1
ts.SkipLine
Next
Exit_Proc:
On Error Resume Next
GetLineFromFile = ts.ReadLine
ts.Close
Set ts = Nothing
Set fso = Nothing
Exit Function
Err_Hnd:
MsgBox Err.Description
Resume Exit_Proc
End Function

Private Sub Command1_Click()
Call SaveXMLInfo("(setq dwglist '(" & """" & App.Path & "\dwg\12-GX2.dwg"")")
End Sub
Private Sub SaveXMLInfo(strID As String)
Dim strOldXML As String, strLine As String, strNewXML As String
Dim lngFree As Long
strOldXML = "C:\lisp copy.lsp"
lngFree = FreeFile
Dim mystring As String
'Load the file into a temporary textbox (should be hidden to make the function faster)
Open strOldXML For Input As #lngFree
Do While Not EOF(lngFree)
Line Input #lngFree, strLine
mystring = mystring & strLine & vbCrLf
Loop
DoEvents
Close #lngFree
'Replace the needed text
mystring = Replace(mystring, Text1.Text, strID)
'Save the new file
strNewXML = "C:\lisp copy.lsp"
lngFree = FreeFile
Open strNewXML For Output As #lngFree
Print #lngFree, mystring
Close #lngFree
End Sub

Oorang
12-01-2008, 09:02 AM
xld Thanks for the example, I usually only use open with Get/Put nice to see an Input example.

dhananjay
12-15-2008, 07:02 AM
Basic was invented in the days of text files, it comes naturally



Const FileName As String = "c:\test\test.txt"
Dim FileNumber As Long
Dim myData As String
Dim counter As Long

FileNumber = FreeFile ' Get unused file
' number.
Open FileName For Input As #FileNumber

Do While Not EOF(FileNumber) And counter < 5

Line Input #FileNumber, myData
counter = counter + 1
Loop
If counter = 5 Then MsgBox myData
Close #FileNumber



I am a newbie... and I am trying to edit a notepad using vba..

I ried to run the above the code and got a 'file not found error'.. May be it is because I could not grasp the concept completely...please help me..

Oorang
12-15-2008, 12:41 PM
rofl... You need to update the path:)

dhananjay
12-15-2008, 07:52 PM
rofl... You need to update the path

I had tried only after changing path mate!!!

but i posted the same code in the forum so that it will be easy for 'xld ' to explain the concept...

Here is the actual code i tried....

Sub test()
Const FileName As String = "D:\dhananjayan\zimmer\test\trail_test.txt"
Dim FileNumber As Long
Dim myData As String
Dim counter As Long

FileNumber = FreeFile ' Get unused file
' number.
Open FileName For Input As #FileNumber

Do While Not EOF(FileNumber) And counter < 5

Line Input #FileNumber, myData
counter = counter + 1
Loop
If counter = 5 Then MsgBox myData
Close #FileNumber
End Sub


Note: trail_test.txt is a proE(cad tool that i am automating) generated test file in proE versioned format...

please help..

here is the sample of what i need to do...

But please give me sample code with funtions for

1. open a txt file.
2.Delete contents
3.write new lines
4. save and close

Eg.
input: test.txt
vbax
is
a
great forum
Output: test.txt
I
am
learning
a lot
here

dhananjay
12-15-2008, 09:43 PM
Hi friends!!! I have got the concept..

But the saved txt file is not in proE version file.. :cry:

Is there a funtion to save the file in same format as that it is being opened...Please:help .. i am racing against time....

Bob Phillips
12-16-2008, 08:18 AM
Is this the sort of thing you were after?



Sub WriteUTF8()
Const inFileName As String = "C:\test\test.txt" '"D:\dhananjayan\zimmer\test\trail_test.txt"
Const outFileName As String = "C:\test\test2.txt"

With CreateObject("ADODB.Stream")
.Open
.LoadFromFile inFileName
.Type = 2
.Charset = "UTF-8"
.ReadText 'read past all existing data
.Writetext "Here is another line" & vbCrLf
.SaveToFile outFileName, 2 ' adSaveCreateOverWrite
.Close
End With
End Sub

dhananjay
12-17-2008, 07:37 AM
Thanks a lot Xld!! got it working with minor modification....

Oorang
12-17-2008, 08:42 AM
Wow XLD, I use ADO all the time and never once noticed that it had a stream object. I have a strong desire to go back through old databases and replace procedures using the Scripting Runtime.

dhananjay - Just as an additional piece of info. You can make xld's method work with intellisense (early binding) by setting a reference to "Microsoft Active X 2.8 Data Objects Library" (C:\Program Files\Common Files\System\msado15.dll). The early bound version would look like this:
Sub WriteUTF8()
Const inFileName As String = "C:\test\test.txt" '"D:\dhananjayan\zimmer\test\trail_test.txt"
Const outFileName As String = "C:\test\test2.txt"
Dim asMyFile As ADODB.Stream
Set asMyFile = New ADODB.Stream
With asMyFile
.Open
.LoadFromFile inFileName
.Type = 2
.Charset = "UTF-8"
.ReadText 'read past all existing data
.WriteText "Here is another line" & vbCrLf
.SaveToFile outFileName, 2 ' adSaveCreateOverWrite
.Close
End With
End Sub

It's not necessary at all to use early binding, it's just my personal preference to have intellisense.

Bob Phillips
12-18-2008, 09:28 AM
Wow XLD, I use ADO all the time and never once noticed that it had a stream object. I have a strong desire to go back through old databases and replace procedures using the Scripting Runtime.

Can you tell that I have to work a lot with text files :)