PageRenderTime 14ms CodeModel.GetById 11ms app.highlight 1ms RepoModel.GetById 0ms app.codeStats 1ms

/Lib/FTPLib.ahk

http://7plus.googlecode.com/
AutoHotKey | 164 lines | 125 code | 18 blank | 21 comment | 19 complexity | 45050c3687821473e4d51774eb6b39e5 MD5 | raw file
  1/* 
  2http://msdn.microsoft.com/library/en-us/wininet/wininet/ftp_sessions.asp 
  3http://msdn.microsoft.com/library/en-us/wininet/wininet/internetopen.asp 
  4http://msdn.microsoft.com/library/en-us/wininet/wininet/internetconnect.asp 
  5*/ 
  6
  7FtpCreateDirectory(DirName) { 
  8global ic_hInternet 
  9r := DllCall("wininet\FtpCreateDirectoryA", "uint", ic_hInternet, "str", DirName) 
 10If (ErrorLevel != 0 or r = 0) 
 11return 0 
 12else 
 13return 1 
 14} 
 15
 16FtpRemoveDirectory(DirName) { 
 17global ic_hInternet 
 18r := DllCall("wininet\FtpRemoveDirectoryA", "uint", ic_hInternet, "str", DirName) 
 19If (ErrorLevel != 0 or r = 0) 
 20return 0 
 21else 
 22return 1
 23} 
 24
 25FtpSetCurrentDirectory(DirName) { 
 26global ic_hInternet 
 27r := DllCall("wininet\FtpSetCurrentDirectoryA", "uint", ic_hInternet, "str", DirName) 
 28If (ErrorLevel != 0 or r = 0) 
 29return 0 
 30else 
 31return 1 
 32} 
 33
 34FtpPutFile(LocalFile, NewRemoteFile="", Flags=0) { 
 35;Flags: 
 36;FTP_TRANSFER_TYPE_UNKNOWN = 0 (Defaults to FTP_TRANSFER_TYPE_BINARY) 
 37;FTP_TRANSFER_TYPE_ASCII = 1 
 38;FTP_TRANSFER_TYPE_BINARY = 2 
 39If NewRemoteFile= 
 40NewRemoteFile := LocalFile 
 41global ic_hInternet 
 42r := DllCall("wininet\FtpPutFileA" 
 43, "uint", ic_hInternet 
 44, "str", LocalFile 
 45, "str", NewRemoteFile 
 46, "uint", Flags 
 47, "uint", 0) ;dwContext 
 48If (ErrorLevel != 0 or r = 0) 
 49return 0 
 50else 
 51return 1 
 52} 
 53
 54FtpGetFile(RemoteFile, NewFile="", Flags=0) { 
 55;Flags: 
 56;FTP_TRANSFER_TYPE_UNKNOWN = 0 (Defaults to FTP_TRANSFER_TYPE_BINARY) 
 57;FTP_TRANSFER_TYPE_ASCII = 1 
 58;FTP_TRANSFER_TYPE_BINARY = 2 
 59If NewFile= 
 60NewFile := RemoteFile 
 61global ic_hInternet 
 62r := DllCall("wininet\FtpGetFileA" 
 63, "uint", ic_hInternet 
 64, "str", RemoteFile 
 65, "str", NewFile 
 66, "int", 1 ;do not overwrite existing files 
 67, "uint", 0 ;dwFlagsAndAttributes 
 68, "uint", Flags 
 69, "uint", 0) ;dwContext 
 70If (ErrorLevel != 0 or r = 0) 
 71return 0 
 72else 
 73return 1 
 74} 
 75
 76FtpGetFileSize(FileName, Flags=0) { 
 77;Flags: 
 78;FTP_TRANSFER_TYPE_UNKNOWN = 0 (Defaults to FTP_TRANSFER_TYPE_BINARY) 
 79;FTP_TRANSFER_TYPE_ASCII = 1 
 80;FTP_TRANSFER_TYPE_BINARY = 2 
 81global ic_hInternet 
 82fof_hInternet := DllCall("wininet\FtpOpenFileA" 
 83, "uint", ic_hInternet 
 84, "str", FileName 
 85, "uint", 0x80000000 ;dwAccess: GENERIC_READ 
 86, "uint", Flags 
 87, "uint", 0) ;dwContext 
 88If (ErrorLevel != 0 or fof_hInternet = 0) 
 89return -1 
 90
 91FileSize := DllCall("wininet\FtpGetFileSize", "uint", fof_hInternet, "uint", 0) 
 92DllCall("wininet\InternetCloseHandle",  "UInt", fof_hInternet) 
 93return, FileSize 
 94} 
 95
 96
 97FtpDeleteFile(FileName) { 
 98global ic_hInternet 
 99r :=  DllCall("wininet\FtpDeleteFileA", "uint", ic_hInternet, "str", FileName) 
100If (ErrorLevel != 0 or r = 0) 
101return 0 
102else 
103return 1 
104} 
105
106FtpRenameFile(Existing, New) { 
107global ic_hInternet 
108r := DllCall("wininet\FtpRenameFileA", "uint", ic_hInternet, "str", Existing, "str", New) 
109If (ErrorLevel != 0 or r = 0) 
110return 0 
111else 
112return 1 
113} 
114
115FtpOpen(Server, Port=21, Username=0, Password=0 ,Proxy="", ProxyBypass="") { 
116IfEqual, Username, 0, SetEnv, Username, anonymous 
117IfEqual, Password, 0, SetEnv, Password, anonymous 
118
119If (Proxy != "") 
120AccessType=3 
121Else 
122AccessType=1 
123;#define INTERNET_OPEN_TYPE_PRECONFIG                    0   // use registry configuration 
124;#define INTERNET_OPEN_TYPE_DIRECT                       1   // direct to net 
125;#define INTERNET_OPEN_TYPE_PROXY                        3   // via named proxy 
126;#define INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY  4   // prevent using java/script/INS 
127
128global ic_hInternet, io_hInternet, hModule 
129hModule := DllCall("LoadLibrary", "str", "wininet.dll") 
130
131io_hInternet := DllCall("wininet\InternetOpenA" 
132, "str", A_ScriptName ;lpszAgent 
133, "UInt", AccessType 
134, "str", Proxy 
135, "str", ProxyBypass 
136, "UInt", 0) ;dwFlags 
137
138If (ErrorLevel != 0 or io_hInternet = 0) { 
139FtpClose() 
140return 0
141} 
142
143ic_hInternet := DllCall("wininet\InternetConnectA" 
144, "uint", io_hInternet 
145, "str", Server 
146, "uint", Port 
147, "str", Username 
148, "str", Password 
149, "uint" , 1 ;dwService (INTERNET_SERVICE_FTP = 1) 
150, "uint", 0 ;dwFlags 
151, "uint", 0) ;dwContext 
152
153If (ErrorLevel != 0 or ic_hInternet = 0) 
154return 0 
155else 
156return 1 
157} 
158
159FtpClose() { 
160global ic_hInternet, io_hInternet, hModule 
161DllCall("wininet\InternetCloseHandle",  "UInt", ic_hInternet) 
162DllCall("wininet\InternetCloseHandle",  "UInt", io_hInternet) 
163DllCall("FreeLibrary", "UInt", hModule) 
164}