Jump to content

having issues with _FTP_FindFileFirst and _FTP_FindFileNext


Champak
 Share

Recommended Posts

I'm trying to delete files in a directory. There are multiple instances of files that have the file size appending the names. So I do a search of the primary name of the file without the appended portion or the extension.

1/ It always errors out on the second file name...upon the loop.

2/ It wont find the first file name (or if it is further down the line in the array) if there is another file with a single numeric value different. Ex: 

It wont find:

C3-0000086_431_Ralph_Lauren_Blue-White_T-Shirt_0

if there is a file with the name of: 

C3-0000086_431_Ralph_Lauren_Blue-White_T-Shirt_1

It will error out.

$s_cache_dir is a 1D array filled with file names without the appended file size and extensions

 

For $i = 0 To UBound($s_cache_dir)-1

    Local $h_Handle

    ConsoleWrite(@CRLF & @CRLF & "+ Looking for: " & $s_cache_dir[$i] & @CRLF)

    $s_image = _FTP_FindFileFirst ( $hConn, "*" & $s_cache_dir[$i] & "*", $h_Handle )
    If Not @error Then
        If StringLen($s_image[10]) > 2 Then
            ConsoleWrite("! deleting " & $s_image[10] & @CRLF)
            ;_FTP_FileDelete ( $hConn, "/" & $delete_FileFolders & "/" & $s_image[10] )
        EndIf
    Else
        _ArrayDisplay($s_image, "ERROR initial search: " & @error)
    EndIf

    ;_ArrayDisplay($s_image, "Find First")

    Do
        $s_image = _FTP_FindFileNext ( $h_Handle )
        If Not @error Then
            If StringLen($s_image[10]) > 2 Then
                ConsoleWrite("! deleting " & $s_image[10] & @CRLF)
                ;_FTP_FileDelete ( $hConn, "/" & $delete_FileFolders & "/" & $s_image[10] )
            EndIf
        EndIf
        ConsoleWrite("++++" & @CRLF)
    Until @error = -1

    ConsoleWrite(">>> " & $s_image & @CRLF)

Next

Any ideas?

EDIT: The only other way I can think of to do this is to take care of it in php, but I don't know how to pass an array from autoit by ways of url to a php page.

Edited by Champak
Link to comment
Share on other sites

You have to give us a specifc example. What exactly it's the value of $s_cache_dir[0] for example?

#include <FTPEx.au3>

Local $sServer = 'ftp.server.tld'
Local $sUsername = 'myusername'
Local $sPass = '********'

Local $hOpen = _FTP_Open('MyFTP Control')
Local $hConn = _FTP_Connect($hOpen, $sServer, $sUsername, $sPass, 1, 21)

Local $h_Handle
_FTP_DirSetCurrent ( $hConn, '/public_ftp' )
ConsoleWrite(_FTP_DirGetCurrent($hConn) & @CRLF)
Local $aFile = _FTP_FindFileFirst($hConn, "*C3-0000086_431_Ralph_Lauren_Blue-White_T-Shirt*", $h_Handle)
If Not @error Then
    ConsoleWrite( $aFile[10] & @CRLF)
    While True
        $aFile = _FTP_FindFileNext($h_Handle)
        If Not IsArray($aFile) Then ExitLoop
        If Not $aFile[0] Then ExitLoop
        ConsoleWrite( $aFile[10] & @CRLF)
    WEnd
    Local $iFindClose = _FTP_FindFileClose($h_Handle)
EndIf

Local $iFtpc = _FTP_Close($hConn)
Local $iFtpo = _FTP_Close($hOpen)

I created two files named C3-0000086_431_Ralph_Lauren_Blue-White_T-Shirt_0_2023_11_13.txt and C3-0000086_431_Ralph_Lauren_Blue-White_T-Shirt_1_2023_11_13.txt and I placed them in /public_ftp and run the code above. The result is as expected:

Quote

/public_ftp
C3-0000086_431_Ralph_Lauren_Blue-White_T-Shirt_0_2023_11_13.txt
C3-0000086_431_Ralph_Lauren_Blue-White_T-Shirt_1_2023_11_13.txt

 

BTW in your code you might end in trouble with this:

Do
    $s_image = _FTP_FindFileNext ( $h_Handle )
    If Not @error Then
        If StringLen($s_image[10]) > 2 Then
            ConsoleWrite("! deleting " & $s_image[10] & @CRLF)
            ;_FTP_FileDelete ( $hConn, "/" & $delete_FileFolders & "/" & $s_image[10] )
        EndIf
    EndIf
    ConsoleWrite("++++" & @CRLF)
Until @error = -1   ; <<< ------ This is not safe

but you can do it like this

Do
    $s_image = _FTP_FindFileNext ( $h_Handle )
    $iErrorCode = @error
    If Not $iErrorCode Then
        If StringLen($s_image[10]) > 2 Then
            ConsoleWrite("! deleting " & $s_image[10] & @CRLF)
            ;_FTP_FileDelete ( $hConn, "/" & $delete_FileFolders & "/" & $s_image[10] )
        EndIf
    EndIf
    ConsoleWrite("++++" & @CRLF)
Until $iErrorCode = -1  ; <<< ------ This is safe

 

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

The array is filled with the following minus the "--- " at the beginning of each line.

--- ----Little_Me_Green_Onesie_Very_Good_C2-0000080_2
--- ---Rae_Dunn_Yellow_Onesie_Very_Good_C2-0000083_2
--- A0-0000010_529_Unbranded_Pink_Accessories_0
--- A0-0000024_543_Unbranded_Blue_Accessories_0
--- A0-0000026_545_Unbranded_Multi-Color_Accessories_0
--- A0-0000027_546_Unbranded_Multi-Color_Accessories_0
--- A0-0000047_566_Unbranded_White_Accessories_0
--- A0-0000048_567_Unbranded_Black_Accessories_0
--- C3-0000082_427_Lee_Blue_Pants_0
--- C3-0000082_427_Lee_Blue_Pants_1
--- C3-0000085_430_Childrens_Place_Grey_T-Shirt_0
--- C3-0000085_430_Childrens_Place_Grey_T-Shirt_1
--- C3-0000086_431_Ralph_Lauren_Blue-White_T-Shirt_0
--- C3-0000086_431_Ralph_Lauren_Blue-White_T-Shirt_1
--- C5-0000001_447_Circo_Blue_Shorts_0
--- C5-0000001_447_Circo_Blue_Shorts_1
--- C5-0000002_446_Childrens_Place_Brown_Pants_0
--- C5-0000002_446_Childrens_Place_Brown_Pants_1
--- C5-0000003_448_Ralph_Lauren_Brown_Shorts_0
--- C5-0000003_448_Ralph_Lauren_Brown_Shorts_1
--- C5-0000004_449_Unbranded_Red_Shorts_0
--- C5-0000004_449_Unbranded_Red_Shorts_1
--- C5-0000005_450_Unbranded_Brown_Shorts_0
--- C5-0000005_450_Unbranded_Brown_Shorts_1
--- C5-0000006_451_Primark_Brown_Shorts_0
--- C5-0000006_451_Primark_Brown_Shorts_1
--- C5-0000007_452_Zara_Brown_Shorts_0
--- C5-0000007_452_Zara_Brown_Shorts_1
--- C5-0000008_453_Unbranded_Blue_Shorts_0
--- C5-0000008_453_Unbranded_Blue_Shorts_1
--- C5-0000009_454_HAndM_Blue_Dress_0
--- C5-0000009_454_HAndM_Blue_Dress_1
--- C5-0000010_455_Nautica_Blue-Grey_Pants_0
--- C5-0000010_455_Nautica_Blue-Grey_Pants_1
--- C5-0000011_457_Unbranded_Multi-Color_Sleeper_0
--- C5-0000011_457_Unbranded_Multi-Color_Sleeper_1
--- C5-0000012_458_Carters_Pink_Sweater_0
--- C5-0000012_458_Carters_Pink_Sweater_1
--- C5-0000013_459_Unbranded_Blue_Set_0
--- C5-0000013_459_Unbranded_Blue_Set_1
--- C5-0000014_460_Little_Me_Blue-White_Set_0
--- C5-0000014_460_Little_Me_Blue-White_Set_1
--- F1G-0000019_358_Carters_White_Footwear_0
--- F1G-0000019_358_Carters_White_Footwear_1
--- F1G-0000027_366_Unbranded_Brown_Footwear_0
--- F1G-0000027_366_Unbranded_Brown_Footwear_1
--- F1G-0000028_367_Unbranded_White_Footwear_0
--- F1G-0000028_367_Unbranded_White_Footwear_1
--- F1G-0000032_382_Smart_Fit_White_Footwear_0
--- F1G-0000032_382_Smart_Fit_White_Footwear_1
--- F1G-0000035_385_Pazitos_Green-Brown_Footwear_0
--- F1G-0000035_385_Pazitos_Green-Brown_Footwear_1
--- F1G-0000037_389_Garanimals_White_Footwear_0
--- F1G-0000037_389_Garanimals_White_Footwear_1
--- F1G-0000038_390_Garanimals_White_Footwear_0
--- F1G-0000038_390_Garanimals_White_Footwear_1
--- Gap_Grey_Romper_Good_C0-0000031_0
--- Gap_Grey_Romper_Good_C0-0000031_1
--- Gerber_Yellow_Set_Very_Good_C0-0000032_0
--- Gerber_Yellow_Set_Very_Good_C0-0000032_1
--- Gymboree_Beige_Romper_Excellent_C0-0000009_0
--- Gymboree_Beige_Romper_Excellent_C0-0000009_1
--- Juicy_Couture_Blue_Sweater_Very_Good_C1-0000002
--- Juicy_Couture_Blue_Sweater_Very_Good_C1-0000002_2
--- Zara_Black_Outerwear_New_With_Tag_J1-0000011
--- Zara_Black_Outerwear_New_With_Tag_J1-0000011_2
--- Zara_Blue_Pants_Excellent_C2-0000099_1
--- Zara_Pink_Outerwear_Very_Good_J1-0000010
--- Zara_Pink_Outerwear_Very_Good_J1-0000010_2

 

 

 

