/Lib/FTPLib.ahk

http://7plus.googlecode.com/ · AutoHotKey · 164 lines · 125 code · 18 blank · 21 comment · 19 complexity · 45050c3687821473e4d51774eb6b39e5 MD5 · raw file

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