AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

File Standard Library
Goto page 1, 2  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Sean



Joined: 12 Feb 2007
Posts: 1337

PostPosted: Tue May 29, 2007 12:20 pm    Post subject: File Standard Library Reply with quote

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 Sat Oct 06, 2007 5:52 am; edited 5 times in total
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 1337

PostPosted: Tue May 29, 2007 12:29 pm    Post subject: Reply with quote

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 Wed May 30, 2007 7:10 am; edited 2 times in total
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 3626
Location: Belgrade

PostPosted: Tue May 29, 2007 12:57 pm    Post subject: Reply with quote

I am impressed how you choose your names.
_________________
Back to top
View user's profile Send private message MSN Messenger
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Tue May 29, 2007 1:53 pm    Post subject: Reply with quote

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.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
majkinetor



Joined: 24 May 2006
Posts: 3626
Location: Belgrade

PostPosted: Tue May 29, 2007 2:19 pm    Post subject: Reply with quote

You forgot FileHelper.. first I thought it is some file management script.
_________________
Back to top
View user's profile Send private message MSN Messenger
Sean



Joined: 12 Feb 2007
Posts: 1337

PostPosted: Tue May 29, 2007 2:55 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 3626
Location: Belgrade

PostPosted: Tue May 29, 2007 5:09 pm    Post subject: Reply with quote

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.
_________________
Back to top
View user's profile Send private message MSN Messenger
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Tue May 29, 2007 5:58 pm    Post subject: Reply with quote

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. Smile
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Sean



Joined: 12 Feb 2007
Posts: 1337

PostPosted: Tue May 29, 2007 6:35 pm    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 1337

PostPosted: Tue May 29, 2007 6:44 pm    Post subject: Reply with quote

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. Smile

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().
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 3626
Location: Belgrade

PostPosted: Tue May 29, 2007 7:45 pm    Post subject: Reply with quote

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. Crying or Very sad
_________________
Back to top
View user's profile Send private message MSN Messenger
ahklerner



Joined: 26 Jun 2006
Posts: 1205
Location: USA

PostPosted: Wed May 30, 2007 1:34 am    Post subject: Reply with quote

majkinetor wrote:
A human kid.


And all this time I thought you were a cat or a cookie monster. Thanks for clearing that up.
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 3626
Location: Belgrade

PostPosted: Wed May 30, 2007 9:11 am    Post subject: Reply with quote

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 Cool

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....
_________________
Back to top
View user's profile Send private message MSN Messenger
Sean



Joined: 12 Feb 2007
Posts: 1337

PostPosted: Wed Sep 26, 2007 6:53 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 3626
Location: Belgrade

PostPosted: Wed Sep 26, 2007 9:03 am    Post subject: Reply with quote

Thx
_________________
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group