Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
ReplaceLine
Author:
MacLeod Broad
Submitted:
1/15/2008
Version:
VB6
Compatibility:
VB6
Category:
File Manipulation
Views:
9029
Replaces a single line of a file. Perfect for making changes to ini files, cfg files, text files, etc... If the file is empty it returns Error 62 and if the line number you want to replace is out of bounds then it returns Error 32202. Usage example: Call ReplaceLine("C:\test.txt", 58, "I am Line 58")
Declarations:
'none
Code:
Public Function ReplaceLine(filePath As String, lineNumber As Integer, replacementText As String) Dim Str As String Dim newFileContent As String Dim i As Integer lineNumber = lineNumber - 1 i = 1 Open filePath For Input As #1 Input #1, Str newFileContent = Str Do While Not EOF(1) If i = lineNumber Then Input #1, Str Str = replacementText newFileContent = newFileContent + vbNewLine + Str Else: Input #1, Str newFileContent = newFileContent + vbNewLine + Str End If i = i + 1 Loop Close #1 If i < lineNumber Then Error (32202) Open filePath For Output As #1 Print #1, newFileContent Close #1 End Function
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2024 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement