Results 1 to 7 of 7

Thread: [RESOLVED] Delete file in subfolder

Hybrid View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2011
    Posts
    31

    Re: Delete file in subfolder

    Ok i found this code at the link you gave me.

    This function is pretty new for me.

    Do i put this in a class?

    Can you explain/help me how to use this ?

    Code:
    Imports System
    Imports System.IO
    
    Public Class MainClass
    
      Shared Sub Main()
    
        Dim nameOfDirectory As String = "C:\"
        Dim myDirectory As DirectoryInfo
        myDirectory = New DirectoryInfo(nameOfDirectory)
        WorkWithDirectory(myDirectory)
    
      End Sub
    
      Shared Public Sub WorkWithDirectory(ByVal aDir As DirectoryInfo)
        Dim nextDir As DirectoryInfo
        WorkWithFilesInDir(aDir)
        For Each nextDir In aDir.GetDirectories
          WorkWithDirectory(nextDir)
        Next
      End Sub
    
      Shared Public Sub WorkWithFilesInDir(ByVal aDir As DirectoryInfo)
        Dim aFile As FileInfo
        For Each aFile In aDir.GetFiles()
          Console.WriteLine(aFile.FullName)
        Next
      End Sub
    
      
    End Class

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Delete file in subfolder

    Form with a single button
    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            RemoveFile("D:\Data", "test.test")
        End Sub
    End Class
    Code Module, replace the console write with your delete method
    Code:
    Imports System.IO
    Module FileCode
        Private FileToRemove As String = ""
        Public Sub RemoveFile(ByVal FolderName As String, ByVal FileName As String)
            FileToRemove = FileName
            RecurseDirectory(New DirectoryInfo(FolderName))
        End Sub
        Public Sub RecurseDirectory(ByVal FolderInfo As DirectoryInfo)
            Dim nextDir As DirectoryInfo
            WorkFiles(FolderInfo)
            For Each nextDir In FolderInfo.GetDirectories
                RecurseDirectory(nextDir)
            Next
        End Sub
        Public Sub WorkFiles(ByVal FolderInfo As DirectoryInfo)
            Dim FI As FileInfo
            For Each FI In FolderInfo.GetFiles()
    
                If IO.Path.GetFileName(FI.FullName.ToLower) = FileToRemove.ToLower Then
                    Console.WriteLine(FI.FullName)
                End If
            Next
        End Sub
    End Module

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width