Jump to content
Sign in to follow this  
FreeRider

Ftp Appending data

Recommended Posts

FreeRider

Hi all,

I'm working on a peace of script for a while now and I'm looking for an easy way to append a local file content to a remote file based on a Ftp Server.

When doing it using "FtpWriteFile" function of the Wininet.dll it does'nt work as It scratch the remote file if already existing....

When using the _FtpCommand (same Dll) it doesn't work neither.

I have all write access to the remote server (when doing it manually from the dos command prompt it works...) and therefore I do not understant why it doesn't work using the FtpCommand function ?

Does anybody have an idea which could help me to go forward ?

Thanks,

FreeRider.

Here is the code I use... and before testing it (if you want to) you must have an open ftpsession and of course you must also close the session after funtion's end.

#region FtpAppendFile
;===============================================================================
; Function Name:    _FTPAppendFile()
; Description:    Reads data from a handle opened by _FTPOpenFile()
; Parameter(s):  $l_FTPSession - Handle returned by _FTPConnect
;                   $SourceData - Can be a array containing strings to write or a list of files (with full path) or just data in a variable (separated by a "|" pipe sign)
;                   $RemoteFile - Full path to the file on the ftp Server (file is created if not exist)
; Requirement(s):   _FtpCommand()
; Return Value(s):  On Success - 
;                  On Failure - A formated error message
; Author(s):        joeyb1275 ???
;                   Modified by Christophe Savard 03/03/2008
;=================================================================================
    Func _FTPAppendFile($l_FTPSession,$SourceData,$RemoteFile)
        Local $S_SourceData = ""
        
        If Not IsArray($SourceData) Then; Sourcedata is not a file list in an array or An array of data
            $S_SourceData = StringSplit($SourceData, "|",1); Put Sourcedata into an array
        EndIf
        
        For $i=1 To $S_SourceData[0]; Loop to build the temp file which will be used for data appending
            If FileExists($S_SourceData[$i]) Then; The source data is in a local file
                If StringRight(FileRead($S_SourceData[$i]),1) = @CRLF Or StringRight(FileRead($S_SourceData[$i]),1) = @CR Or StringRight(FileRead($S_SourceData[$i]),1) = @LF Then
                    FileWrite(@ScriptDir & "\TempoAppend.txt",FileRead($S_SourceData[$i])); Writes the file content to temp file w/o a line feed at the end
                Else
                    FileWriteLine(@ScriptDir & "\TempoAppend.txt",FileRead($S_SourceData[$i])); Writes the file content to temp file adding a line feed at the end
                EndIf
            Else; The source data is not a file but a string
                FileWriteLine(@ScriptDir & "\TempoAppend.txt",$S_SourceData[$i]); Writes the sting to temp file with line feed at the end
            EndIf
        Next
    
        $WrkDir=@WorkingDir; Record the actual WrkDir
        FileChangeDir ( @ScriptDir ); Set the current local directory to the actual script dir
        
    ;MsgBox(0,"", "Curr WinDir = " & @WorkingDir & @CRLF & "Ftp Dir = " & StringLeft($RemoteFile,StringInStr($RemoteFile,"/",0,-1)) & @CRLF & "FTP File = " & StringTrimLeft($RemoteFile,StringInStr($RemoteFile,"/",0,-1)))
        
        $ChangeDir_FtpCommand =  "CWD" & " " & StringLeft($RemoteFile,StringInStr($RemoteFile,"/",0,-1)); command line to move to the required dir
        $DirRes=_FTPCommand($l_FTPSession[2], $ChangeDir_FtpCommand,$INTERNET_FLAG_TRANSFER_ASCII, False) 
        $Append_FTPCommand = "APPE" & " " & "TempoAppend.txt" & " " & StringTrimLeft($RemoteFile,StringInStr($RemoteFile,"/",0,-1)); Command line to append file content to remote file
        $CmdRes=_FTPCommand($l_FTPSession[2], $Append_FTPCommand, $INTERNET_FLAG_TRANSFER_ASCII, False)
            
        If @error <> 0 Then 
            FileChangeDir ( $WrkDir); Restores the original Wrkdir
            FileDelete(@ScriptDir & "\TempoAppend.txt")
            Return _GetLastErrorMessage() & @CRLF & "Data :" & @CRLF & FileRead(@ScriptDir & "\TempoAppend.txt") & @CRLF & "Command Returned : " & $CmdRes
        EndIf
        FileDelete(@ScriptDir & "\TempoAppend.txt")
        FileChangeDir ( $WrkDir); Restores the original Wrkdir
        Return 1
    EndFunc
#endregion FtpAppendFile

FreeRiderHonour & Fidelity

Share this post


Link to post
Share on other sites
martin

Hi all,

I'm working on a peace of script for a while now and I'm looking for an easy way to append a local file content to a remote file based on a Ftp Server.

