NeuroToxic Posted January 5, 2008 Share Posted January 5, 2008 (edited) Hi all, Sorry for my bad english, I'm french. I just finished 2 functions for FTP servers and I wanted to share it. _FTP_File_List_To_Array() : Lists all files and folders in a specified path on FTP server. _FTP_Delete_Recursive() : Delete all files and folders in a specified path on FTP server. If you see improvements. The code of the 2 functions : expandcollapse popup;=============================================================================== ; ; Function Name: _FTP_File_List_To_Array ; Description: Lists all files and folders in a specified path on FTP server. ; Syntax : _FTP_File_List_To_Array($l_FTPSession, $s_Remote) ; Parameter(s): $l_FTPSession - The Long from _FTPConnect() ; $s_Remote - The remote Location for the directory. ; Requirement(s): DllCall, wininet.dll, Array.au3. ; FTP.au3 of Wouter van Kesteren. ; Functions : _FtpFileFindFirst(), _FtpFileFindNext(), _FtpFileFindClose() of Dick Bronsdijk. ; Return Value(s): On Success - returns an array with list of directory and files. ; On Failure - returns False. ; Author(s): Neuro - Jérémy GUILLOT. ; ;=============================================================================== #include <Array.au3> Func _FTP_File_List_To_Array($l_FTPSession, $s_Remote = "/") Local $array[2] Local $array_end = UBound($array) Local $i Local $line Local $Handle Local $DllRect Local $j If $s_Remote == "" Then $s_Remote = "/" ElseIf $s_Remote <> "" And $s_Remote <> "/" Then If StringLeft($s_Remote, 1) <> "/" Then $s_Remote = "/" & $s_Remote EndIf If StringRight($s_Remote, 1) <> "/" Then $s_Remote = $s_Remote & "/" EndIf EndIf $array[0] = 1 $array[1] = "directory" & ";;;;;" & $s_Remote & ";;;;;" & "0" ;~ directory list While $i <= $array_end $line = StringSplit($array[$i], ";;;;;", 1) If IsArray($line) And $line[0] == 3 Then If $line[3] == 0 Then $j = 1 Local $FileInfo = _FtpFileFindFirst($l_FTPSession, $line[2] & "*", $Handle, $DllRect) If @error Then Return False If $FileInfo[0] Then Do ;~ If current is directory then insert directory in array If $FileInfo[1] == 16 And $FileInfo[10] <> "." And $FileInfo[10] <> ".." Then ; dir attrib = 16 ReDim $array[UBound($array)] $array_end = UBound($array) $array[0] += 1 _ArrayInsert($array, $i + $j, "directory" & ";;;;;" & $line[2] & $FileInfo[10] & "/" & ";;;;;" & "0") $j += 1 EndIf $FileInfo = _FtpFileFindNext($Handle, $DllRect) Until Not $FileInfo[0] EndIf _FtpFileFindClose($Handle, $DllRect) $array[$i] = "directory" & ";;;;;" & $line[2] & ";;;;;" & "1" $i = 1 EndIf ;~ ConsoleWrite("directory find : " & $line[2] & @CRLF) EndIf $i += 1 WEnd ;~ set all 1 in array to 0, to perform a new search For $i = 1 To UBound($array) - 1 $line = StringSplit($array[$i], ";;;;;", 1) If IsArray($line) And $line[0] == 3 Then If $line[3] <> 0 Then $array[$i] = "directory" & ";;;;;" & $line[2] & ";;;;;" & "0" EndIf EndIf Next ;~ files list $i = 1 While $i <= $array_end $line = StringSplit($array[$i], ";;;;;", 1) If IsArray($line) And $line[0] == 3 Then If $line[1] == "directory" And $line[3] == 0 Then $j = 1 $FileInfo = _FtpFileFindFirst($l_FTPSession, $line[2] & "*", $Handle, $DllRect) If @error Then Return False If $FileInfo[0] Then Do ;~ If current is file then insert the file in array If $FileInfo[1] == 128 Then ; file attrib = 128 ;~ ConsoleWrite("file find : " & $line[2] & $FileInfo[10] & @CRLF) ReDim $array[UBound($array)] $array_end = UBound($array) $array[0] += 1 _ArrayInsert($array, $i + $j, "file" & ";;;;;" & $line[2] & $FileInfo[10] & ";;;;;" & "1") $j += 1 EndIf $FileInfo = _FtpFileFindNext($Handle, $DllRect) Until Not $FileInfo[0] EndIf _FtpFileFindClose($Handle, $DllRect) $array[$i] = "directory" & ";;;;;" & $line[2] & ";;;;;" & "1" $i = 1 EndIf EndIf $i += 1 WEnd ;~ clean array For $i = 1 To UBound($array) - 1 $line = StringSplit($array[$i], ";;;;;", 1) If IsArray($line) And $line[0] == 3 Then $array[$i] = $line[2] EndIf Next Return $array EndFunc ;==> _FTP_File_List_To_Array ;=============================================================================== ; ; Function Name: _FTP_Delete_Recursive ; Description: Delete all files and folders in a specified path on FTP server. ; Syntax : _FTP_Delete_Recursive($l_FTPSession, $s_Remote) ; Parameter(s): $l_FTPSession - The Long from _FTPConnect() ; $s_Remote - The remote Location for the directory. ; Requirement(s): DllCall, wininet.dll, Array.au3. ; FTP.au3 of Wouter van Kesteren. ; Functions : _FtpFileFindFirst(), _FtpFileFindNext(), _FtpFileFindClose() of Dick Bronsdijk. ; Return Value(s): On Success - returns True. ; On Failure - returns False. ; Author(s): Neuro - Jérémy GUILLOT. ; ;=============================================================================== #include <Array.au3> Func _FTP_Delete_Recursive($l_FTPSession, $s_Remote) Local $array[2] Local $array_end = UBound($array) Local $i Local $line Local $Handle Local $DllRect Local $j Local $k If $s_Remote == "" Then $s_Remote = "/" $array[0] = $s_Remote & ";;;;;" & "0" $i = 0 ElseIf $s_Remote <> "" And $s_Remote <> "/" Then If StringLeft($s_Remote, 1) <> "/" Then $s_Remote = "/" & $s_Remote EndIf If StringRight($s_Remote, 1) <> "/" Then $s_Remote = $s_Remote & "/" EndIf $array[1] = $s_Remote & ";;;;;" & "0" $i = 1 Else $array[1] = $s_Remote & ";;;;;" & "0" $i = 1 EndIf ;~ directory list and files delete While $i <= $array_end $line = StringSplit($array[$i], ";;;;;", 1) If IsArray($line) And $line[0] == 2 Then If $line[2] == 0 Then $j = 1 $k = 0 Local $FileInfo = _FtpFileFindFirst($l_FTPSession, $line[1] & "*", $Handle, $DllRect) If @error Then Return False If $FileInfo[0] Then Do ;~ If current is directory then insert directory in array If $FileInfo[1] == 16 And $FileInfo[10] <> "." And $FileInfo[10] <> ".." Then ; dir attrib = 16 ;~ ConsoleWrite("dir find : " & $line[1] & $FileInfo[10] & "/" & @CRLF) ReDim $array[UBound($array)] $array_end = UBound($array) _ArrayInsert($array, $i + $j, $line[1] & $FileInfo[10] & "/" & ";;;;;" & "0") $j += 1 $k += 1 EndIf ;~ If current is file then delete the file If $FileInfo[1] == 128 Then ; file attrib = 128 ;~ ConsoleWrite("delete file : " & $line[1] & $FileInfo[10] & @CRLF) _FTPDelFile($l_FTPSession, $line[1] & $FileInfo[10]) If @error Then Return False $k += 1 EndIf $FileInfo = _FtpFileFindNext($Handle, $DllRect) Until Not $FileInfo[0] EndIf _FtpFileFindClose($Handle, $DllRect) If $array[$i] == $s_Remote & ";;;;;" & "0" And $k == 0 Then $array_end -= 1 EndIf $array[$i] = $line[1] & ";;;;;" & "1" $i = 0 EndIf EndIf $i += 1 WEnd ;~ delete all directory For $i = UBound($array) - 1 To 1 Step -1 $line = StringSplit($array[$i], ";;;;;", 1) If IsArray($line) And $line[0] == 2 Then ;~ ConsoleWrite("delete dir : " & $line[1] & @CRLF) _FTPDelDir($l_FTPSession, $line[1]) If @error Then Return False EndIf Next Return True EndFunc ;==> _FTP_Delete_Recursive This 2 functions needs Array.au3, FTP.au3 of Wouter and FTP functions of Dick Bronsdijk, I have make a new FTP.au3 with all this FTP functions (in attach).new_FTP.au3 Edited January 7, 2008 by NeuroToxic Link to comment Share on other sites More sharing options...
NeuroToxic Posted January 5, 2008 Author Share Posted January 5, 2008 Nobody is interested ? or to improve the code ? Link to comment Share on other sites More sharing options...
Zedna Posted January 5, 2008 Share Posted January 5, 2008 Nobody is interested ?or to improve the code ?I like improved FTP UDF version.Look here for correction of old bug. Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
NeuroToxic Posted January 5, 2008 Author Share Posted January 5, 2008 ok thank you, Can you test this 2 functions works for you ? The code of my two functions can be improved to run faster ? I'am a beginner in au3 script. Link to comment Share on other sites More sharing options...
NeuroToxic Posted January 7, 2008 Author Share Posted January 7, 2008 (edited) I have updated my _FTP_Delete_Recursive() function. -- Fix a delete bug when the specified path is empty Edited January 7, 2008 by NeuroToxic Link to comment Share on other sites More sharing options...
NeuroToxic Posted January 7, 2008 Author Share Posted January 7, 2008 -- Fix another delete bug, the code and the "new_FTP.au3" are updated Link to comment Share on other sites More sharing options...
wysocki Posted February 15, 2008 Share Posted February 15, 2008 (edited) I seem to be having a problem loading my array of files. After I connect to the ftp server, the following command successfully returns an array of files: $ftparray = _FTP_File_List_To_Array($conn, 'mydomain.com') (where mydomain.com is the root folder name). But if I specify a second level deep of folder name it fails like this example: $ftparray = _FTP_File_List_To_Array($conn, 'mydomain.com/video') I get an error in the new_ftp.au3 file on line (625) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.: [EDIT:] I found the problem and fixed it (?) myself. Obviously, since I didn't write the routine, there could be some problem with my fix, but so far it works fine for me. Basically, around line 625 there's a while loop that exceeded the array limits (don't know why it was setup like that). To fix it, I changed the "less than or equal" to just "less than". So I replaced: While $i <= $array_end with While $i < $array_end Edited February 20, 2008 by wysocki Link to comment Share on other sites More sharing options...
wysocki Posted February 21, 2008 Share Posted February 21, 2008 But it still crashes if there are no files in the target folder! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now