Does anyone know the how to DELETE a line of strings in a
txt file. And do you use the OUTPUT mode or APPEND mode.
Thanyou heaps!!!
Printable View
Does anyone know the how to DELETE a line of strings in a
txt file. And do you use the OUTPUT mode or APPEND mode.
Thanyou heaps!!!
don't have time to test this...off the top of
the block..give it a try..I'll check the q & a
when I get to work..gotta go...bye.
I would read this into a list box just for show
until you know it's doing what you want.
then you would open your file and load the array
back into it using output as that would
overwrite the original file.
'the results are what you want.
Option Explicit
Option Compare Text
Dim filesys, txtStream As Object
Private Sub Form_Load()
Dim myArr() As Variant
Dim i As Integer
Dim myInput, myVar as string
Set filesys = CreateObject("Scripting.FileSystemObject")
Set txtStream = filesys.openTextFile("C:\my documents\x.txt")
myInput = inputbox("Enter the line your are searching for."),"Search File For"
Do Until txtStream.atendofstream
myVar = txtStream.readline
myVar = Trim(myArr(i))
If myVar <> myInput
Then
i = i + 1
ReDim Preserve myArr(1 To i) As Variant
myArr(i) = myVar
End If
Loop
for i = 1 to i
list1.additem myArr(i)
next
'leave this commented out till you are happy it works..
'here you should have all lines except the one you don't want
'open "yourfilepath\yourfile.ext" for output as #1
'for i = 1 to i
'write #1, myArr(i)
'next
'Close #1
End Sub
To do that, you don't use APPEND. APPEND is for add line at the end of the file.
To delete a line in a file, you must to make a temporary where you copy all the lines, but not these you want to delete.
You kill the old file and you rename de temporary file with the good name.
It's hard but I don't kown another way to make it.
No, i don't know an easy way either.
Open the file for binary.
Read the file into a byte array using "Get"
Close the file
Convert the byte array to a string usign "StrConv()"
Delete the unwanted bit from the string.
Open the file for output, (this automatically deletes the content)
Write the string back to the file using Print #1, text
Open the file in binary
set lof spaces in a string and read it with get
close the file
a)
use instr to search for vbcrlf and cut of the unwanted line using mid
b) split it into array of strings, remove the unvanted string, join them
open in output to erase content
and the put it back into the file in binary
Thanks for helping me out, I've now got an idea for what I have to do now!
Delete lines in a textbox:
Code:Option Explicit
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal
nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA"
(ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As
Long, ByVal lpTempFileName As String) As Long
Private Sub CmdDeleteLine_Click()
Const MAX_PATH = 260
Const NAME_LEN = MAX_PATH + 80
Dim inname As String
Dim strlen As Integer
Dim outpath As String
Dim outname As String
Dim infile As Integer
Dim outfile As Integer
Dim one_line As String
Dim target As String
Dim deleted As Integer
On Error GoTo DeleteLineError
' Open the input file.
inname = FileText.Text
infile = FreeFile
Open inname For Input As infile
' Open the output file.
outpath = Space$(NAME_LEN)
strlen = GetTempPath(NAME_LEN, outpath)
If strlen = 0 Then
MsgBox "Error getting temporary file path."
Exit Sub
Else
outpath = Left$(outpath, strlen)
End If
outname = Space$(NAME_LEN)
If GetTempFileName(outpath, "tmp", _
0, outname) = 0 _
Then
MsgBox "Error getting temporary file name."
Exit Sub
End If
strlen = InStr(outname, vbNullChar) - 1
If strlen > 0 Then _
outname = Left$(outname, strlen)
outfile = FreeFile
Open outname For Output As outfile
MousePointer = vbHourglass
DoEvents
' Copy the file skipping lines containing the
' target.
deleted = 0
target = TargetText.Text
Do While Not EOF(infile)
Line Input #infile, one_line
If InStr(one_line, target) = 0 Then
Print #outfile, one_line
Else
deleted = deleted + 1
End If
Loop
' Close the files.
Close infile
Close outfile
' Delete the original file.
Kill inname
' Give the new file the old name.
Name outname As inname
MsgBox Format$(deleted) & " lines deleted."
DeleteLineError:
MousePointer = vbDefault
Exit Sub
End Sub