Jump to content

Sync folder to FTP


Recommended Posts

Hi,

I just created a script to automatically sync my ftp server.

As help I used the FTP.au3.

But I dont know how to upload an entire folder. Anyone got a clue how I can do that?

PS: Yes, I only want a one-way-sync. Means I dont care what happens on the server, it should delete what happened there.

My script is also attached.

FTP.au3

FTP Sync.au3

Link to comment
Share on other sites

Check out _FTP_DirPutContents() in the FTPEx include. It should do what you need.

Link to comment
Share on other sites

Hmm.

It doesnt work, here's my script:

#include <FTPEx.au3>
#Include <Timers.au3>


Local $dir= "C:\...\html" ;removed, but the directory is really called "html".
Local $server = "ftp.leet-skills.com"
Local $username = "" ;removed.
Local $pass = "";removed.

Local $show = true

HotKeySet ("{F4}", "_sync")


Local $timer = TimerInit()
Local $dif



_sync()


Func _sync()
    
    
if $show = true then
ToolTip("Starting Sync...",0,0)
endif
$Open = _FTP_Open('LS_FTP')
$Conn = _FTP_Connect($Open, $server, $username, $pass)




_Ftp_DirPutContents($Open, $dir, '/html/', 1)





$Ftpc = _FTP_Close($Open)

if $show = true then
ToolTip("Sync done!",0,0)
Sleep(2000)
ToolTip("")
endif

EndFunc



While True
$dif = TimerDiff($timer)
If $dif > 10000 Then
_sync()
$timer = TimerInit()
Endif
WEnd
Link to comment
Share on other sites

I now found out the error.

I dont have the permission to delete the HTML folder. But I have permission to delete all the files it contains. So my script somehow has to find all the files it has and then delete them....

How can I do that? ;)

Here's my working script:

#include <FTPEx.au3>
#Include <Timers.au3>

Local $dir = '...\html'
Local $server = 'ftp.leet-skills.com'
Local $username = '...'
Local $pass = '...'

Local $show = true

HotKeySet ("{F4}", "_sync")


Local $timer = TimerInit()
Local $dif

Local $h_Handle

_sync()


Func _sync()
    
    
if $show = true then
ToolTip("Starting Sync...",0,0)
endif
$Open = _FTP_Open('LS_FTP')

$Conn = _FTP_Connect($Open, $server, $username, $pass)




_FTP_DirPutContents($Conn, $dir, '/html', 1)
;^this works, but it doesnt delete old files.
; Overwriting works.


$Ftpc = _FTP_Close($Open)


if $show = true then
ToolTip("Sync done!",0,0)
Sleep(2000)
ToolTip("")
endif

EndFunc



While True
$dif = TimerDiff($timer)
If $dif > 10000 Then
_sync()
$timer = TimerInit()
Endif
WEnd
Edited by LeetSkillsCom
Link to comment
Share on other sites

I've ran into this problem before. I don't think its a permission problem, its that you can't delete a directory that has files or other directorys inside of it. I bet if you try you can delete an empty directory. Heres a function and small example to help with the recursive deleting.

#include <FTPEx.au3>

$dir = '/StartDir/Recursive Delete'
$server = '192.168.1.101'
$username = ''
$pass = ''

$Open = _FTP_Open('test')
$Conn = _FTP_Connect($Open, $server, $username, $pass)
If Not @error Then ConsoleWrite('Connected!' & @CRLF)

_FTP_DirSetCurrent($Conn, '/StartDir/');Very Important to set the current dirctory to the directory that the folder you want to delete is in.
_FTPRemovedir($Conn, $dir)
_FTP_Close($Open)

Func _FTPRemovedir($l_FTPSession, $sFTPDirectory)
    Local $aFile, $hSearch, $sWorkingdir, $sFolderlist, $i, $bFirst, $aFolderStack[2] = [1, $sFTPDirectory]
    While $aFolderStack[0] > 0
        $sWorkingdir = $aFolderStack[$aFolderStack[0]]
        $aFolderStack[0] -= 1
        $aFile = _FTP_FindFileFirst($l_FTPSession, $sWorkingdir & '/*', $hSearch, $INTERNET_FLAG_NO_CACHE_WRITE)
        If Not @error Then
            $bFirst = True
            While 1
                If $bFirst = False Then
                    $aFile = _FTP_FindFileNext($hSearch)
                    If @error Then ExitLoop
                EndIf
                If $aFile[1] = 16 Then; If file is a directory we are going to add it to the stack of folders we need to go through
                    $aFolderStack[0] += 1
                    If UBound($aFolderStack) <= $aFolderStack[0] Then ReDim $aFolderStack[UBound($aFolderStack) * 2]
                    $aFolderStack[$aFolderStack[0]] = $sWorkingdir & "/" & $aFile[10]
                    $sFolderlist &= $sWorkingdir & "/" & $aFile[10] & ';'; Here I am adding the folder to a list of directorys I need to delete later
                Else; else we delete it
                    _FTP_FileDelete($l_FTPSession, $sWorkingdir & "/" & $aFile[10])
                    ConsoleWrite('File: ' & $sWorkingdir & "/" & $aFile[10] & ' Deleted' & @CRLF)
                EndIf
                $bFirst = False
            WEnd
        EndIf
        _FTP_FindFileClose($hSearch)
    WEnd
    $aFolderStack = StringSplit(StringTrimRight($sFolderlist, 1), ';')
    For $i = $aFolderStack[0] To 1 Step -1 ;Here we delete all those empty folders from last to first
        _FTP_DirDelete($l_FTPSession, $aFolderStack[$i])
        ConsoleWrite('Directory: ' & $aFolderStack[$i] & ' Deleted' & @CRLF)
    Next
    _FTP_DirDelete($l_FTPSession, $sFTPDirectory);Delete the original directory
EndFunc   ;==>_FTPRemovedir
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...