AutoHotkey Community

It is currently May 25th, 2012, 7:07 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: File Standard Library
PostPosted: May 29th, 2007, 12:20 pm 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
It'll read a (binary) internet file or memory content, then write them to a file on the local HDD.
Also read a (binary) file on the local HDD and write to the memory/variable.

PS. Although there is a function InternetWriteFile(), it's not enough to upload a local file to the Web. So, please don't try it.

DOWNLOAD FileHelper.ahk or StandardLibrary File.ahk.


Last edited by Sean on October 6th, 2007, 5:52 am, edited 5 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 29th, 2007, 12:29 pm 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
FTP download with passive mode example:

PS. I'll remove the actual address tomorrow.

Code:
#Include FileHelper.ahk

hInet := InternetOpen()
hUrl  := InternetOpenUrl(hInet, "ftp://ftp.xxx.com/install.exe", 0x08000000)    ; INTERNET_FLAG_PASSIVE
hFile := CreateFile("C:\install.exe", 1)

If (hUrl = -1) || (hFile = -1)
   ExitApp

VarSetCapacity(buffer, 1024)
Loop
{
   If !nSize := InternetReadFile(hUrl, &buffer, 1024)
       Break
   WriteFile(hFile, &buffer, nSize)
   Sleep, 10
}

CloseHandle(hFile)
InternetCloseHandle(hUrl)
InternetCloseHandle(hInet)


Last edited by Sean on May 30th, 2007, 7:10 am, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 29th, 2007, 12:57 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
I am impressed how you choose your names.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 29th, 2007, 1:53 pm 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
You like to go straight to the heart of stuff, unencumbered by most error checking...

Well, I am not too fond of the choice of the first two names: why ReadMemory writes to a file? It seems a bit strange. WriteMemory indeed put data in memory, but the name doesn't tell it comes from a file.

Although it is unlikely to change, I am not fond of using boolean * 2 to create 0 or 2.
I am not sure that the SetEndOfFile is necessary in ReadMemory, but I admit I didn't knew the function so I am happy to discover it, it can be useful to truncate or extend a file (along with SetFileValidData).
In WriteMemory, the VarSetCapacity(sBuffer, -1) is useful if indeed the data read is textual, but isn't there a risk if it is binary? Mmm, probably not, if the buffer isn't shrunk.

That's lot of criticism, but your code is of course very useful, thanks for sharing.

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 29th, 2007, 2:19 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
You forgot FileHelper.. first I thought it is some file management script.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 29th, 2007, 2:55 pm 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
PhiLho wrote:
You like to go straight to the heart of stuff, unencumbered by most error checking...

Yes, I usually don't like to be bothered by the error messages.
So, I add the error checking only when creating the script, then after some tests I remove them.
If there are complaints to have a difficulty to debug, of course I can put them back.

Quote:
Well, I am not too fond of the choice of the first two names: why ReadMemory writes to a file? It seems a bit strange. WriteMemory indeed put data in memory, but the name doesn't tell it comes from a file.

I admit it's not intuitive at first usage.
However, they are created and mostly used as the counter part of ReadFile/WriteFile.
So, it became rather cumbersome after some usages, was so at least to me, to use different names, like ReadFromMemory, WriteToMemory, or ReadMemoryToFile, WriteMemoryFromFile, etc.

Quote:
Although it is unlikely to change, I am not fond of using boolean * 2 to create 0 or 2.

Yes, I realized that after post. It also does not accord with the usage of bFolder where I used "? : ".
I'll change it when I update the script.

Quote:
I am not sure that the SetEndOfFile is necessary in ReadMemory, but I admit I didn't knew the function so I am happy to discover it, it can be useful to truncate or extend a file (along with SetFileValidData).

As a matter of fact, it's only needed when overwriting a file.
If the original size were larger than the overwritten one, there will remain garbage at the end of the file, without it.
BTW, I never felt the need of SetFileValidData personally, so I didn't add it.

Quote:
In WriteMemory, the VarSetCapacity(sBuffer, -1) is useful if indeed the data read is textual, but isn't there a risk if it is binary? Mmm, probably not, if the buffer isn't shrunk.

I think the buffer won't shrink according to the documentation.
And, you're right it's only for the textual contents.

Quote:
That's lot of criticism, but your code is of course very useful, thanks for sharing.

Thanks.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 29th, 2007, 5:09 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
Quote:
Yes, I usually don't like to be bothered by the error messages.
So, I add the error checking only when creating the script, then after some tests I remove them.
If there are complaints to have a difficulty to debug, of course I can put them back.

I sometimes put error handling sometimes don't depending on mood and free time, but I always know its a bad thing.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 29th, 2007, 5:58 pm 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
majkinetor wrote:
I sometimes put error handling sometimes don't depending on mood and free time, but I always know its a bad thing.
Particularly for scripts handling files: a file might not be writable, for example (or its name isn't legal, etc.).
I suppose it is OK for Sean scripts made for himself, and I understand he just gave the work he uses internally, but it might not be suitable as general purpose functions to release to public, like a standard library.
This is just a warning to those wanting to use them. :-)

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 29th, 2007, 6:35 pm 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
majkinetor wrote:
I sometimes put error handling sometimes don't depending on mood and free time, but I always know its a bad thing.

So, you don't do it just because of mood even though you know not doing is bad?
Are you a kid?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 29th, 2007, 6:44 pm 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
PhiLho wrote:
Particularly for scripts handling files: a file might not be writable, for example (or its name isn't legal, etc.).
I suppose it is OK for Sean scripts made for himself, and I understand he just gave the work he uses internally, but it might not be suitable as general purpose functions to release to public, like a standard library.
This is just a warning to those wanting to use them. :-)

Actually, error checking is quite easy for the file case.
Just check the returned hFile. So, a user can add the check when writing the script.
I added this check in WriteMemory() as a user can't check it himself in this case.
In ReadMemory() case, no file will be created anyway if there is an error which would be much rare than the case of WriteMemory().


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 29th, 2007, 7:45 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
Quote:
So, you don't do it just because of mood even though you know not doing is bad?
Are you a kid?

Yes.
A human kid.

Sad to know you have grown up. My deepest symphaties. :cry:

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 30th, 2007, 1:34 am 
Offline

Joined: June 26th, 2006, 6:14 pm
Posts: 1379
Location: USA
majkinetor wrote:
A human kid.


And all this time I thought you were a cat or a cookie monster. Thanks for clearing that up.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 30th, 2007, 9:11 am 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
Cookie monster? You mean,the monster that tastes like cookie, or the monster who use cookies to scare souls around? In any case, most woman I know would kill to have one cooke monster in the house. I will take that as a compliment 8)

I wish I could be a cat, to see in the dark, to feel things, to be fed by negative energy. But as you are not a kid anymore, I beleive this story sounds not very interesting to you....

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 26th, 2007, 6:53 am 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
I uploaded File.ahk which is in the StandardLibrary format corresponding to FileHelper.ahk.
It can be downloaded from the first post in this thread.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 26th, 2007, 9:03 am 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
Thx

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google Feedfetcher, tkmmkt and 11 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group