Sign in to follow this  
Followers 0
Michiel78

Delete specific files on FTP

11 posts in this topic

Part of the program I'm writing is supposed to delete files on my ftp server that do not have the extension .htm. I've tried to achieve this with the functions _FTP_FileFindFirst, _FTP_FileFindNext and StringRight, but it doesn't work at all. Working this out for a normal folder is no problem, but I'm new to the _FTP functions so could anyone help me out with this one?

This is the part of the code I'm talking about:

$nonhtm = _FTP_FindFileFirst($Conn, $ftpfolder & "/*.*", $h_Handle)
If $aFile = -1 Then
    MsgBox(0, "Error", "No files were found")
    Exit
EndIf
While 1
    $nonhtm = FileFindNextFile($h_Handle)
If @error Then ExitLoop

If StringRight($nonhtm, 4) <> "/*.htm*" Then
    _FTP_FileDelete($Conn, $ftp & "/" & $nonhtm)
EndIf

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Try _FTP_FindFileNext instead of FileFindNextFileiPosted Image Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Try _FTP_FindFileNext instead of FileFindNextFileiPosted Image

Thanks for the tip ;) ! I just noticed that it wasn't my most recent version of the code, here is the version it should be:

$nonhtm = _FTP_FindFileFirst($Conn, $ftpfolder & "/*.*", $h_Handle)
If $aFile = -1 Then
    MsgBox(0, "Error", "No files were found")
    Exit
EndIf
While 1
    $nonhtm = _FTP_FindFileNext($h_Handle)

If StringRight($nonhtm, 4) <> "/*.htm*" Then
    _FTP_FileDelete($Conn, $ftpfolder & "/" & $nonhtm)
EndIf

Although this one has _FTP_FindFileNext and the right remotefolder for _FTP_FileDelete, it still doesn't work :) . With another script I've tried to check if the problem isn't the ftpserver, but it succesfully connected and was able to find files within the folder. Deleting the non-htm files still doesn't work however.

Edited by Michiel78

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

