Jump to content

Recursive FTP search


Recommended Posts

I searched the forums a little, but I couldn't find any recursive FTP file/folder searches. I was wondering if anybody has one? Perhaps I just couldn't find it?

I'm bad with recursion so maybe somebody else could write one faster than I could, but if not I can try my best.

I'm not sure what a quick or efficient way to do this would be, so any help is appreciated.

Link to comment
Share on other sites

I was looking for one that would list all files, in other words no "include list".

I plan to modify it so that a function is called as soon as a file/folder is found, rather than doing it after the function returned.

Meaning: If a file is found: Call _FTP_Download(), but if a folder is found: CreateDir(), or something similar.

That's my goal. Download everything under a certain path to the desktop, maintaining all file structures.

And really, I'm looking at less than 100 folders, most likely less than 100 files. Speed isn't a large issue, but the faster the better. :idea:

I also don't need a full-out UDF. As long as it works, I don't need much error checking or documentation. A few comments would be nice, but that's it.

Edited by darkjohn20
Link to comment
Share on other sites

#Include <FTPEx.au3>

_FTP_Download('', @ScriptDir & '\Uploaded', 'ftp.mozilla.org')

Func _FTP_Download($sRemotePath, $sLocalPath, $sHost, $sLogin = '', $sPassword = '', $iPassive = 1, $iPort = 0)

    Local $hFtp, $hSession, $aFind, $hFind, $Path, $Result = 1

    DirCreate($sLocalPath)
    If (Not FileExists($sLocalPath)) Or (Not StringInStr(FileGetAttrib($sLocalPath), 'D')) Then
        Return 0
    EndIf
    $hFtp = _FTP_Open('MyFtp')
    $hSession = _FTP_Connect($hFtp, $sHost, $sLogin, $sPassword, $iPassive, $iPort)
    $aFind = _FTP_FindFileFirst($hSession, $sRemotePath, $hFind)
    While Not @error
        $Path = $sLocalPath & '\' & $aFind[10]
        If BitAND($aFind[1], $FILE_ATTRIBUTE_DIRECTORY) Then
            DirCreate($Path)
            If (Not FileExists($Path)) Or (Not StringInStr(FileGetAttrib($Path), 'D')) Then
                Return 0
            EndIf
            If Not _FTP_Download($sRemotePath & '/' & $aFind[10], $Path, $sHost, $sLogin, $sPassword, $iPassive, $iPort) Then
                Return 0
            EndIf
        Else
            ConsoleWrite($sLocalPath & '\' & $aFind[10] & @CR)
;           If Not _FTP_FileGet($hSession, $sRemotePath & '/' & $aFind[10], $Path) Then
;               $Result = 0
;           EndIf
        EndIf
        $aFind = _FTP_FindFileNext($hFind)
    WEnd
    _FTP_FindFileClose($hFind)
    _FTP_Close($hFtp)
    Return $Result
EndFunc   ;==>_FTP_Download

Link to comment
Share on other sites

Link to comment
Share on other sites

You sure?

Oops, I removed the line to recurse. I tried this and it still didn't work...

#include-once
#include <FTPEx.au3>

Func _FTP_Recurse($hSession, $sRemotePath, $sLocalPath = @DesktopDir)
    Local $aFind, $hFind, $sPath, $iResult = 1
    If StringRight($sLocalPath, 1) <> "\" Then $sLocalPath &= "\"
    If StringRight($sRemotePath, 1) <> "/" Then $sRemotePath &= "/"

    DirCreate($sLocalPath)
    _Splash_Add("Downloading...")
    $aFind = _FTP_FindFileFirst($hSession, $sRemotePath, $hFind)
    While Not @error
    $sPath = $sLocalPath & $aFind[10]

    If BitAND($aFind[1], $FILE_ATTRIBUTE_DIRECTORY) Then
    DirCreate($sPath)
            _Splash_Add("Dir: " & $sPath)
            _FTP_Recurse($hSession, $sRemotePath & $aFind[10], $sPath)
    Else
            _Splash_Add("File: " & $sPath)
            _FTP_FileGet($hSession, $sRemotePath & $aFind[10], $sPath)
    EndIf

    $aFind = _FTP_FindFileNext($hFind)
    WEnd

    _FTP_FindFileClose($hFind)
    Return $iResult
EndFunc
Edited by darkjohn20
Link to comment
Share on other sites

Link to comment
Share on other sites

You can not open more than one search per session. For each search, you should create a new connection. I gave you a working example...

:)

Ahh, my bad. I see what's wrong. Thanks. :idea:

The only reason I removed that from mine was because I had more FTP actions in my main script.

Edited by darkjohn20
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...