Results 1 to 7 of 7

Thread: [RESOLVED] Delete file in subfolder

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2011
    Posts
    31

    Resolved [RESOLVED] Delete file in subfolder

    Hello all,

    I have a problem and can't figure it out anymore.
    Bin looking at this prob for like a day now.

    I hope any of you can and want to help me.

    I have below code and i want to change it so that it will delete the file "test.test" in a sub directory.
    I don't know the name of the sub directory so it has to go true the sub directories and when it finds the "test.test" file
    it needs to delete it.

    The folder only has 2 sub directories. That is the only thing i know.

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
                Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\test\"
                
                If System.IO.Directory.Exists(path) = True Then
                    System.IO.Directory.Delete(path, True)
                 Else
                     msgbox("file isn't there")
                End If
    
        End Sub

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

    Re: Delete file in subfolder


  3. #3

    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

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

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2011
    Posts
    31

    Re: Delete file in subfolder

    Alright thanks men.

    Works like a charm.

    I changed console.writeline with:
    Code:
    File.Delete(FI.FullName)
    It's a normal file and can be deleted at any time.

    And changed the single button click to:

    Code:
    Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
            RemoveFile(path & "\test\test\", "test.test")
    Thanks allot for the fast reply and the fast help.

    You rock

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

    Re: [RESOLVED] Delete file in subfolder

    Good to hear this works for you.

    For the path you can also use Io.Path.Combine

    Code:
    Dim path As String = IO.Path.Combine( _
        Environment.GetFolderPath( _
            Environment.SpecialFolder.ApplicationData), "test\test\")

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2011
    Posts
    31

    Re: [RESOLVED] Delete file in subfolder

    That's an even better option.
    I can use that in the rest of my code.

    Thanks again Keven.

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