Sign in to follow this  
Followers 0
ohgod

difference from the last state of a file

8 posts in this topic

the diff.txt should contain the difference (which is incrementing in rows) between the last state of the ex.txt and the new state of it.

Every time the script runs the ex.txt becomes bigger by some rows, i want to have a file with the new rows only, but this way, you can see below is very slow and doesn't work with big files.

Tried to read the rows into an array but always run into declaration problems.

Can anyone tell how to do this properly?

func diff()
$lExported=("c:\Ready\ex.txt")
$hdiff=FileOpen("c:\ready\diff.txt",2)
$CountLines=IniRead($config,"last","llinenumber","1")
;MsgBox(64, "", "There are " & $CountLines )
$i=0
While 1
    $line = FileReadLine($lExported,$CountLines)
     If @error = -1 Then 
    ;MsgBox(0, "Line readvege:", $countlines)
        IniWrite($config,"last","llinenumber",$CountLines)
     ExitLoop
     EndIf
    $i=$i+1
    $CountLines=$CountLines+1
    FileWriteLine($hdiff,$line)
   ;MsgBox(0, "Line read:", $line)
Wend

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

the diff.txt should contain the difference (which is incrementing in rows) between the last state of the ex.txt and the new state of it.

Every time the script runs the ex.txt becomes bigger by some rows, i want to have a file with the new rows only, but this way, you can see below is very slow and doesn't work with big files.

Tried to read the rows into an array but always run into declaration problems.

Can anyone tell how to do this properly?

func diff()
$lExported=("c:\Ready\ex.txt")
$hdiff=FileOpen("c:\ready\diff.txt",2)
$CountLines=IniRead($config,"last","llinenumber","1")
;MsgBox(64, "", "There are " & $CountLines )
$i=0
While 1
    $line = FileReadLine($lExported,$CountLines)
     If @error = -1 Then 
;MsgBox(0, "Line readvege:", $countlines)
        IniWrite($config,"last","llinenumber",$CountLines)
     ExitLoop
     EndIf
    $i=$i+1
    $CountLines=$CountLines+1
    FileWriteLine($hdiff,$line)
 ;MsgBox(0, "Line read:", $line)
Wend
Might be easier to do something like this (just an idea not tested)

;$last size is last recorded size of file
$nextsize = FileGetSize($filename)
if $nextsize > $lastsize then
  $hf =  _APIFileOpen ($filename)
  _APIFileSetPos($hf, $lastsize)
  $newstuff =  _APIFileRead($hf,$newsize - $lastsize)
  _APIFileClose ($hf)
;do something with $newstuff
 $lastsize = $newsize
endif
Edited by martin

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.

Share this post


Link to post
Share on other sites

C:\Program Files\AutoIt3\Include\APITailRW.au3 (74) : ==> "long_ptr", "int_ptr" and "short_ptr" DllCall() types have been deprecated.  Use "long*", "int*" and "short*" instead.: 
Local $AFSP_r = DllCall("kernel32.dll", "long", "SetFilePointer", "hwnd", $hFile, "long", $nPos, "long_ptr", 0, "long", $FILE_BEGIN) 
?
>Exit code: 1   Time: 1.688

#include "APITailRW.au3"
$config="c:\ready\config.ini"
$filename=("c:\Ready\ex.txt")
$diff=("c:\Ready\diffex.txt")
$newsize = FileGetSize($filename)
$lastsize=IniRead($config,"last","size","1")
if $newsize > $lastsize then
  $hf =  _FileOpenAPI ($filename)
  _FileSetPosapi($hf, $lastsize)
  $newstuff =  _FileReadAPI($hf,$newsize - $lastsize)
  _FileCloseAPI ($hf)
  $hdiff=_FileOpenAPI ($diff)
;do something with $newstuff
_FileWriteAPI($hdiff,$newstuff)
_filecloseAPI($hdiff)

Iniwrite($config,"last","size",$newsize)
endif

Escapes me.....

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Here is an example that works for me, but my TailRW.au3 is different to what you are using so you will have to change the code below.

Where I have _APIFile... you need _File...API

#include "TailRW.au3"
$config = "c:\ready\config.ini"
$filename = "ex.txt"
$f1 = FileOpen($filename, 2)
For $n = 1 To 10
    FileWriteLine($f1, "pigs have wings")
