Advertisement
Advertisement
| 05.13.2008 at 07:36AM PDT, ID: 23397951 |
|
[x]
Attachment Details
|
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: |
Imports System.IO
Public Class Form1
'' select source 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If FolderBrowserDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
Label1.Text = FolderBrowserDialog.SelectedPath
End If
End Sub
''' select source 2
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If FolderBrowserDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
Label2.Text = FolderBrowserDialog.SelectedPath
End If
End Sub
''' select source 3
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If FolderBrowserDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
Label3.Text = FolderBrowserDialog.SelectedPath
End If
End Sub
''' load timers
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Timer2.Enabled = True
REM TextBox1.Text = Format(TimeOfDay, "Long Time")
End Sub
'' display clock in program
Private Sub Timer1_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Timer1.Tick
REM Me.Text = TimeOfDay & " As Run Process"
Label4.Text = CStr(TimeOfDay)
End Sub
'' run action at displayed time
Private Sub Timer2_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Timer2.Tick
If TextBox1.Text = Label4.Text Then
Dim fldrs As ArrayList = GetFolders(Label1.Text)
Dim i As Integer
For i = 0 To (fldrs.Count - 1)
MessageBox.Show(GetNewestFile(fldrs(i), 150, True))
Next
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim fldrs As ArrayList = GetFolders(Label1.Text)
Dim i As Integer
Dim FileList As New ArrayList
For i = 0 To (fldrs.Count - 1)
FileList.Add(GetNewestFile(fldrs(i), 150, True))
Next
End Sub
Private Function GetFolders(ByVal path As String) As ArrayList
Dim categories() As String = Directory.GetDirectories(Label1.Text)
Dim i As Integer
Dim al As New ArrayList
For i = 0 To categories.Length - 1
al.Add(categories(i))
Next
Return (al)
End Function
''' <summary>
''' Get the name of the file in the specified folder that is the newest over specified size.
''' </summary>
''' <param name="folderPath">The full path to the folder we want to search</param>
''' <param name="minSizeKb">The size (in kilobyes, 1024 bytes) the file must be in order to be considered.</param>
''' <param name="includeSubFolders">search subfolders as well</param>
''' <returns>Full path to newest file</returns>
''' <remarks>Uses last modified date, not created date</remarks>
Public Function GetNewestFile(ByVal folderPath As String, ByVal minSizeKb As Long, ByVal includeSubFolders As Boolean) As String
Dim latestFileDate As Date = New Date(1901, 1, 1)
Dim latestFilePath As String = ""
Dim di As New DirectoryInfo(folderPath)
If di.Exists Then ' continue if directory exists
' search the current folder first
searchFolder(di, minSizeKb, includeSubFolders, latestFileDate, latestFilePath)
If includeSubFolders Then ' search subfolders if required
Dim subDirs As DirectoryInfo() = di.GetDirectories()
For Each subdi In subDirs
searchFolder(subdi, minSizeKb, includeSubFolders, latestFileDate, latestFilePath)
Next
End If
End If ' If di.Exists then
Return latestFilePath
End Function
''' <summary>
''' Helper function called recursively for subdirectories. Do not call this function directly, call 'GetNewestFile'
''' </summary>
''' <param name="di"></param>
''' <param name="minSizeKb"></param>
''' <param name="includeSubFolders"></param>
''' <param name="latestFileDate">used for returning matches in subdirectories</param>
''' <param name="latestFilePath">used for returning matches in subdirectories</param>
''' <remarks></remarks>
Private Sub searchFolder(ByVal di As DirectoryInfo, ByVal minSizeKb As Long, ByVal includeSubFolders As Boolean, _
ByRef latestFileDate As Date, ByRef latestFilePath As String)
Const KBSIZE As Long = 1024
Dim fi As FileInfo() = di.GetFiles() ' get list of all files in directory
For i = fi.GetLowerBound(0) To fi.GetUpperBound(0)
If (fi(i).Length / KBSIZE) > minSizeKb Then ' make sure file meets min size requirement
If fi(i).LastWriteTimeUtc > latestFileDate Then ' found newer file, save it
latestFileDate = fi(i).LastWriteTimeUtc
latestFilePath = fi(i).FullName
End If
End If
Next
If includeSubFolders Then
Dim subDirs As DirectoryInfo() = di.GetDirectories()
For Each subdi In subDirs
searchFolder(subdi, minSizeKb, includeSubFolders, latestFileDate, latestFilePath)
Next
End If
End Sub
End Class
|