VB.NET Examples

ChilkatHOMEAndroid™ASPVisual BasicVB.NETC#iOS (IPhone)Objective-CC++CMFCDelphiFoxProJavaPerl
PHP ExtensionPHP ActiveXPythonPowerShellRubySQL ServerVBScript

VB.NET Examples

Bounced Mail
Bz2
Character Encoding
CSV
Digital Certificates
Digital Signatures
Email
FTP
HTML Conversion
HTTP
IMAP
Encryption
MHT / HTML Email
POP3
PFX
RSA
S/MIME
SMTP
Socket
Spider
SSH
SSH Tunnel
SSH Key
SFTP
Tar Archive
Upload
XML
XMP
Zip Compression
Misc

More Examples...
Amazon S3
Email Object
DKIM / DomainKey
NTLM
FileAccess
RSS
Atom
Self-Extractor
Service
PPMD
Deflate
DH Key Exchange
DSA
Bzip2
LZW

Byte Array
VB.NET FTPS
System.IO

 

 

 

 

 

 

Delete Remote FTP Directory Tree

Download: Chilkat .NET Assemblies

VB.NET sample code to delete an entire directory tree from an FTP server.

    ' Delete an entire remote directory tree on an FTP server.

    '
    ' The delete is abortable at any point via the AbortCheck event callback.
    ' 
    ' Two other event callbacks are available:

    ' OnVerifyDeleteFile - called before each file is deleted, allowing
    ' your application to skip specific files.

    ' OnVerifyDeleteDir - called before each remote sub-directory is entered,
    ' allowing your application to skip entire sub-directories.

    ' Don't forget to use WithEvents!
    Dim WithEvents ftpObj As New Chilkat.Ftp2
    Dim AbortButtonPressed As Boolean

    ' This is the OnAbortCheck event callback.  It is called periodically according
    ' to the HeartbeatMs property setting.
    Private Sub ftpObj_OnAbortCheck(ByVal sender As Object, ByVal args As Chilkat.AbortCheckEventArgs) Handles ftpObj.OnAbortCheck

        ' Was the Abort button pressed?
        If (AbortButtonPressed) Then
            ' To abort the current FTP operation, set Abort = true
            args.Abort = True
        End If

        ' Update a progress bar so we can visually see the heartbeat:
        If (ProgressBar1.Value < 100) Then
            ProgressBar1.Value = ProgressBar1.Value + 10
        Else
            ProgressBar1.Value = 0
        End If

        ' Handle application events so our user interface remains responsive.
        Application.DoEvents()

    End Sub

    ' Our user-interface has an abort button which when pressed,
    ' sets the AbortCurrentFtpOp boolean to True.  If an FTP operation
    ' is in progress, it will be aborted at the next heartbeat.
    Private Sub AbortButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AbortButton.Click

        AbortButtonPressed = True

    End Sub

    ' Called before each file is deleted.
    Private Sub ftpObj_OnVerifyDeleteFile(ByVal sender As Object, ByVal args As Chilkat.FtpTreeEventArgs) Handles ftpObj.OnVerifyDeleteFile

        ListBox1.Items.Add(args.Path)
        ListBox1.Refresh()

        ' Do not delete .txt files:
        If (args.Path.EndsWith(".txt")) Then
            args.Skip = True
        End If

    End Sub

    ' Called before each sub-directory is entered.
    Private Sub ftpObj_OnVerifyDeleteDir(ByVal sender As Object, ByVal args As Chilkat.FtpTreeEventArgs) Handles ftpObj.OnVerifyDeleteDir

        ListBox1.Items.Add("DIR: " & args.Path)
        ListBox1.Refresh()

        ' Do not delete directories named "important"
        If (args.Path.Contains("/important/")) Then
            args.Skip = True
        End If

    End Sub

    ' Connect to an FTP server, login, and delete a directory tree:
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click

        Dim success As Boolean

        success = ftpObj.UnlockComponent("Anything for 30-day trial")
        If (Not success) Then
            MessageBox.Show(ftpObj.LastErrorText)
            Exit Sub
        End If

        ' -----------------------------------------------
        ' IMPORTANT: Be sure to enable events on the object
        ' -----------------------------------------------
        ftpObj.EnableEvents = True

        ' Set our heartbeat interval to .1 seconds:
        ftpObj.HeartbeatMs = 100

        ' Set the FTP hostname, login, and password.
        ftpObj.Hostname = "ftp.yourftpserver.com"
        ftpObj.Username = "***"
        ftpObj.Password = "***"

        ' Connect and login to the FTP server.  Heartbeat events fire during the
        ' the connection establishment, authenticaion, and during all other communications
        ' with the FTP server.
        success = ftpObj.Connect()
        If (Not success) Then
            MessageBox.Show(ftpObj.LastErrorText)
            Exit Sub
        End If

        ' Navigate to the root of the remote directory tree to be deleted.
        success = ftpObj.ChangeRemoteDir("b")
        If (Not success) Then
            MessageBox.Show(ftpObj.LastErrorText)
            Exit Sub
        End If

        ' Delete the remote tree.  This remotes all files and sub-directories
        ' in the current remote directory.  If we want to remove the root
        ' directory itself (in this case "b") then move up one directory
        ' level and then delete the directory.
        success = ftpObj.DeleteTree()
        If (Not success) Then
            MessageBox.Show(ftpObj.LastErrorText)
            Exit Sub
        End If

        ' If we want to remove the "b" directory, move up one level and delete
        ' it.  However, this would only work if there are no more files and
        ' directories remaining under "b".
        success = ftpObj.ChangeRemoteDir("..")
        If (Not success) Then
            MessageBox.Show(ftpObj.LastErrorText)
            Exit Sub
        End If
        success = ftpObj.RemoveRemoteDir("b")
        If (Not success) Then
            MessageBox.Show(ftpObj.LastErrorText)
            Exit Sub
        End If

        ' Disconnect
        ftpObj.Disconnect()

        ' We're finished!
        MessageBox.Show("FTP DeleteTree Finished!")

    End Sub

 

© 2000-2012 Chilkat Software, Inc. All Rights Reserved.

Mail Component · XML Parser