Jump to content
Sign in to follow this  

FTP function with recursive call

Recommended Posts



I'm trying to write a function ("_FTPFolderTree") to get all accessable (Sub-)Folders from a FTP-Server.

What it should do:

- switch the directory with _FTPSelectFolder() (to check if it's readable)

- if accessable:

--- store the foldername (as a string, seperated by @LF)

--- search for subfolders with _FTPFileFindFirst() and _FTPFileFindNext()

--- recursively call the function for each subfolder, storing the return in the seperated string

- return the string containing the foldernames

Unfortunately the recursive call doesn't seem to work correct. The function returns only the first sublevel of folders without going deeper. I tried a lot :whistle:, but these damned recursive calls... now it looks like I need some educated help :)

The problem might also be in my way to use _FTPFileFindFirst() or _FTPFileFindNext(). I also had an eye on the DLLStruct (not familiar with that - what does that do?) and the searchhandle . I tried to rewrite my function, so the searchhandle is closed before the subfolders are searched, but this only blasted the running time and returned strange things... :lol:

Here is a dummy file including the function and a main program trying to read from ftp.mcafee.com:

(other FTP-functions in the file are taken from FTP.au3 by Wouter van Kesteren, Beast and Dick Bronsdijk, but slightly changed to work with the opened wininet.dll)

_FTPFolderTree_Dummy.au3 (AutoIt Version: (beta))

Here is the source for the function:

func _FTPFolderTree($l_FTPSession, $s_Remote = "") 
    ; on success: returns string of readable folders (seperated by @LF)
    ; on failure: returns empty string and sets error to 1
    ; $l_FTPSession  - The Long from _FTPConnect()
    ; $s_Remote      - the foldername from where to search the subfolders - empty string = root folder
    if StringLeft($s_Remote,1) = "/" then $s_Remote = StringTrimleft($s_Remote,1)
    ;try to set the $s_remote directory (check for FTP-read-access)
    _FtpSetCurrentDir($l_FTPSession, "/" & $s_Remote)
    if @error Then   ; no read access , return empty string
        return ""
    Local $l_handle 
    Local $dll_struct
    Local $ret_folders = "" 
    ;store $s_Remote
    $ret_folders = $ret_folders & "/" & $s_Remote & @LF
    ;search for subfolders in $s_Remote
    Local $ret_array = _FTPFileFindFirst($l_FTPSession, "*.*", $l_handle, $dll_struct)
    If @error = 0 Then
        if ($ret_array[1] = 16) and ($ret_array[10] <> ".") and ($ret_array[10] <> "..") Then 
            ; _FTPFileFindFirst has returned a subfolder
            ;recursive function call, pass subfolder as $s_Remote
            $ret_folders = $ret_folders & _FTPFolderTree($l_FTPSession,$s_Remote & "/" & $ret_array[10] )
            $ret_array = _FTPFileFindNext($l_handle, $dll_struct)
            If $ret_array[0] <> 0 Then
                if ($ret_array[1] = 16) and ($ret_array[10] <> ".") and ($ret_array[10] <> "..") Then 
                    ; _FTPFileFindNext has returned a subfolder
                    ; recursive function call, pass subfolder as $s_Remote                  
                    $ret_folders = $ret_folders & _FTPFolderTree($l_FTPSession,$s_Remote & "/" & $ret_array[10] )
        Until $ret_array[0] = 0  ; _FTPFileFindNext does not find anything more
    _FTPFileFindClose($l_handle, $dll_struct)
    return $ret_folders

Thank you for reading my awkward english to the end, I hope someone has a good idea (I trust in you :-)


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.