Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

File Standard Library


  • Please log in to reply
23 replies to this topic
Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007
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.

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007
FTP download with passive mode example:

PS. I'll remove the actual address tomorrow.

#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)


majkinetor
  • Moderators
  • 4512 posts
  • Last active: Jul 29 2016 12:40 AM
  • Joined: 24 May 2006
I am impressed how you choose your names.
Posted Image

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
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.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

majkinetor
  • Moderators
  • 4512 posts
  • Last active: Jul 29 2016 12:40 AM
  • Joined: 24 May 2006
You forgot FileHelper.. first I thought it is some file management script.
Posted Image

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

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.

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.

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.

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.

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.

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

Thanks.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: Jul 29 2016 12:40 AM
  • Joined: 24 May 2006

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.
Posted Image

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005

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. :-)
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

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?

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

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().

majkinetor
  • Moderators
  • 4512 posts
  • Last active: Jul 29 2016 12:40 AM
  • Joined: 24 May 2006

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:
Posted Image

ahklerner
  • Members
  • 1386 posts
  • Last active: Oct 08 2014 10:29 AM
  • Joined: 26 Jun 2006

A human kid.


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

majkinetor
  • Moderators
  • 4512 posts
  • Last active: Jul 29 2016 12:40 AM
  • Joined: 24 May 2006
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....
Posted Image

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007
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.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: Jul 29 2016 12:40 AM
  • Joined: 24 May 2006
Thx
Posted Image