May be there is an error in "If StringRight($nonhtm, 4) <> "/*.htm*" Then"

because /*.htm* is 7 characters !

Try with only ".htm" Posted Image

Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Author, please post your script coz I wondering if there is a error in your script in ftp connection handle. I think you try to use functions while ftp connection is already ended or didn't started at all.

Edited by electrico

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Author, please post your script coz I wondering if there is a error in your script in ftp connection handle. I think you try to use functions while ftp connection is already ended or didn't started at all.

This is the full FTP part of the script:

#include <FTPEx.au3>

$ftp_server = "ftp.example.com"
$ftp_username = "username"
$ftp_password = "password"
$ftpfolder = "html"

Local $ftp_session
Local $h_Handle

$ftp_session = _FTP_Open("image_session")
$ftp_CS = _FTP_Connect($ftp_session, $ftp_server, $ftp_username, $ftp_password)

$nonhtm = _FTP_FindFileFirst($ftp_CS, $ftpfolder & "/*.*", $h_Handle)
If $nonhtm = -1 Then
    MsgBox(0, "Error", "No files were found")
    Exit
EndIf
While 1
    $nonhtm = _FTP_FindFileNext($h_Handle)

If StringRight($nonhtm, 4) <> ".htm" Then
    _FTP_FileDelete($ftp_CS, $nonhtm)
EndIf
WEnd
_FTP_Close($ftp_session)
Edited by Michiel78

Share this post


Link to post
Share on other sites

#include <FTPEx.au3>

$ftp_server = "ftp.example.com"
$ftp_username = "username"
$ftp_password = "password"
$ftpfolder = "html"

Local $ftp_session
Local $h_Handle

$ftp_session = _FTP_Open("image_session")
$ftp_CS = _FTP_Connect($ftp_session, $ftp_server, $ftp_username, $ftp_password)

$nonhtm = _FTP_FindFileFirst($ftp_CS, $ftpfolder & "/*.*", $h_Handle)

If $nonhtm = -1 Then
    MsgBox(0, "Error", "No files were found")
    Exit
EndIf

While 1
    $nonhtm = _FTP_FindFileNext ( $h_Handle )
    If StringRight ( $nonhtm, 4 ) <> ".htm" Then 
        ConsoleWrite ( "!>---- $nonhtm : " & $nonhtm & @Crlf )
        _FTP_FileDelete ( $ftp_CS, $nonhtm )
    EndIf
WEnd
_FTP_Close($ftp_session)

try and say if there is something write in your scite console for know what function is in error Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

#include <FTPEx.au3>

$ftp_server = "ftp.example.com"
$ftp_username = "username"
$ftp_password = "password"
$ftpfolder = "html"

Local $ftp_session
Local $h_Handle

$ftp_session = _FTP_Open("image_session")
$ftp_CS = _FTP_Connect($ftp_session, $ftp_server, $ftp_username, $ftp_password)

$nonhtm = _FTP_FindFileFirst($ftp_CS, $ftpfolder & "/*.*", $h_Handle)

If $nonhtm = -1 Then
    MsgBox(0, "Error", "No files were found")
    Exit
EndIf

While 1
    $nonhtm = _FTP_FindFileNext ( $h_Handle )
    If StringRight ( $nonhtm, 4 ) <> ".htm" Then 
        ConsoleWrite ( "!>---- $nonhtm : " & $nonhtm & @Crlf )
        _FTP_FileDelete ( $ftp_CS, $nonhtm )
    EndIf
WEnd
_FTP_Close($ftp_session)

try and say if there is something write in your scite console for know what function is in error Posted Image

It seems to be stuck in an endless loop, for the console keeps writing:

>Running:(3.3.6.1):C:\Program Files\AutoIt3\autoit3.exe "C:\Users\Michiel\_FTP_FindFileNext.au3"    
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
!>---- $nonhtm : 
etc

This is while there are only 6 testfiles in the folder, of which 1 is non-htm. Also this file is not deleted during the endless loop, so I'm still not sure what the problem is. Probably something is causing the endless loop and something in the script causes the files not to be deleted. Anyone knows what it may be ;) ?

Edited by Michiel78

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Sorry for the delay , try this

#include <Array.au3>
#include <FTPEx.au3>

$ftp_server = "ftp.example.com"
$ftp_username = "username"
$ftp_password = "password"
$ftpfolder = "html"

Local $ftp_session
Local $h_Handle

$ftp_session = _FTP_Open("image_session")
$ftp_CS = _FTP_Connect($ftp_session, $ftp_server, $ftp_username, $ftp_password)

$nonhtm = _FTP_FindFileFirst ( $ftp_CS, $ftpfolder & "/*.*", $h_Handle )

If Not @error Then 
    _ArrayDisplay ( $nonhtm, '$nonhtm' )
Else
    MsgBox ( 0, "Error", "No files were found" )
    Exit
EndIf

While 1
    $nonhtm = _FTP_FindFileNext ( $h_Handle ) ; return an array !
    If StringRight ( $nonhtm[10] , 4 ) <> ".htm" Then 
        ConsoleWrite ( "!>---- $nonhtm[10]  : " & $nonhtm[10]  & @Crlf )
        _FTP_FileDelete ( $ftp_CS, $nonhtm[10] )
    EndIf
WEnd

_FTP_Close($ftp_session)

I think your error is in FTP_FindFileNext because it return an array ! Posted Image

Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

@Wakillon:

Your script didn't work immediately, but after changing some things in the code it now works like a charm!

The script that did it:

#include <FTPEx.au3>

$ftp_server = "ftp.example.com"
$ftp_username = "username"
$ftp_password = "password"
$ftpfolder = "test"

Local $ftp_session
Local $h_Handle

$ftp_session = _FTP_Open("image_session")
$ftp_CS = _FTP_Connect($ftp_session, $ftp_server, $ftp_username, $ftp_password)

$nonhtm = _FTP_FindFileFirst ( $ftp_CS, $ftpfolder & "/*.*", $h_Handle )

While 1
    $nonhtm = _FTP_FindFileNext ( $h_Handle )
    If StringLen($nonhtm[10]) > 2 AND StringRight ( $nonhtm[10] , 4 ) <> ".htm" Then
        _FTP_FileDelete ( $ftp_CS, "/" & $ftpfolder & "/" & $nonhtm[10] )
    EndIf
WEnd

_FTP_Close($ftp_session)

You were right about the array it returns, I didn't think of that and so forgot the [10] behind $nonhtm. Another thing I forgot was to put "/" & $ftpfolder & "/" & in the _FTP_FileDelete line, since only the filename+extension is returned and not the full path. Without your help it wouldn't have worked, so thank you very much ;) !

There is only one thing I still wonder, when I launch the script the console gives the following error:

C:\Users\Michiel\_FTP_FindFileNext.au3 (18) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
If StringLen($nonhtm[10]) > 2 AND StringRight ( $nonhtm[10] , 4 ) <> ".htm" Then
If StringLen(^ ERROR

Usualy I can fix this error by putting ExitLoop after the first If criteria it should meet, but now with the AND (two if statements) it somehow doesn't work. Does anyone know how to fix this error?

[update]: I inserted "If @error Then ExitLoop" to end the While loop when no more files can be found, the script now fully works!

Edited by Michiel78

Share this post


Link to post
Share on other sites

You were right about the array it returns, I didn't think of that and so forgot the [10] behind $nonhtm. Another thing I forgot was to put "/" & $ftpfolder & "/" & in the _FTP_FileDelete line, since only the filename+extension is returned and not the full path. Without your help it wouldn't have worked, so thank you very much ;) !

[update]: I inserted "If @error Then ExitLoop" to end the While loop when no more files can be found, the script now fully works!

I'm glad to help you ! Posted Image

While 1
    $nonhtm = _FTP_FindFileNext ( $h_Handle )
    If Not @error Then
        If StringLen($nonhtm[10]) > 2 AND StringRight ( $nonhtm[10] , 4 ) <> ".htm" Then
            _FTP_FileDelete ( $ftp_CS, "/" & $ftpfolder & "/" & $nonhtm[10] )
        EndIf
    Else
        ExitLoop
    EndIf
WEnd

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

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  
Followers 0