[Class] FTP

Post your working scripts, libraries and tools for AHK v1.1 and older
ShatterCoder
Posts: 73
Joined: 06 Oct 2016, 15:57

Re: [Class] FTP

Post by ShatterCoder » 09 Jun 2023, 15:51

Also the "experimental" FindFiles() method works perfectly in my own testing/usage. It was super helpful for this project. Thanks again for providing such an excellent class library

helloicanseeu
Posts: 7
Joined: 30 Nov 2023, 01:32

Re: [Class] FTP

Post by helloicanseeu » 11 Jan 2024, 23:27

ShatterCoder wrote:
09 Jun 2023, 15:39
I'd like to submit an addition to the class: FIleRead() which allows you to read a file from the FTP server directly to a variable without needing to write to disk.

Code: Select all

ReadFile(hConnect, FileName)
	{
		static GENERIC_READ := 0x80000000
		if (hFile := this.OpenFile(hConnect, FileName, GENERIC_READ))
		{
			data_out := ""
			chunksize := 1024 ;chunk size to request per dllcall
			VarSetCapacity(buf, chunksize ) 
			VarSetCapacity(bytes_read, 4 * 4) ;dword
			bytes_read := 0
			while (DllCall("wininet\InternetReadFile", "ptr", hFile, "Uptr", &buf, "uint", chunksize , "ptr", &bytes_read) && bytes_read)
				data_out .= StrGet(&Buf + 0, "UTF-8")
			this.InternetCloseHandle(hFile)
			return data_out
		}
		return false
	}
I'm not sure how many people will have need of this particular method, but for myself it was needed because I was having to look through multiple files on an FTP server per second to look for a specific string so avoiding disk read/write time was essential.

example usage:

Code: Select all

hFTP := FTP.Open("AHK-FTP")
hSession := FTP.Connect(hFTP, "ftp.example.com", 21, "user", "passwd")
Filecontent := FTP.ReadFile(hSession, "testfile.txt")
msgbox, % Filecontent
FTP.Disconnect(hSession)
FTP.Close(hFTP)
tyty needed this function too, u saved me hours of work

ShatterCoder
Posts: 73
Joined: 06 Oct 2016, 15:57

Re: [Class] FTP

Post by ShatterCoder » 26 Jan 2024, 13:38

helloicanseeu wrote:
11 Jan 2024, 23:27
tyty needed this function too, u saved me hours of work
Glad someone else could benefit from my pain :thumbup:

helloicanseeu
Posts: 7
Joined: 30 Nov 2023, 01:32

Re: [Class] FTP

Post by helloicanseeu » 04 Mar 2024, 06:19

ShatterCoder wrote:
26 Jan 2024, 13:38
helloicanseeu wrote:
11 Jan 2024, 23:27
tyty needed this function too, u saved me hours of work
Glad someone else could benefit from my pain :thumbup:

just to add, i had to reduce chunksize in ReadFile(hConnect, FileName)
from 1024 to 512/256, else it was intermittently working with my ftp servers,
was dealing with files the size 100+ bytes though.
HF GL

Post Reply

Return to “Scripts and Functions (v1)”