View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript,microsoft.public.scripting.wsh
\Crash\ Dummy \Crash\ Dummy is offline
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