Remove, replace, or insert content in VB to a line in the text
And analyze the processing examples of the text
VB Operates text files Many ways, the following example is what I do for my own project or answer netizens, I am very representative, I hope to give you some inspiration.
'Function: Remove and replace text in a row, or insert a row into the text content' Author: soho_andy (ice) 'Parameters:' strSourceFile complete original file name 'strTargetFile generate the complete name of the new file' intRow operation of the number of rows
Sub file operation line (strSourceFile As String, strTargetFile As String, intRow As Long) Dim filenum As Integer Dim fileContents As String Dim fileInfo () As String Dim i As Integer Dim j As Integer filenum = FreeFile Open strSourceFile For Binary As #filenum FileContents = Space (Lof (filenum)) get #filenum,, FileContents Close Filenum fileinfo = split (filecontents, vbcrlf) 'Remove the number of source files, according to the return line, separated into an array filenum = FreeFile if Dir (StrtargetFile, VBNORMAL) <> "" "THEN KILLSTARGETFILE END IF DIM FILESTR () AS String 'Deleting a Billy Code Block Open StrtargetFile for Append AS #Filenum' Cycling Each Row for i = 0 To Ubound (FileInfo) - 1 IF i <> Introw - 1 Then Print #filenum, fileinfo (i) end if next closed #filenum 'replaces a line code block open startrtargetfile for append as #filenum' loop Each line for i = 0 to Ubound (fileInfo) - 1 if i = introw - 1 "End if next closed #filenum 'inserted into a row code block Open StrtargetFile for i = 0 to Ubound (fileInfo) - 1 if i = = intertrow - 1 Then Print #filenum, "You want to insert the content" Print #filenum, fileinfo (i) 'reserved the original line, the location is moved to an end if next closed #filenum msgbox "End Sub
'Another example of an example of actual problem' '' netizen's request 'has file a.txt, where two lines of data are stored, and the data is separated by commas, and now you have to read the data of the odd position of the first line to another. The first line of the text file (B.TXT), similarly, writing data of the odd position of the second row to the second line. ', For example:' file a.txt as follows: '1, 2, 3, 4, 5 '6, 7, 8, 9, 10' Operation is completed, the file B.TXT should be '1, 3, 5'6, 8,10 'author: soho_andy (ice)' parameters: 'strSourceFile complete original file name' strTargetFile generate a new file full name
Sub extracting odd bit data (strSourceFile As String, strTargetFile As String) Dim filenum As Integer Dim fileContents As String Dim fileInfo () As String Dim i As Integer Dim j As Integer Dim tmpDemData As String filenum = FreeFile Open strSourceFile For Binary As #filenum FileContents = Space (Lof (filenum)) get #filenum,, filecontents close filenum fileinfo = split (filecontents, vbcrlf) 'Remove the number of rows of source files, separate into array filenum = freefile tmpdata = "" "if Dir (IF DIR) Strtargetfile, VBNORMAL) <> "" THEN KILL STRTARGETFILE END IF DIM FILESTR () AS String
Open strtargetfile for append as #filenum 'cycle Each row for i = 0 to Ubound (fileInfo) - 1 filestr = split (Trim (FileInfo (i)), ",")' Separate data TMPDEMDATA = "" For j = 0 to ubound (filestr) 'Judging whether it is an odd bit IF (J MOD 2) = 0 THEN TMPDEMDATA = TmpData & FileStr (j) elseif J <> 0 and j <> ubound (filestr) TmpData = TMPDEMDATA & TMPDATA & "," End if next "Saves a line such as the target file Print #Filenum, TmpData next close #filenum msgbox" End SubPrivate Sub Command1_Click () extracts odd-digit data "D: /AA.TXT", "D: / BB. TXT "End Sub