Link to comment
Share on other sites

If you are looking for *--- C3-0000086_431_Ralph_Lauren_Blue-White_T-Shirt_0* but the file name on server is something like  C3-0000086_431_Ralph_Lauren_Blue-White_T-Shirt_0-3000x3000h.jpg why would you expect to find the file? Why don't you trim these --- and the space after?

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

Four posts earlier I stated the "--- " on each line was not a part of the array/search, that's just a part of my consolewrite. The search is just *C3-0000086_431_Ralph_Lauren_Blue-White_T-Shirt_0*. I don't think I need the first "*", it's just there because of a previous iteration. I left it because it wasn't hurting anything.

Link to comment
Share on other sites

1 hour ago, Champak said:

The array is filled with the following minus the "--- " at the beginning of each line.

2 minutes ago, Champak said:

Four posts earlier I stated the "--- " on each line was not a part of the array/search, that's just a part of my consolewrite.

You are not helpful if you don't provide precise information. English is not my main language but I'm pretty sure that "array is filled with the following minus" it means these minuses are part of the array content.

Make sure you are in the right directory by using

ConsoleWrite(_FTP_DirGetCurrent($hConn) & @CRLF)

then check what error code is returned after you call _FTP_FindFileFirst(). You can also get all files from directory into an array with _FTP_ListToArray() and check if your files are there and check their names.

If nothing works then isolate your files, create a new directory somewhere on the server and move just these two files and try the steps above.

 

When the words fail... music speaks.

Link to comment
Share on other sites

2 minutes ago, Andreik said:

English is not my main language but I'm pretty sure that "array is filled with the following minus" it means these minuses are part of the array content.

LOL, yes, that could be taken that way, but you left out the very next word "the" which would be the operative word to make my point. But English is an unnecessarily complicated language.

I'm in the right directory because other files are being found (sorry I wasn't specific about these).....files that don't have a duplicate with "_1" at the end.

So it's found when it's just

*C3-0000086_431_Ralph_Lauren_Blue-White_T-Shirt_0*

But when it's both:

*C3-0000086_431_Ralph_Lauren_Blue-White_T-Shirt_0*

*C3-0000086_431_Ralph_Lauren_Blue-White_T-Shirt_1*

Neither is found.

So the first 6 files happen to just be "_0" and they are all found until it reaches the files with both "_0" and "_1", so no issue with directory...as far as I see.

I can't use _FTP_ListToArray(), two many files in the dir, ftp is capped at 8000. I'm getting my listing through a php function and then scraping the page. But either way, I have verified all the files in question are in the dir.

The @error is "-1", if you want the information in the error array see attached.

Screenshot 2023-11-12 224056ERROR.jpg

Link to comment
Share on other sites

I just tested my theory and deleted one of the paired files and all iterations with the ending of "_1" and ran the function and it still didn't see the version of "_0". So it isn't the pairing causing the issue, so now I'm at a loss because I don't see any other differences between what's able to be found and what isn't. I'm going to dig around this a little more, but it may be easier for me to just throw this to a php function to delete the files because I'm not seeing anything.

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...