I have a search function set-up to find all files and sub-folders based on size and/or file type. I'm now trying to use this to search among our database to find users with large amounts of data that can be deleted. However, there are too many users in the system so I would like to search alphabetically or numerically.
Ex: I want to search for folders > 10MB in I:\Documents and Settings\
it searches I:\Documents and Settings\user1
I:\Documents and Settings\user2
I:\Documents and Settings\user3
I:\Documents and Settings\user4
I:\Documents and Settings\user5.... and so on until I receive an error that it's taking to long
What I'd like to do is run a loop that allows me to search this directory from user 1 to user 15 and then user 15 through user 30 and so on...
any help would be great below is my code. my form uses a listbox for output, 2 numericupdowns for file and folder size and a textbox used with folderbrowserdialog to enter a directory
Ex: I want to search for folders > 10MB in I:\Documents and Settings\
it searches I:\Documents and Settings\user1
I:\Documents and Settings\user2
I:\Documents and Settings\user3
I:\Documents and Settings\user4
I:\Documents and Settings\user5.... and so on until I receive an error that it's taking to long
What I'd like to do is run a loop that allows me to search this directory from user 1 to user 15 and then user 15 through user 30 and so on...
any help would be great below is my code. my form uses a listbox for output, 2 numericupdowns for file and folder size and a textbox used with folderbrowserdialog to enter a directory
VB.NET:
Imports System.IO
Public Class Form1
'this button activates your search for the given directory
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SearchDirectory(TextBox1.Text)
ListBox1.Items.Add("")
ListBox1.Items.Add("Done.")
End Sub
Private Sub SearchDirectory(ByVal sDir As String)
Dim strDir As String
Dim strFile As String
Dim dblFolderSize As Double
Dim fileType As String
fileType = TextBox2.Text
Try
'search by each folder within the directory
For Each strDir In System.IO.Directory.GetDirectories(sDir)
dblFolderSize = GetFolderSize(strDir, True) / 1048576
'run search for folders only larger or equal to the given value
If dblFolderSize >= NumericFolder.Value Then
'return results for folders
ListBox1.Items.Add("Size: " + CStr(Round((dblFolderSize / 100), 2)) + "MB** FOLDER: " + strDir)
'search within each folder for files of the specified size
For Each strFile In System.IO.Directory.GetFiles(strDir)
Dim dblFileSize As Double
dblFileSize = (GetFileSize(strFile)) / 1048576
If dblFileSize >= NumericFile.Value Then
'if looking for a file type this will return those results
If strFile.EndsWith(fileType) Then
ListBox1.Items.Add(" Size: " + CStr(Round(dblFileSize, 2) / 100) + "MB*** FILENAME: " + strFile)
ListBox1.Refresh()
End If
End If
Next
're-run the search to get every sub-folder within the directory
SearchDirectory(strDir)
End If
Next
Catch ex As System.Exception
Debug.WriteLine(ex.Message)
End Try
End Sub
Function GetFolderSize(ByVal DirPath As String, Optional ByVal IncludeSubFolders As Boolean = True) As Double
Dim dblDirSize As Double
Dim objFileInfo As FileInfo
Dim objDir As DirectoryInfo = New DirectoryInfo(DirPath)
Dim objSubFolder As DirectoryInfo
Try
'add length of each file
For Each objFileInfo In objDir.GetFiles()
dblDirSize += objFileInfo.Length
Next
'call recursively to get sub folders
'if you don't want this set optional param to False
If IncludeSubFolders Then
For Each objSubFolder In objDir.GetDirectories()
dblDirSize += GetFolderSize(objSubFolder.FullName)
Next
End If
Catch ex As Exception
End Try
Return dblDirSize
End Function
'this function is to get file size by converting fileInfo as a String
Function GetFileSize(ByVal fileSize As String) As Long
Dim objFileInfo As FileInfo = New FileInfo(fileSize)
Dim Size As Double
Size = objFileInfo.Length
Return Size
End Function
'use this to enable a browse window to search a given path
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
FolderBrowserDialog1.ShowDialog()
TextBox1.Text = FolderBrowserDialog1.SelectedPath
End Sub
Function Round(ByVal nValue As Double, ByVal nDigits As Integer) As Double
Round = Int(nValue * (10 ^ nDigits) + (0.5) / (10 ^ nDigits))
End Function
'return results as a txt file
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim streamWriter As StreamWriter = New StreamWriter("I:\results.txt")
For Each o As Object In ListBox1.Items
streamWriter.WriteLine(o.ToString())
Next
streamWriter.Flush()
streamWriter.Close()
End Sub
End Class
Last edited: