Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text File Manipulation in VBA 1

Status
Not open for further replies.
May 29, 2003
73
US
Hi All
How would I delete first two rows of the text file and save text file with other name. For example, I have a text file test1.txt located in C:\Temp\ with below text

Cusip Description1 Net
Return

12533 Fund 1 .0526
12453 Fund 2 .0587

I know the code for reading and writing to text file, but I dont know the code how to delete a row from a text file and saving it. As you can see from above text example, the first two line of the are not delimited corrected, but the data section is space delimited.

Thanks in advance!
 
Open the data file (#1)
Open a second file (#2)
Read a line from #1

While not at the end of file #1
If the line has data
Write the line to #2
Read the next line from #1
End If
Loop

Close #1 and #2
Replace #2 with #1

 
Thanks Joseph!
But how do you delete rows (first two rows) from the source file and save it to the destination file? Do you have sample code for that? Or how do you accomplish the same thing?

 
Here's a little sample using filesystemobject, don't think you can delete a line in a file, but just don't append them to the next...:

[tt]dim fs as scripting.filesystemobject
dim txtInn as scripting.textstream
dim txtOut as scripting.textstream
dim lCount as long
dim sTmp as string
set fs=new scripting.filesystemobject
set txtInn=fs.opentextfile("C:\Temp\test1.txt",forreading)
set txtInn=fs.createtextfile("C:\Temp\test2.txt",true)
do while not txtInn.atendofstream
sTmp=txtInn.readline
lCount=lCount+1
if lCount>2 then
txtOut.writeline sTmp
end if
loop
txtInn.close
txtOut.close
set txtInn=nothing
set txtOut=nothing[/tt]

- typed not tested, needs a reference to Microsoft Scripting Runtime

Roy-Vidar
 
Thanks ROY!
Your solution is what I needed for my code. It works perfectly :eek:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top