Jump to content

how to keep filesize


dickep
 Share

Recommended Posts

OK, this is more a "general" question versus AutoIt specific. :)

I am writing an application to gather data. This data is then written to a file in text form.

Now, I would like to check the file size and if it over, lets say, 5MB, then the oldest (first) record will be removed and the latest data saved.

Kind of like a tower with water being pumped in from the bottom. The information stream is pushed into the tower, new always going in at the bottom. When the tower is full - and information is still being pumped in - the oldest information spills over never to be seen again. :)

So, my question - after thinking about this for the last 3 days and searching the internet - is "How can I get this done?" :)

Well, if I have not thoroughtly confused you all, can I get some help/guidance/tutoring/etc. on a module/function that can do this?

Thanks

E

Link to comment
Share on other sites

All this is just the kind of project for AutoIt.

Get the latest beta version of AutoIt - ver 3.2.11.0 and install it.

You could include "File.au3" from the same folder and use _FileReadToArray in that file, first, then use _ArrayPush on the array, which is described this way: "Adds new values without increasing array size by inserting at the end the new value and deleting the first one or vice versa." - You might want the vis-versa - something like this:

#include "C:\Program Files\AutoIt3\Beta\Include\Array.au3"
#include "C:\Program Files\AutoIt3\Beta\Include\File.au3"
Global $myArr
$newLineOfData = InputBox("NewSquirrely", "Enter new data")
_FileReadToArray("C:\My DataFolder\File.dat", $myArr)
_ArrayPush($myArr, $newLineOfData, 1)
_FileWriteFromArray("C:\My DataFolder\File.dat", $myArr)

Das Häschen benutzt Radar

Link to comment
Share on other sites

Something like this (assuming the file is ANSI):

$sOld = FileRead($sFilename)
$sTotal = $sOld & $sNew
If StringLen($sTotal) > $iLimit Then $sTotal = StringRight($sTotal, $iLimit)
$hFile = FileOpen($sFilename, 2)
FileWrite($hFile, $sTotal)
FileClose($hFile)

And if file sizes are (very) big, using API to read/seek/write would be more efficient.

Edited by Siao

"be smart, drink your wine"

Link to comment
Share on other sites

A demo of (and slight variation* on) Siao's fine code.

*To trim the first line completely.

Run the demo below and then remark out...

$sTotal = StringTrimLeft($sTotal, (StringInStr($sTotal, @CRLF, 2) + 1))

...and run it again to see what I mean.

; create a file for demo
$sFilename = "testthis.txt"
FileDelete($sFilename)
$FileHandle = FileOpen($sFilename, 1)

Dim $iLimit = 5000, $cnt
Do
    $cnt += 1
    FileWriteLine($FileHandle, $cnt & " .......some text here......")
    $Sz = FileGetSize($sFilename)
    TrayTip($cnt, "Waiting for a size of " & $iLimit & @CR & "Current File Size ~" & $Sz, 100)
Until $Sz > $iLimit
FileClose($FileHandle)

; add new text and demo trim
$sOld = FileRead($sFilename)
$sTotal = $sOld & "anything ###just to show some extra text added####"

If StringLen($sTotal) > $iLimit Then
    $sTotal = StringRight($sTotal, $iLimit)
    $sTotal = StringTrimLeft($sTotal, (StringInStr($sTotal, @CRLF, 2) + 1))
EndIf
$hFile = FileOpen($sFilename, 2)
FileWrite($hFile, $sTotal)
FileClose($hFile)

MsgBox(0, "New File Size", FileGetSize($sFilename))
Run("notepad " & $sFilename)

Edit: corrected the code that the forum HTML barfed...

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

A demo of (and slight variation* on) Siao's fine code.

*To trim the first line completely.

Run the demo below and then remark out...

$sTotal = StringTrimLeft($sTotal, (StringInStr($sTotal, @CRLF, 2) + 1))

...and run it again to see what I mean.

; create a file for demo
$sFilename = "testthis.txt"
FileDelete($sFilename)
$FileHandle = FileOpen($sFilename, 1)

Dim $iLimit = 5000, $cnt
Do
    $cnt += 1
    FileWriteLine($FileHandle, $cnt & " .......some text here......")
    $Sz = FileGetSize($sFilename)
    TrayTip($cnt, "Waiting for a size of " & $iLimit & @CR & "Current File Size ~" & $Sz, 100)
Until $Sz > $iLimit
FileClose($FileHandle)

; add new text and demo trim
$sOld = FileRead($sFilename)
$sTotal = $sOld & "[url=""]\\\\\some[/url] new text/////"

If StringLen($sTotal) > $iLimit Then
    $sTotal = StringRight($sTotal, $iLimit)
    $sTotal = StringTrimLeft($sTotal, (StringInStr($sTotal, @CRLF, 2) + 1))
EndIf
$hFile = FileOpen($sFilename, 2)
FileWrite($hFile, $sTotal)
FileClose($hFile)

MsgBox(0, "New File Size", FileGetSize($sFilename))
Run("notepad " & $sFilename)
Thank you but I have a question on the "$sTotal" line. I copied this to my system and tried running it. But get an error message about an unterminated string.

So, if you could explain what you were trying, I can get a better idea of how the sample can help me.

Thanks

E

Link to comment
Share on other sites

Thank you but I have a question on the "$sTotal" line. I copied this to my system and tried running it. But get an error message about an unterminated string.

So, if you could explain what you were trying, I can get a better idea of how the sample can help me.

Thanks

E

Just change the line biginning

$sTotal = $sOld & ...

to

$sTotal = $sOld & "anything ###just to show some extra text added####"

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...