Jump to content


Photo

File Standard Library


  • Please log in to reply
22 replies to this topic

#1 Sean

Sean
  • Members
  • 2462 posts

Posted 29 May 2007 - 11:20 AM

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.

#2 Sean

Sean
  • Members
  • 2462 posts

Posted 29 May 2007 - 11:29 AM

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)


#3 majkinetor

majkinetor
  • Fellows
  • 4511 posts

Posted 29 May 2007 - 11:57 AM

I am impressed how you choose your names.

#4 PhiLho

PhiLho
  • Fellows
  • 6850 posts

Posted 29 May 2007 - 12:53 PM

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.

#5 majkinetor

majkinetor
  • Fellows
  • 4511 posts

Posted 29 May 2007 - 01:19 PM

You forgot FileHelper.. first I thought it is some file management script.

#6 Sean

Sean
  • Members
  • 2462 posts

Posted 29 May 2007 - 01:55 PM

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.

#7 majkinetor

majkinetor
  • Fellows
  • 4511 posts

Posted 29 May 2007 - 04:09 PM

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.

#8 PhiLho

PhiLho
  • Fellows
  • 6850 posts

Posted 29 May 2007 - 04:58 PM

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

#9 Sean

Sean
  • Members
  • 2462 posts

Posted 29 May 2007 - 05:35 PM

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?

#10 Sean

Sean
  • Members
  • 2462 posts

Posted 29 May 2007 - 05:44 PM

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

#11 majkinetor

majkinetor
  • Fellows
  • 4511 posts

Posted 29 May 2007 - 06:45 PM

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:

#12 ahklerner

ahklerner
  • Members
  • 1382 posts

Posted 30 May 2007 - 12:34 AM

A human kid.


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

#13 majkinetor

majkinetor
  • Fellows
  • 4511 posts

Posted 30 May 2007 - 08:11 AM

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

#14 Sean

Sean
  • Members
  • 2462 posts

Posted 26 September 2007 - 05:53 AM

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.

#15 majkinetor

majkinetor
  • Fellows
  • 4511 posts

Posted 26 September 2007 - 08:03 AM

Thx