FTP via drag n drop + file preview

[ /ftp.ahk ] + [ a.exe ] -_- [ forum post ]

in some cases the fastest solution
•upload to a specific folder and finaly the uploaded file displayed in the default browser

NOTE:
•only the ahk version with hardcoded ftp data works on my pc
•this older script requiers the basic ahk.exe with  239kb

;CODE start ↓
;gui designed ICEfreez http://autohotkey.com/forum/topic45154.html
#singleinstance force
;You can hardcode your settings in here so you don't have to type them all the time.

FTPURL = ftp.domain        ;FTP URL.
FTPU =                               ;FTP User Name
FTPP =                               ;FTP Password
PORT =21                          ;FTP Port

remotelocation =                  ; folder/
weburl = /                               ; url/

;If you don't want a GUI to always change your info just remove this section.
gui, add, text,,FTP URL
gui, add, edit,w600 h40,%FTPURL%
gui, add, text,,FTP User Name
gui, add, edit,w600 h20,%FTPU%
gui, add, text,,FTP Password
gui, add, edit,w600 h20,%FTPP%
gui, add, text,,Port
gui, add, edit,w60 h20,%PORT%
gui, add, text,,Remote folder to drop files into.
gui, add, edit,w600 h20,%remotelocation%
gui, add, text,,URL of the folder these files will be accessable from.
gui, add, edit,w600 h20,%weburl%

;Create GUI
gui, add, text,, Copy last to clipboard.
gui, add, checkbox, vclipboardcheck Checked
gui, add, text,, Open URL after upload.
gui, add, checkbox, vrunafter Checked

gui, add, text, y+20 600, Drop files to upload here.
gui, add, edit, readonly w600 h80 vstatus
gui, show,,Simple Folder FTP
return

;Process a file once you drop a file in.
GuiDropFiles:
gui, submit, nohide
Loop, parse, A_GuiControlEvent, `n
{
;get file name without path.
SplitPath, A_LoopField, name

;Connect and upload file.
FtpOpen(FTPURL, PORT, FTPU , FTPP)
FtpSetCurrentDirectory(remotelocation)
FtpPutFile(A_LoopField, name)
FtpClose()

;update status to show file uploaded
updatemsg(weburl name)

;Copy to Clipboard and Run Options
if(runafter=1)
run, %weburl%%name%
if(clipboardcheck=1)
clipboard=%weburl%%name%
}
Return

GuiClose:
GuiEscape:
ExitApp

updatemsg(msg)
{
gui, submit, nohide
global status
if (status <> " ")
updatestatus = %msg%`n%Status%
GuiControl, ,Status, %updatestatus%
}

