Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript,microsoft.public.scripting.wsh
external usenet poster
 
Posts: 8
Default How to delete rows in a csv file?

I have a vbscript that appends data to a csv file on a daily basis. When new
data gets appended to the bottom rows of the file, I'd like the oldest data
in the top rows to be deleted. Does anyone know how I can delete the top 3
rows every time the file gets appended? Here's what I've got so far:

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set fsoOut = CreateObject("scripting.filesystemobject")
Set outFile = fsoOut.OpenTextFile("MyFile.csv", ForAppending, True)

<<<Here's where I want to delete rows 1-3 in the csv file

outFile.writeline (Now & "," & DataValue)
outFile.close


Thanks for any help on this...


  #2   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript,microsoft.public.scripting.wsh
external usenet poster
 
Posts: 923
Default How to delete rows in a csv file?

If it is the top three rows in the Excel worksheet after you have appended
new data then use.....

Rows("1:3").Entirerow.delete

But I suspect that may not be a full answer?
--
Cheers
Nigel



"D.P. Roberts" wrote in message
...
I have a vbscript that appends data to a csv file on a daily basis. When
new data gets appended to the bottom rows of the file, I'd like the oldest
data in the top rows to be deleted. Does anyone know how I can delete the
top 3 rows every time the file gets appended? Here's what I've got so far:

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set fsoOut = CreateObject("scripting.filesystemobject")
Set outFile = fsoOut.OpenTextFile("MyFile.csv", ForAppending, True)

<<<Here's where I want to delete rows 1-3 in the csv file

outFile.writeline (Now & "," & DataValue)
outFile.close


Thanks for any help on this...



  #3   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript,microsoft.public.scripting.wsh
external usenet poster
 
Posts: 3
Default How to delete rows in a csv file?

you cant "delete" rows from a text file. youll have to open a new, temp
file, read the original file, write the lines to the temp file (skipping the
ones you dont want) then delete the old, rename the new



"D.P. Roberts" wrote in message
...
I have a vbscript that appends data to a csv file on a daily basis. When
new data gets appended to the bottom rows of the file, I'd like the oldest
data in the top rows to be deleted. Does anyone know how I can delete the
top 3 rows every time the file gets appended? Here's what I've got so far:

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set fsoOut = CreateObject("scripting.filesystemobject")
Set outFile = fsoOut.OpenTextFile("MyFile.csv", ForAppending, True)

<<<Here's where I want to delete rows 1-3 in the csv file

outFile.writeline (Now & "," & DataValue)
outFile.close


Thanks for any help on this...



  #4   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript,microsoft.public.scripting.wsh
external usenet poster
 
Posts: 1
Default How to delete rows in a csv file?

I have a vbscript that appends data to a csv file on a daily basis. When new
data gets appended to the bottom rows of the file, I'd like the oldest data
in the top rows to be deleted. Does anyone know how I can delete the top 3
rows every time the file gets appended? Here's what I've got so far:


Unfortunately, you can not read/write with random access to a file from
VBScript. You can, however, edit the contents of one file while copying it to a
second file, or while the complete file is in memory. As usual, there are
several ways to do it.How exactly you do it depends on how big the file is and
on personal preference. Here is how I might do it, using the example code you
posted:

'your code
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set fsoOut = CreateObject("scripting.filesystemobject")
Set outFile = fsoOut.OpenTextFile("MyFile.csv", ForAppending, True)
outFile.writeline (Now & "," & DataValue)
outFile.close

'added code
Set inFile= fsoOut.OpenTextFile("MyFile.csv", ForReading, True)
Set tempFile=fso.CreateTextFile("MyFile.tmp")

for n=0 to 2:inFile.skipLine:next

do until inFile.AtEndOfStream
line=inFile.readLine
tempFile.writeLine line
loop

inFile.close
tempFile.close

fsoOut.deleteFile "MyFile.csv"
fsoOut.moveFile "MyFile.tmp", "MyFile.csv"
--
Crash



  #5   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript,microsoft.public.scripting.wsh
external usenet poster
 
Posts: 8
Default How to delete rows in a csv file?

Why can't I simply open the csv files as an Excel application, delete the
rows, close the file, then open it again as a text file for appending?

I tried this but I think the syntax is wrong:

Dim XL, XLBook
Set XL = CreateObject("Excel.application")
Set XLBook = XL.Workbooks.Open("MyFile.csv")
Rows("1:3").Entirerow.delete <<<Type mismatch error for "Rows" happens
on this line



"D.P. Roberts" wrote in message
...
I have a vbscript that appends data to a csv file on a daily basis. When
new data gets appended to the bottom rows of the file, I'd like the oldest
data in the top rows to be deleted. Does anyone know how I can delete the
top 3 rows every time the file gets appended? Here's what I've got so far:

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set fsoOut = CreateObject("scripting.filesystemobject")
Set outFile = fsoOut.OpenTextFile("MyFile.csv", ForAppending, True)

<<<Here's where I want to delete rows 1-3 in the csv file

outFile.writeline (Now & "," & DataValue)
outFile.close


Thanks for any help on this...






  #6   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript,microsoft.public.scripting.wsh
external usenet poster
 
Posts: 3
Default How to delete rows in a csv file?

thats a different story.

did you try XLBook.Rows?

i dont think "Rows" means anything to anyone


"D.P. Roberts" wrote in message
...
Why can't I simply open the csv files as an Excel application, delete the
rows, close the file, then open it again as a text file for appending?

I tried this but I think the syntax is wrong:

