-
October 2nd, 2013, 10:09 AM
#1
Removeing unwanted files from numerous directories
Ok I am working on an app to delete unwanted files from a main directory and all sub directories under it. There are no files what so ever in the main directory there is just sub directories. I only want to keep 2 files a video file and another file.
Here is my code
Code:
Imports System.IO
Public Class filedelete
Dim dirPath As String
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Update_Form()
End Sub
Private Sub Update_Form()
Dim dirs As List(Of String) = New List(Of String)(Directory.GetDirectories(dirPath))
Dim filename As String
Dim fileext As String
Dim fullpath As String
' clear the listboxes to be updated
ListBox1.Items.Clear()
Dim ValidExtensions As String() = {".mpg", ".avi", ".wmv", ".mp4", ".nfo", ".jpg", ".png", ".gif"}
Try
For Each folder In dirs
Dim txtFiles = Directory.GetFiles(folder)
For Each currentFile As String In txtFiles
filename = currentFile.Substring(folder.Length + 1)
fullpath = folder + "\" + filename
fileext = Path.GetExtension(filename)
' list of files to be deleted in this listbox
If Not ValidExtensions.Contains(fileext) Then
ListBox1.Items.Add(fullpath)
End If
Next
Next
Catch ex As Exception
MessageBox.Show("Unable to list files due to " & ex.Message)
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
' File delete each item from the list box
' Text box to show each file being deleted.
Dim l_index As Integer
Dim l_text As String
Try
' File delete each item from the list box
' Text box to show each file being deleted.
For l_index = 0 To ListBox1.Items.Count - 1
l_text = CStr(ListBox1.Items(l_index))
File.Delete(l_text)
Next
Catch ex As Exception
MessageBox.Show("Unable to list files due to " & ex.Message)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim folderDlg As New FolderBrowserDialog
Try
folderDlg.ShowNewFolderButton = True
If (folderDlg.ShowDialog() = DialogResult.OK) Then
TextBox1.Text = folderDlg.SelectedPath
Dim root As Environment.SpecialFolder = folderDlg.RootFolder
ElseIf (folderDlg.ShowDialog() = DialogResult.Cancel) Then
Return
End If
dirPath = TextBox1.Text
Update_Form()
Catch ex As Exception
MessageBox.Show("Unable to list files due to " & ex.Message)
End Try
End Sub
End Class
The list box is used to display the items only. There is going to be no selection from the listbox what so ever to delete the files. As I want all the files, that are not video files and one other file, in those directories deleted. (Sorry for the bold but wanted to stress this)
Couple of issues here.
1: It seems that when I select a folder from the file dialog it doesn't work all the time. I have created a bunch of sample directories with nested directories in there each with 5 or 6 sample files with 2 that are valid. The directories are named sample, sample(1) and so on to sample(7) each has 15 sub directories named sample1 ... sample15 each with a .avi .jpg a .sfv a .url a .txt file in them
2: I would like to remove each entry from the listbox during the file delete routine. Since I am not selecting anything from the list box and using that in the File.Delete() call I am unsure how to remove the corresponding entry from the list box.
3: It also seems now my file delete is not working either. That could be the issue with the list box getting the correct values.
4: In the file dialog If I cancel out of it and I test for the cancel key to be clicked and if it is then I return to the main form. How ever it takes 2 clicks of the cancel button to get it to return. How do I fix that?
5: In the file dialog box how can I get it to display all my drives under my computer instead of Desktop and everything below it. I do understand My Computer returns "" but is there anyway to get it to default to a drive in Computer
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
|