Jump to content
Sign in to follow this  
tom13

Upload File help please

Recommended Posts

tom13

Hey,

I'm completely new to AutoIT. What I want is a script that will upload a file to some FTP address. This is what I copied from another script:

_FTPConnect()
_FTPPutFile()

;===============================================================================
;
; Function Name:    _FTPConnect()
; Description:    Connects to an FTP server.
; Parameter(s):  $l_InternetSession - The Long from _FTPOpen()
;                  $s_ServerName        - Server name/ip.
;                  $s_Username        - Username.
;                  $s_Password          - Password.
;                  $i_ServerPort          - Server port ( 0 is default (21) )
;                   $l_Service          - I dont got a clue what this does.
;                   $l_Flags            - Special flags.
;                   $l_Context          - I dont got a clue what this does.
; Requirement(s):   DllCall, wininet.dll
; Return Value(s):  On Success - Returns an indentifier.
;                  On Failure - 0  and sets @ERROR
; Author(s):        Wouter van Kesteren
;
;===============================================================================

Func _FTPConnect($l_InternetSession, $s_ServerName = "xxx.xxx.xxx.xxx", $s_Username = "username", $s_Password = "password", $i_ServerPort = 0, $l_Service = 1, $l_Flags = 0, $l_Context = 0)
    
    Local $ai_InternetConnect = DllCall('wininet.dll', 'long', 'InternetConnect', 'long', $l_InternetSession, 'str', $s_ServerName, 'int', $i_ServerPort, 'str', $s_Username, 'str', $s_Password, 'long', $l_Service, 'long', $l_Flags, 'long', $l_Context)
    If @error OR $ai_InternetConnect[0] = 0 Then
        SetError(-1)
        Return 0
    EndIf
            
    Return $ai_InternetConnect[0]
    
EndFunc;==> _FTPConnect()

;===============================================================================
;
; Function Name:    _FTPPutFile()
; Description:    Puts an file on an FTP server.
; Parameter(s):  $l_FTPSession  - The Long from _FTPConnect()
;                  $s_LocalFile     - The local file.
;                  $s_RemoteFile      - The remote Location for the file.
;                  $l_Flags     - Special flags.
;                  $l_Context     - I dont got a clue what this does.
; Requirement(s):   DllCall, wininet.dll
; Return Value(s):  On Success - 1
;                  On Failure - 0
; Author(s):        Wouter van Kesteren
;
;===============================================================================

Func _FTPPutFile($l_FTPSession = "long", $s_LocalFile = "text.txt", $s_RemoteFile = "log.txt", $l_Flags = 0, $l_Context = 0)

    Local $ai_FTPPutFile = DllCall('wininet.dll', 'int', 'FtpPutFile', 'long', $l_FTPSession, 'str', $s_LocalFile, 'str', $s_RemoteFile, 'long', $l_Flags, 'long', $l_Context)
    If @error OR $ai_FTPPutFile[0] = 0 Then
        SetError(-1)
        Return 0
    EndIf
    
    Return $ai_FTPPutFile[0]
    
EndFunc;==> _FTPPutFile()

As it is now, it's not working. Can you help me to let this work please?

So, I want text.txt to be uploaded to ftp://xxx.xxx.xxx.xxx when you open the script.

Thanks in advance

Edited by tom13

Share this post


Link to post
Share on other sites
BrettF

Hi Tom13,

Welcome to the forums.

With the functions, you need to use the parameters, like:

_FTPConnect($l_InternetSession, $s_ServerName = "xxx.xxx.xxx.xxx", $s_Username = "username", $s_Password = "password", $i_ServerPort = 0, $l_Service = 1, $l_Flags = 0, $l_Context = 0)

_FTPPutFile($l_FTPSession = "long", $s_LocalFile = "text.txt", $s_RemoteFile = "log.txt", $l_Flags = 0, $l_Context = 0)

Try that and see how you go :shocked:

Share this post


Link to post
Share on other sites
Jos

don't forget to do a DllLoad() at the beginning and use the Handle returned or else will give problems.

:shocked:


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
tom13

Hi Tom13,

Welcome to the forums.

With the functions, you need to use the parameters, like:

_FTPConnect($l_InternetSession, $s_ServerName = "xxx.xxx.xxx.xxx", $s_Username = "username", $s_Password = "password", $i_ServerPort = 0, $l_Service = 1, $l_Flags = 0, $l_Context = 0)

_FTPPutFile($l_FTPSession = "long", $s_LocalFile = "text.txt", $s_RemoteFile = "log.txt", $l_Flags = 0, $l_Context = 0)

Try that and see how you go :shocked:

Thanks for your reply, but when I update it and run it I get this error:

Error: Variable used without being declared.

Please keep in mind that I know nothing about AutoIT so I'm probably doing something very newbies :(

Share this post


Link to post
Share on other sites
BrettF

Thanks for your reply, but when I update it and run it I get this error:

Error: Variable used without being declared.

Please keep in mind that I know nothing about AutoIT so I'm probably doing something very newbies :shocked:

Ok, I think I should have made it a little clearer, my bad on my behalf. Just to make sure you have to do this:

#Include <FTP.au3>
$server = 'server.host.com'
$username = 'username'
$pass = 'password'