Next
FileClose($f1)
$lastsize = FileGetSize($filename)
$f1 = FileOpen($filename, 1)
For $n = 1 To 10
    FileWriteLine($f1, "elephants build nests in rhubarb trees")
Next
FileClose($f1)
$diff = 'difference.txt'
$newsize = FileGetSize($filename)
;$lastsize=FileGetSize($filename)
If $newsize > $lastsize Then
;MsgBox(0,$lastsize,$newsize)
    $hf = _APIFileOpen($filename)
    _APIFileSetPos($hf, $lastsize)
    $newstuff = _APIFileRead($hf, $newsize - $lastsize)
    ConsoleWrite($newstuff & @CRLF)
    _APIFileClose($hf)
    FileWrite($diff,$newstuff)
    
    

    IniWrite($config, "last", "size", $newsize)
EndIf
Edited by martin

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.

Share this post


Link to post
Share on other sites

I still get the same error msg maybe because I have different "APITailRW.au3" but could not find yours anywhere.

this is mine, the only one I could find:

http://www.autoitscript.com/forum/index.php?showtopic=6853&st=105&p=336438&#entry336438 ://http://www.autoitscript.com/forum/i...p;#entry336438 ://http://www.autoitscript.com/forum/i...p;#entry336438

C:\Program Files\AutoIt3\Include\APITailRW.au3 (74) : ==> "long_ptr", "int_ptr" and "short_ptr" DllCall() types have been deprecated.  Use "long*", "int*" and "short*" instead.: 
Local $AFSP_r = DllCall("kernel32.dll", "long", "SetFilePointer", "hwnd", $hFile, "long", $nPos, "long_ptr", 0, "long", $FILE_BEGIN) 
?
>Exit code: 1   Time: 1.782

Share this post


Link to post
Share on other sites

I still get the same error msg maybe because I have different "APITailRW.au3" but could not find yours anywhere.

this is mine, the only one I could find:

http://www.autoitscript.com/forum/index.php?showtopic=6853&st=105&p=336438&#entry336438 ://http://www.autoitscript.com/forum/i...p;#entry336438 ://http://www.autoitscript.com/forum/i...p;#entry336438 ://http://www.autoitscript.com/forum/i...p;#entry336438

C:\Program Files\AutoIt3\Include\APITailRW.au3 (74) : ==> "long_ptr", "int_ptr" and "short_ptr" DllCall() types have been deprecated.  Use "long*", "int*" and "short*" instead.: 
Local $AFSP_r = DllCall("kernel32.dll", "long", "SetFilePointer", "hwnd", $hFile, "long", $nPos, "long_ptr", 0, "long", $FILE_BEGIN) 
?
>Exit code: 1   Time: 1.782
Here is my example using the same udf as you have but there is 1 difference. You must replace

long_ptr

in the udf with

long*

#include "APITailRW.au3"
$config = "c:\ready\config.ini"
$filename = "ex.txt"
$f1 = FileOpen($filename, 2)
For $n = 1 To 10
    FileWriteLine($f1, "pigs have wings")
Next
FileClose($f1)
$lastsize = FileGetSize($filename)
$f1 = FileOpen($filename, 1)
For $n = 1 To 10
    FileWriteLine($f1, "elephants build nests in rhubarb trees")
Next
FileClose($f1)
$diff = 'difference.txt'
$newsize = FileGetSize($filename)
;$lastsize=FileGetSize($filename)
If $newsize > $lastsize Then
;MsgBox(0,$lastsize,$newsize)
    $hf = _FileOpenAPI($filename)
    _FileSetPosAPI($hf, $lastsize)
    $newstuff = _FileReadAPI($hf, $newsize - $lastsize)
    ConsoleWrite($newstuff & @CRLF)
    _FileCloseAPI($hf)
    FileWrite($diff,$newstuff)
    
    

    IniWrite($config, "last", "size", $newsize)
EndIf

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.

Share this post


Link to post
Share on other sites

THANK YOU!

Share this post


Link to post
Share on other sites

THANK YOU!

:)

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.

Share this post


Link to post
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
Sign in to follow this  
Followers 0