When doing it using "FtpWriteFile" function of the Wininet.dll it does'nt work as It scratch the remote file if already existing....

When using the _FtpCommand (same Dll) it doesn't work neither.

I have all write access to the remote server (when doing it manually from the dos command prompt it works...) and therefore I do not understant why it doesn't work using the FtpCommand function ?

Does anybody have an idea which could help me to go forward ?

Thanks,

FreeRider.

Here is the code I use... and before testing it (if you want to) you must have an open ftpsession and of course you must also close the session after funtion's end.

#region FtpAppendFile
;===============================================================================
; Function Name:    _FTPAppendFile()
; Description:    Reads data from a handle opened by _FTPOpenFile()
; Parameter(s):  $l_FTPSession - Handle returned by _FTPConnect
;                   $SourceData - Can be a array containing strings to write or a list of files (with full path) or just data in a variable (separated by a "|" pipe sign)
;                   $RemoteFile - Full path to the file on the ftp Server (file is created if not exist)
; Requirement(s):   _FtpCommand()
; Return Value(s):  On Success - 
;                  On Failure - A formated error message
; Author(s):        joeyb1275 ???
;                   Modified by Christophe Savard 03/03/2008
;=================================================================================
    Func _FTPAppendFile($l_FTPSession,$SourceData,$RemoteFile)
        Local $S_SourceData = ""
        
        If Not IsArray($SourceData) Then; Sourcedata is not a file list in an array or An array of data
            $S_SourceData = StringSplit($SourceData, "|",1); Put Sourcedata into an array
        EndIf
        
        For $i=1 To $S_SourceData[0]; Loop to build the temp file which will be used for data appending
            If FileExists($S_SourceData[$i]) Then; The source data is in a local file
                If StringRight(FileRead($S_SourceData[$i]),1) = @CRLF Or StringRight(FileRead($S_SourceData[$i]),1) = @CR Or StringRight(FileRead($S_SourceData[$i]),1) = @LF Then
                    FileWrite(@ScriptDir & "\TempoAppend.txt",FileRead($S_SourceData[$i])); Writes the file content to temp file w/o a line feed at the end
                Else
                    FileWriteLine(@ScriptDir & "\TempoAppend.txt",FileRead($S_SourceData[$i])); Writes the file content to temp file adding a line feed at the end
                EndIf
            Else; The source data is not a file but a string
                FileWriteLine(@ScriptDir & "\TempoAppend.txt",$S_SourceData[$i]); Writes the sting to temp file with line feed at the end
            EndIf
        Next
    
        $WrkDir=@WorkingDir; Record the actual WrkDir
        FileChangeDir ( @ScriptDir ); Set the current local directory to the actual script dir
        
;MsgBox(0,"", "Curr WinDir = " & @WorkingDir & @CRLF & "Ftp Dir = " & StringLeft($RemoteFile,StringInStr($RemoteFile,"/",0,-1)) & @CRLF & "FTP File = " & StringTrimLeft($RemoteFile,StringInStr($RemoteFile,"/",0,-1)))
        
        $ChangeDir_FtpCommand =  "CWD" & " " & StringLeft($RemoteFile,StringInStr($RemoteFile,"/",0,-1)); command line to move to the required dir
        $DirRes=_FTPCommand($l_FTPSession[2], $ChangeDir_FtpCommand,$INTERNET_FLAG_TRANSFER_ASCII, False) 
        $Append_FTPCommand = "APPE" & " " & "TempoAppend.txt" & " " & StringTrimLeft($RemoteFile,StringInStr($RemoteFile,"/",0,-1)); Command line to append file content to remote file
        $CmdRes=_FTPCommand($l_FTPSession[2], $Append_FTPCommand, $INTERNET_FLAG_TRANSFER_ASCII, False)
            
        If @error <> 0 Then 
            FileChangeDir ( $WrkDir); Restores the original Wrkdir
            FileDelete(@ScriptDir & "\TempoAppend.txt")
            Return _GetLastErrorMessage() & @CRLF & "Data :" & @CRLF & FileRead(@ScriptDir & "\TempoAppend.txt") & @CRLF & "Command Returned : " & $CmdRes
        EndIf
        FileDelete(@ScriptDir & "\TempoAppend.txt")
        FileChangeDir ( $WrkDir); Restores the original Wrkdir
        Return 1
    EndFunc
#endregion FtpAppendFile
I don't know the answer, but it looks like you have missed out a command

APPE

Syntax: APPE remote-filename

Append data to the end of a file on the remote host. If the file does not already exist, it is created. This command must be preceded by a PORT or PASV command so that the server knows where to receive data from.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
FreeRider

Hi Martin,

Thanks for your response time.... Howerver I also made a test with the PASV flag before to try the Append command... And it still doesn't work.

I think, like you do, that I missed something but don't know what... That's the problem.

FreeRider


FreeRiderHonour & Fidelity

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  

×