Dim XL, XLBook
Set XL = CreateObject("Excel.application")
Set XLBook = XL.Workbooks.Open("MyFile.csv")
Rows("1:3").Entirerow.delete <<<Type mismatch error for "Rows" happens
on this line



"D.P. Roberts" wrote in message
...
I have a vbscript that appends data to a csv file on a daily basis. When
new data gets appended to the bottom rows of the file, I'd like the oldest
data in the top rows to be deleted. Does anyone know how I can delete the
top 3 rows every time the file gets appended? Here's what I've got so far:

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set fsoOut = CreateObject("scripting.filesystemobject")
Set outFile = fsoOut.OpenTextFile("MyFile.csv", ForAppending, True)

<<<Here's where I want to delete rows 1-3 in the csv file

outFile.writeline (Now & "," & DataValue)
outFile.close


Thanks for any help on this...






  #7   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript,microsoft.public.scripting.wsh
external usenet poster
 
Posts: 3
Default How to delete rows in a csv file?

and while youre at it, why bother appending as a text file? just use the
excel object to append to it while its open



"D.P. Roberts" wrote in message
...
Why can't I simply open the csv files as an Excel application, delete the
rows, close the file, then open it again as a text file for appending?

I tried this but I think the syntax is wrong:

Dim XL, XLBook
Set XL = CreateObject("Excel.application")
Set XLBook = XL.Workbooks.Open("MyFile.csv")
Rows("1:3").Entirerow.delete <<<Type mismatch error for "Rows" happens
on this line



"D.P. Roberts" wrote in message
...
I have a vbscript that appends data to a csv file on a daily basis. When
new data gets appended to the bottom rows of the file, I'd like the oldest
data in the top rows to be deleted. Does anyone know how I can delete the
top 3 rows every time the file gets appended? Here's what I've got so far:

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set fsoOut = CreateObject("scripting.filesystemobject")
Set outFile = fsoOut.OpenTextFile("MyFile.csv", ForAppending, True)

<<<Here's where I want to delete rows 1-3 in the csv file

outFile.writeline (Now & "," & DataValue)
outFile.close


Thanks for any help on this...






  #8   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript,microsoft.public.scripting.wsh
external usenet poster
 
Posts: 8
Default How to delete rows in a csv file?

Yes, I treid XLBook.Rows with no luck.

"Joe Reynolds" wrote in message
...
thats a different story.

did you try XLBook.Rows?

i dont think "Rows" means anything to anyone


"D.P. Roberts" wrote in message
...
Why can't I simply open the csv files as an Excel application, delete the
rows, close the file, then open it again as a text file for appending?

I tried this but I think the syntax is wrong:

Dim XL, XLBook
Set XL = CreateObject("Excel.application")
Set XLBook = XL.Workbooks.Open("MyFile.csv")
Rows("1:3").Entirerow.delete <<<Type mismatch error for "Rows"
happens on this line



"D.P. Roberts" wrote in message
...
I have a vbscript that appends data to a csv file on a daily basis. When
new data gets appended to the bottom rows of the file, I'd like the
oldest data in the top rows to be deleted. Does anyone know how I can
delete the top 3 rows every time the file gets appended? Here's what I've
got so far:

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set fsoOut = CreateObject("scripting.filesystemobject")
Set outFile = fsoOut.OpenTextFile("MyFile.csv", ForAppending, True)

<<<Here's where I want to delete rows 1-3 in the csv file

outFile.writeline (Now & "," & DataValue)
outFile.close


Thanks for any help on this...








  #9   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript,microsoft.public.scripting.wsh
external usenet poster
 
Posts: 8
Default How to delete rows in a csv file?

I got it working using Crash's approach. Thanks!

""Crash" Dummy" wrote in message
...
I have a vbscript that appends data to a csv file on a daily basis. When
new
data gets appended to the bottom rows of the file, I'd like the oldest
data
in the top rows to be deleted. Does anyone know how I can delete the top
3
rows every time the file gets appended? Here's what I've got so far:


Unfortunately, you can not read/write with random access to a file from
VBScript. You can, however, edit the contents of one file while copying it
to a
second file, or while the complete file is in memory. As usual, there are
several ways to do it.How exactly you do it depends on how big the file is
and
on personal preference. Here is how I might do it, using the example code
you
posted:

'your code
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set fsoOut = CreateObject("scripting.filesystemobject")
Set outFile = fsoOut.OpenTextFile("MyFile.csv", ForAppending, True)
outFile.writeline (Now & "," & DataValue)
outFile.close

'added code
Set inFile= fsoOut.OpenTextFile("MyFile.csv", ForReading, True)
Set tempFile=fso.CreateTextFile("MyFile.tmp")

for n=0 to 2:inFile.skipLine:next

do until inFile.AtEndOfStream
line=inFile.readLine
tempFile.writeLine line
loop

inFile.close
tempFile.close

fsoOut.deleteFile "MyFile.csv"
fsoOut.moveFile "MyFile.tmp", "MyFile.csv"
--
Crash





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to delete blank rows after file save pm Excel Discussion (Misc queries) 2 April 15th 10 11:58 PM
How to delete alternating BLANK rows in .xls file all @ once? Desperate Excel Discussion (Misc queries) 3 October 27th 09 08:34 PM
How delete to end of file; comp keeps adding rows; over 3000! whalesong Excel Worksheet Functions 1 September 8th 08 11:30 PM
Delete some rows at the end of the file Dan Excel Programming 1 September 20th 04 01:21 PM
Delete rows in Excel file with macros Krassimir Excel Programming 0 October 1st 03 01:04 PM


All times are GMT +1. The time now is 12:46 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"