|
-
Aug 24th, 2011, 08:59 AM
#1
Thread Starter
Junior Member
[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
-
Aug 24th, 2011, 09:42 AM
#2
Re: Delete file in subfolder
-
Aug 24th, 2011, 09:59 AM
#3
Thread Starter
Junior Member
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
-
Aug 24th, 2011, 10:11 AM
#4
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
-
Aug 24th, 2011, 10:37 AM
#5
Thread Starter
Junior Member
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
-
Aug 24th, 2011, 10:49 AM
#6
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\")
-
Aug 24th, 2011, 10:54 AM
#7
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|