;------------------------------------FTP Functions begin.------------------------------------
;FTP functions coded by olfen (http://autohotkey.com/forum/topic10393.html)

/*

http://msdn.microsoft.com/library/en-us/wininet/wininet/ftp_sessions.asp

http://msdn.microsoft.com/library/en-us/wininet/wininet/internetopen.asp

http://msdn.microsoft.com/library/en-us/wininet/wininet/internetconnect.asp

*/

FtpCreateDirectory(DirName) {
global ic_hInternet
r := DllCall("wininetFtpCreateDirectoryA", "uint", ic_hInternet, "str", DirName)
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}

FtpRemoveDirectory(DirName) {
global ic_hInternet
r := DllCall("wininetFtpRemoveDirectoryA", "uint", ic_hInternet, "str", DirName)
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}

FtpSetCurrentDirectory(DirName) {
global ic_hInternet
r := DllCall("wininetFtpSetCurrentDirectoryA", "uint", ic_hInternet, "str", DirName)
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}

FtpPutFile(LocalFile, NewRemoteFile="", Flags=0) {
;Flags:
;FTP_TRANSFER_TYPE_UNKNOWN = 0 (Defaults to FTP_TRANSFER_TYPE_BINARY)
;FTP_TRANSFER_TYPE_ASCII = 1
;FTP_TRANSFER_TYPE_BINARY = 2
If NewRemoteFile=
NewRemoteFile := LocalFile
global ic_hInternet
r := DllCall("wininetFtpPutFileA"
, "uint", ic_hInternet
, "str", LocalFile
, "str", NewRemoteFile
, "uint", Flags
, "uint", 0) ;dwContext
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}

FtpGetFile(RemoteFile, NewFile="", Flags=0) {
;Flags:
;FTP_TRANSFER_TYPE_UNKNOWN = 0 (Defaults to FTP_TRANSFER_TYPE_BINARY)
;FTP_TRANSFER_TYPE_ASCII = 1
;FTP_TRANSFER_TYPE_BINARY = 2
If NewFile=
NewFile := RemoteFile
global ic_hInternet
r := DllCall("wininetFtpGetFileA"
, "uint", ic_hInternet
, "str", RemoteFile
, "str", NewFile
, "int", 1 ;do not overwrite existing files
, "uint", 0 ;dwFlagsAndAttributes
, "uint", Flags
, "uint", 0) ;dwContext
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}

FtpGetFileSize(FileName, Flags=0) {
;Flags:
;FTP_TRANSFER_TYPE_UNKNOWN = 0 (Defaults to FTP_TRANSFER_TYPE_BINARY)
;FTP_TRANSFER_TYPE_ASCII = 1
;FTP_TRANSFER_TYPE_BINARY = 2
global ic_hInternet
fof_hInternet := DllCall("wininetFtpOpenFileA"
, "uint", ic_hInternet
, "str", FileName
, "uint", 0×80000000 ;dwAccess: GENERIC_READ
, "uint", Flags
, "uint", 0) ;dwContext
If (ErrorLevel != 0 or fof_hInternet = 0)
return -1

FileSize := DllCall("wininetFtpGetFileSize", "uint", fof_hInternet, "uint", 0)
DllCall("wininetInternetCloseHandle", "UInt", fof_hInternet)
return, FileSize
}

FtpDeleteFile(FileName) {
global ic_hInternet
r := DllCall("wininetFtpDeleteFileA", "uint", ic_hInternet, "str", FileName)
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}

FtpRenameFile(Existing, New) {
global ic_hInternet
r := DllCall("wininetFtpRenameFileA", "uint", ic_hInternet, "str", Existing, "str", New)
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}

FtpOpen(Server, Port=21, Username=0, Password=0 ,Proxy="", ProxyBypass="") {
IfEqual, Username, 0, SetEnv, Username, anonymous
IfEqual, Password, 0, SetEnv, Password, anonymous

If (Proxy != "")
AccessType=3
Else
AccessType=1
;#define INTERNET_OPEN_TYPE_PRECONFIG 0 // use registry configuration
;#define INTERNET_OPEN_TYPE_DIRECT 1 // direct to net
;#define INTERNET_OPEN_TYPE_PROXY 3 // via named proxy
;#define INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY 4 // prevent using java/script/INS

global ic_hInternet, io_hInternet, hModule
hModule := DllCall("LoadLibrary", "str", "wininet.dll")

io_hInternet := DllCall("wininetInternetOpenA"
, "str", A_ScriptName ;lpszAgent
, "UInt", AccessType
, "str", Proxy
, "str", ProxyBypass
, "UInt", 0) ;dwFlags

If (ErrorLevel != 0 or io_hInternet = 0) {
FtpClose()
return 0
}

ic_hInternet := DllCall("wininetInternetConnectA"
, "uint", io_hInternet
, "str", Server
, "uint", Port
, "str", Username
, "str", Password
, "uint" , 1 ;dwService (INTERNET_SERVICE_FTP = 1)
, "uint", 0 ;dwFlags
, "uint", 0) ;dwContext

If (ErrorLevel != 0 or ic_hInternet = 0)
return 0
else
return 1
}

FtpClose() {
global ic_hInternet, io_hInternet, hModule
DllCall("wininetInternetCloseHandle", "UInt", ic_hInternet)
DllCall("wininetInternetCloseHandle", "UInt", io_hInternet)
DllCall("FreeLibrary", "UInt", hModule)
}

Leave a Reply