Sign in to follow this  
Followers 0
gil900

need to read only the last 1000 characters of a file.

8 posts in this topic

#1 ·  Posted (edited)

Hello,

I nead help.

 

this command:

FileRead("database.txt",1000)

can read the first 1000 characters of the file.

but i need to read the only last 1000 characters and i don't see this option in the function.

i know that i can read the whole file and then use

StringRight($fileread,1000)

but in this way the script will load a lot of unnecessary data to the memory so it will take more memory and it also will take more time to read the file..

 

i need to read directly the last 1000 characters..

how can i do this?

thanks for helpers!

Edited by gil900

Share this post


Link to post
Share on other sites



I think there is a way to do this using winapi functions. You cannot achieve this result with the normal fileread function.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

try this:

$hFile = FileOpen("file.txt")
FileSetPos($hFile,-1000,$FILE_END)
$string = FileRead($hFile,1000)
ConsoleWrite(StringLen($string) & " " & $string & @CRLF)





I created a dummy file, with multi thousands of chars...all asdf...at the end, i put in string=last

the above outputs last (as the last chars) and count of 1000

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I think there is a way to do this using winapi functions. You cannot achieve this result with the normal fileread function.

 

Where?

I looked in the UDF and I have not found.

 

 

try this:

$hFile = FileOpen("file.txt")

FileSetPos($hFile,-1000,$FILE_END)

$string = FileRead($hFile,1000)

ConsoleWrite(StringLen($string) & " " & $string & @CRLF)

 

 

From looking at your code, it does not matter what you do later ..

At first you read the whole file with FileOpen("file.txt") and that's exactly what I do not want to do.

EDIT:

Oops, I did not see properly before.

I'll check your code

Edited by gil900

Share this post


Link to post
Share on other sites

Well, it cuts out the memory usage to hold all that data from your FileRead string, at least.

The string from mine will limit to only 1k chars.


IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

Gil900,

FileOpen doesn't read he file!


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Gil900,

FileOpen doesn't read he file!

I know.

I edited my response.

I just didn't saw good .. (I thought I saw FileRead instead FileOpen)

jdelaney, thank you.

I checked your code and it does exactly what I want and it works!

Thanks

Edited by gil900

Share this post


Link to post
Share on other sites

try this:

$hFile = FileOpen("file.txt")
FileSetPos($hFile,-1000,$FILE_END)
$string = FileRead($hFile,1000)
ConsoleWrite(StringLen($string) & " " & $string & @CRLF)





I created a dummy file, with multi thousands of chars...all asdf...at the end, i put in string=last

the above outputs last (as the last chars) and count of 1000

Oh, well, I guess I didn't know what I was talking about in this case. Never seen the filesetpos() function used before.

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