$Open = _FTPOpen('MyFTP Control')
$Conn = _FTPConnect($Open, $server, $username, $pass)

If $Conn = 0 Then
    MsgBox (0, "Error", "Error!")
EndIf

$Ftpp = _FtpPutFile($Conn, 'FULL\PATH\TO\TEXT', '/text.txt')
$Ftpc = _FTPClose($Open)

I'm assuming you have the full FTP UDF bu w0uter in your script dir.

So you declare those varibles at the top, and then open the connection. Now you connect to your FTP server, and the you check it for an error. Then you put your file and the close the connection. Thats it really. :(

Share this post


Link to post
Share on other sites
Zedna

Ok, I think I should have made it a little clearer, my bad on my behalf. Just to make sure you have to do this:

I'm assuming you have the full FTP UDF bu w0uter in your script dir.

So you declare those varibles at the top, and then open the connection. Now you connect to your FTP server, and the you check it for an error. Then you put your file and the close the connection. Thats it really. :shocked:

As JDeB said you missed DllOpen/DllClose:

and also Exit after error

#Include <FTP.au3>

$server = 'server.host.com'
$username = 'username'
$pass = 'password'

$dllhandle = DllOpen('wininet.dll')
$Open = _FTPOpen('MyFTP Control')
$Conn = _FTPConnect($Open, $server, $username, $pass)

If $Conn = 0 Then
    MsgBox (0, "Error", "Error!")
    Exit
EndIf

$Ftpp = _FtpPutFile($Conn, 'C:\FULL\PATH\TO\TEXT', '/text.txt')
$Ftpc = _FTPClose($Open)
DllClose($dllhandle)
Edited by Zedna

Share this post


Link to post
Share on other sites
BrettF
BrettF
Jazkal

Zedna (or anybody),

Would you happen to know the needed change in that example script to make a passive upload?

Share this post


Link to post
Share on other sites
Jos

This is an updated FTPHandleFile() which will Upload a file to the given FTP server and uses the Dllopen Handle:

Func FTPHandleFile($s_ServerName, $s_Username, $s_Password, $Source_File, $Target_File, $Transfer_Type)
    $h_FTP_Dll = DllOpen('wininet.dll')
    $Open = _FTPOpen($h_FTP_Dll, 'my ftp')
    If $Open <> 0 Then
        $Conn = _FTPConnect($h_FTP_Dll, $Open, $s_ServerName, $s_Username, $s_Password)
        If $Conn <> 0 Then
            $Ftpp = _FTPPutFile($h_FTP_Dll, $Conn, $Source_File, $Target_File, $Transfer_Type)
            If @error = 0 Then
                ConsoleWrite("File:" & $Source_File & " copied to:" & $Target_File & @LF)
                Return 1
            Else
                ConsoleWrite("Error:" & $Ftpp & " Failed copying File:" & $Source_File & " to:" & $Target_File & @LF)
                Return 0
            EndIf
        Else
            ConsoleWrite("Failed starting connection" & @LF)
        EndIf
        $Ftpc = _FTPClose($h_FTP_Dll, $Open)
    Else
        ConsoleWrite("Failed opening connection")
    EndIf
    DllClose($h_FTP_Dll)
EndFunc   ;==>FTPHandleFile

Use attached Ftp.au3 which is a modified version of W0uter's posted version.

Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
Jos

Zedna (or anybody),

Would you happen to know the needed change in that example script to make a passive upload?

Try this to see if that works:

Update this in the previous posted example.

$INTERNET_FLAG_PASSIVE= 0x08000000
$Conn = _FTPConnect($h_FTP_Dll, $Open, $s_ServerName, $s_Username, $s_Password,0,$INTERNET_FLAG_PASSIVE)

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
Zedna

This is an updated FTPHandleFile() which will Upload a file to the given FTP server and uses the Dllopen Handle:

Use attached Ftp.au3 which is a modified version of W0uter's posted version.

I think better solution will be to add into FTP.au3 UDF DllOpen('wininet.dll'), so this DLL will be opened at start of whole script which has #include <ftp.au3>

DllClose() will be automatically done by Autoit at end of script.

So no need to make modified UDF with changed paramtres in every function

Share this post


Link to post
Share on other sites
BrettF

I think better solution will be to add into FTP.au3 UDF DllOpen('wininet.dll'), so this DLL will be opened at start of whole script which has #include <ftp.au3>

DllClose() will be automatically done by Autoit at end of script.

So no need to make modified UDF with changed paramtres in every function

or maybe in _FTPOpen/_FTPClose????

Share this post


Link to post
Share on other sites
Zedna

or maybe in _FTPOpen/_FTPClose????

Yes that's good idea.

So in _FTPOpen().

In _FTPClose() not because you have to make $dll_handle make as global variable (return from _FTPOpen()

so it's simpler to just make DllOpen in _FTPOpen()

and AutoIt automatically close Dll on script exit.

Share this post


Link to post
Share on other sites
Jos

Either way works, but you still should change W0uters UDF's to use the retuned Handle by DllLoad() in stead of "winInet.dll"...

:shocked:


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
Jos

Come to think of it, I have been waiting for somebody to take the FTP.au3 UDFs and submit something that's worth putting in the standard UDF library ..

Surprised nobody did that yet ...

:shocked:


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.