Advertisement
Advertisement
| 05.14.2008 at 01:42PM PDT, ID: 23403064 |
|
[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: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: |
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 FileList As New System.Collections.Generic.List(Of String)
Dim targetfolder As String = Label2.Text
For Each folder As String In fldrs
Dim filename As String = GetNewestFile(folder, 150, True)
If Not String.IsNullOrEmpty(filename) Then FileList.Add(filename)
Next folder
Try
CopyFiles(FileList, targetfolder)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
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 FileList As New System.Collections.Generic.List(Of String)
Dim targetfolder As String = Label2.Text
For Each folder As String In fldrs
Dim filename As String = GetNewestFile(folder, 150, True)
If Not String.IsNullOrEmpty(filename) Then FileList.Add(filename)
Next folder
Try
CopyFiles(FileList, targetfolder)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Sub CopyFiles(ByVal FileList As IEnumerable, ByVal TargetFolder As String)
If Not Directory.Exists(TargetFolder) Then Directory.CreateDirectory(TargetFolder)
For Each FileName As String In FileList
File.Copy(FileName, TargetFolder & "\" & Path.GetFileName(FileName), True)
MessageBox.Show((FileName))
Next FileName
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
|