Jump to content
Sign in to follow this  
darkjohn20

Recursive FTP search

Recommended Posts

darkjohn20

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.

Share this post


Link to post
Share on other sites
grasshopper3

are u looking for one that looks for a certain file or file type or one that list all the files that are in the FTP dir?

Share this post


Link to post
Share on other sites
darkjohn20

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

Share this post


Link to post
Share on other sites
Yashied

#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

Share this post


Link to post
Share on other sites
Yashied

Share this post


Link to post
Share on other sites
darkjohn20

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

Share this post


Link to post
Share on other sites
Yashied

Share this post


Link to post
Share on other sites
darkjohn20

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

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.