Benny-D wrote:
That code is so big! And I don't understand anything there.
I went with
INet.ahk. I wanted to create and upload a redirection html script located in 2 different versions (spellings) of a directory on my home page. I basically tacked
INet.ahk onto this
code (scroll down to
Ace_NoOne, Posted: Sun May 27, 2007 8:17 pm)
If the directories don't exist, they will be created.
Code:
#NoEnv
Server = home.blahblah.net
Port = 21
User = xxx
Pwd = xxx
LocalFile = %1%
Directory1 = /Directory
Directory2 = /directory
; Check that the redirection file actually exists before we try to upload it
IfNotExist, %LocalFile%
{
MsgBox, Local File %LocalFile% does not exist.
Exit, 1
}
; Initialize Internet resources and make Internet functions available
if (not INetStart())
{
MsgBox, Failed to initialize.
Exit, 1
}
; Open a connection to the FTP server
hConnection := INetConnect(Server, Port, User, Pwd, "ftp", 1)
if (hConnection)
{
Error = 0
if (not UploadFile(hConnection, Directory1, LocalFile))
Error = 1
if (not UploadFile(hConnection, Directory2, LocalFile))
Error = 1
INetCloseHandle(hConnection)
INetStop()
Exit, Error
}
else
{
MsgBox, Failed to create a connection to the FTP server.
INetStop()
Exit, 1
}
UploadFile(hConnection, Directory, LocalFile)
{
if (ChangeDirectory(hConnection, Directory))
{
SplitPath, LocalFile, RemoteFile
if (FtpPutFile(hConnection, LocalFile, RemoteFile))
{
Return, true
}
else
{
MsgBox, Cannot upload file into %Directory%.
Return, false
}
}
else
{
MsgBox, Could not create or change to remote directory %Directory%.
Return, false
}
}
ChangeDirectory(hConnection, Directory)
{
if (FtpSetCurrentDirectory(hConnection, Directory))
{
Return, true
}
else
{
if (FtpCreateDirectory(hConnection, Directory))
{
if (FtpSetCurrentDirectory(hConnection, Directory))
{
Return, true
}
else
{
MsgBox, Cannot change to remote directory %Directory%.
Return, false
}
}
else
{
MsgBox, Cannot create remote directory %Directory%.
Return, false
}
MsgBox, Cannot change to remote directory %Directory%.
Return, false
}
}
; ******************************************************************************
; ------------------------------------------------------------------------------
; This function loads and tells the Internet DLL to initialize internal data structures
; and prepare for future calls from the application.
;
; Internet functions are not available before this function is called.
;
; param Proxy [in]
; Specifies an optional name of a proxy server.
; param ProxyBypass [in]
; Specifies an optional list of host names or IP addresses, or both, that should not be routed
; through the proxy when a proxy is used.
; param Agent [in]
; Specifies the name of the application or entity calling the WinINet functions.
; This name is used as the user agent in the HTTP protocol.
; When empty, the name of the script is used
; return
; [bool] true if the function succeeds, otherwise false
;
INetStart(Proxy="", ProxyBypass="", Agent="")
{
global
; get a handle to the WinINet library
inet_hModule := DllCall("LoadLibrary", "str", "wininet.dll")
if(!inet_hModule) {
inet_hModule = 0
return false
}
; INTERNET_OPEN_TYPE_PRECONFIG 0 // use registry configuration
; INTERNET_OPEN_TYPE_DIRECT 1 // direct to net
; INTERNET_OPEN_TYPE_PROXY 3 // via named proxy
; INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY 4 // prevent using java/script/INS
inet_hInternet := DllCall("wininet\InternetOpenA"
, "str", (Agent != "" ? Agent : A_ScriptName)
, "UInt", (Proxy != "" ? 3 : 1)
, "str", Proxy
, "str", ProxyBypass
, "UInt", 0)
If(!inet_hInternet) {
INetCloseHandle(inet_hModule)
return false
}
return true
}
; ------------------------------------------------------------------------------
; Terminates the application's use of WinINet functions and unloads the Internet DLL.
;
; Internet functions are not available after this function call is called.
;
INetStop()
{
global
INetCloseHandle(inet_hInternet)
DllCall("FreeLibrary", "UInt", inet_hModule)
inet_hModule = inet_hInternet = 0
}
; ------------------------------------------------------------------------------
; Closes a single Internet handle, previously opened by INetConnect, FtpOpenFile, ...
;
; param hConnection [in]
; The connection handle to be closed.
; return
; [bool] true if the function succeeds, otherwise false
;
INetCloseHandle(hInternet)
{
return DllCall("wininet\InternetCloseHandle", "UInt", hInternet)
}
; ------------------------------------------------------------------------------
; Opens an File Transfer Protocol (FTP), Gopher, or HTTP session for a given site.
;
; FtpPassive [in]
; The value 1 causes the application to use passive FTP semantics
;
; param Server [in]
; The host name or IP number of a server.
; param Server [in]
; The TCP/IP port to connect to.
; param Username [in]
; The name of the user to log on (default is anonymous)
; param Password [in]
; The password to use to log on (default is anonymous)
; param Service [in]
; Type of service to access. Must be of the following value: http (default), ftp, url or gopher
; param FtpPassive [in]
; Option specific to ftp service. Setting 1 causes the application to use passive FTP semantics.
; return
; the handle of the opened connection
;
INetConnect(Server, Port, Username="anonymous", Password="anonymous", Service="http", FtpPassive=0)
{
global inet_hInternet
hConnection := DllCall("wininet\InternetConnectA"
, "uint", inet_hInternet
, "str", Server
, "uint", Port
, "str", Username
, "str", Password
, "uint", (Service = "ftp" ? 1 : (Service = "gopher" ? 2 : 3)) ; INTERNET_SERVICE_xxx
, "uint", (FtpPassive != 0 ? 0x08000000 : 0) ; INTERNET_FLAG_PASSIVE
, "uint", 0)
return hConnection
}
; ------------------------------------------------------------------------------
; Creates a directory on a FTP server.
;
; param hConnection [in]
; A valid connection handle returned by an INetConnect call (using Type="ftp").
; param Directory [in]
; The name of the directory to be created. This can be a fully qualified path
; or a name relative to the current directory.
; return
; [bool] true if the function succeeds, otherwise false
;
FtpCreateDirectory(hConnection, Directory)
{
return DllCall("wininet\FtpCreateDirectoryA", "uint", hConnection, "str", Directory)
}
; ------------------------------------------------------------------------------
; Sets the current working directory for a specified FTP session.
;
; param hConnection [in]
; A valid connection handle returned by an INetConnect call (using Type="ftp").
; param Directory [in]
; The name of the directory to become the current working directory. This can be a fully qualified path
; or a name relative to the current directory.
; return
; [bool] true if the function succeeds, otherwise false
;
FtpSetCurrentDirectory(hConnection, Directory)
{
return DllCall("wininet\FtpSetCurrentDirectoryA", "uint", hConnection, "str", Directory)
}
; ------------------------------------------------------------------------------
; Gets the current working directory of a specified FTP session.
;
; param hConnection [in]
; a valid connection handle returned by an INetConnect call (using Type="ftp")
; param Directory [in]
; a variable that receives the absolute path of the current directory
; return
; [bool] true if the function succeeds, otherwise false
;
FtpGetCurrentDirectory(hConnection, ByRef @Directory)
{
len := 261 ; MAX_PATH 260
VarSetCapacity(@Directory, len)
result := DllCall("wininet\FtpGetCurrentDirectoryA", "uint", hConnection, "str", @Directory, "uint*", len)
VarSetCapacity(@Directory, -1)
return result
}
; ------------------------------------------------------------------------------
; Uploads a file to a FTP server (the remote file is overwritten if it already exists).
;
; param hConnection [in]
; a valid connection handle returned by an INetConnect call (using Type="ftp")
; param LocalFile [in]
; name of the local file to upload
; param RemoteFile [in]
; name of the remote file to create (when empty, the remote file created will have the same name than the local one)
; param TransferType [in]
; "A" for ascii transfert or "B" (default) for binary mode
; return
; [bool] true if the function succeeds, otherwise false
;
FtpPutFile(hConnection, LocalFile, RemoteFile="", TransferType="B")
{
return DllCall("wininet\FtpPutFileA"
, "uint", hConnection
, "str", LocalFile
, "str", (RemoteFile != "" ? RemoteFile : LocalFile)
, "uint", (TransferType == "A" ? 1 : 2) ; FTP_TRANSFER_TYPE_ASCII or FTP_TRANSFER_TYPE_BINARY
, "uint", 0)
}
; ------------------------------------------------------------------------------
; Deletes a file stored on a FTP server
;
; param hConnection [in]
; a valid connection handle returned by an INetConnect call (using Type="ftp")
; param File [in]
; the name of the file to be deleted
; return
; [bool] true if the function succeeds, otherwise false
;
FtpDeleteFile(hConnection, File)
{
return DllCall("wininet\FtpDeleteFileA", "uint", hConnection, "str", File)
}
I didn't really try to understand
INet.ahk, I just mercilessly deleted the functions I didn't use. The script expects the filename (and path) to be uploaded.
Not particularly elegant, but it works!
