[Class] FTP

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

Re: [Class] FTP

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

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: 145
Joined: 06 Oct 2016, 15:57

Re: [Class] FTP

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

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
Albireo
Posts: 1821
Joined: 16 Oct 2013, 13:53

Re: [Class] FTP

08 Jul 2024, 10:07

Will this script be adapted to AHK 2.0?
User avatar
jNizM
Posts: 3201
Joined: 30 Sep 2013, 01:33
Contact:

Re: [Class] FTP

09 Jul 2024, 07:04

I'll take a look at it. However, FTP has been considered unsafe for years. It's best to use SFTP.
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
Albireo
Posts: 1821
Joined: 16 Oct 2013, 13:53

Re: [Class] FTP

09 Jul 2024, 07:51

True, but in my case the information is encrypted. Maybe there is a similar AHK solution for SFTP?

Don't know if it's difficult, but one thing I noticed is that it gets the same error message regardless of whether the IP address is incorrect, or if the user / password is incorrect.
Then it takes quite a long time (in uncertainty) before the error message appears. (has the program hung or - what happens?...)
Andreichxxx
Posts: 3
Joined: 28 Sep 2024, 06:15

Re: [Class] FTP

28 Sep 2024, 07:20

Guys, please help, show an example how to get the date of creation (upload) of a file FTP server.
Andreichxxx
Posts: 3
Joined: 28 Sep 2024, 06:15

Re: [Class] FTP

05 Oct 2024, 05:23

I've been looking for an answer for days now, this option just shows a random time.

Code: Select all

hFTP := FTP.Open("AHK-FTP")
hSession := FTP.Connect(hFTP, "Server", 21, "UserName", "Password")
item := FTP.FindFirstFile(hSession,"/upload/1.txt")
MsgBox % "Name : " . item.Name
 . "`nCreationTime : " . item.CreationTime
 . "`nLastAccessTime : " . item.LastAccessTime
 . "`nLastWriteTime : " . item.LastWriteTime
FTP.Disconnect(hSession)
FTP.Close(hFTP)

#Include FTP.ahk
Andreichxxx
Posts: 3
Joined: 28 Sep 2024, 06:15

Re: [Class] FTP

06 Oct 2024, 10:36

Andreichxxx wrote:
28 Sep 2024, 07:20
Guys, please help, show an example how to get the date of creation (upload) of a file FTP server.
I solved the problem this way!

Code: Select all

hFTP := FTP.Open("AHK-FTP")
hSession := FTP.Connect(hFTP, "Serv", 21, "Name", "Passw")
for k, File in FTP.FindFiles(hSession,"/upload/Soft/1.txt")
MsgBox % ""
   . "`nFile name : " . File.FileName
   . "`nDate of file modification : " . File.LastWriteTime
FTP.Disconnect(hSession)
FTP.Close(hFTP)

#Include FTP.ahk
Albireo
Posts: 1821
Joined: 16 Oct 2013, 13:53

Re: [Class] FTP

06 Oct 2024, 14:23

Thanks for your posts.
Will test if it can work for me too.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 135 guests