Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Get files from directories

Posted on 2004-08-20
5
192 Views
Last Modified: 2010-04-23
Here is what i need

A multiline text box
A path text box
A start button

I will put a list of file names in the multi line text box 1 file per line
i will puth the root path in where the files will be searched

when i hit the start i want it to go into the root path and all of its sub directories and search for the files

then at the end i want the latest created file to be moved into a new directory we will say c:\MyFiles

And write a text file that will say where it copied the file from. Full Path

How can this be done ?
0
Comment
Question by:Vartana
5 Comments
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11857370
What will happen if any file is found ?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11857501
Here is a complete solution to your question:

Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents FolderBrowserDialog1 As System.Windows.Forms.FolderBrowserDialog
    Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
    Friend WithEvents cmdRootPath As System.Windows.Forms.Button
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents rootPath As System.Windows.Forms.TextBox
    Friend WithEvents files As System.Windows.Forms.TextBox
    Friend WithEvents outputPath As System.Windows.Forms.TextBox
    Friend WithEvents cmdOutputPath As System.Windows.Forms.Button
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents cmdSearch As System.Windows.Forms.Button
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents lblLatestFile As System.Windows.Forms.Label
    Friend WithEvents lblCreated As System.Windows.Forms.Label
    Friend WithEvents Label7 As System.Windows.Forms.Label
    Friend WithEvents lblCurrentSearchPath As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.rootPath = New System.Windows.Forms.TextBox
        Me.cmdRootPath = New System.Windows.Forms.Button
        Me.FolderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog
        Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
        Me.files = New System.Windows.Forms.TextBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.outputPath = New System.Windows.Forms.TextBox
        Me.cmdOutputPath = New System.Windows.Forms.Button
        Me.Label3 = New System.Windows.Forms.Label
        Me.cmdSearch = New System.Windows.Forms.Button
        Me.Label4 = New System.Windows.Forms.Label
        Me.lblLatestFile = New System.Windows.Forms.Label
        Me.lblCreated = New System.Windows.Forms.Label
        Me.Label7 = New System.Windows.Forms.Label
        Me.lblCurrentSearchPath = New System.Windows.Forms.Label
        Me.Label6 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(8, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(80, 16)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Root Path:"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'rootPath
        '
        Me.rootPath.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.rootPath.Location = New System.Drawing.Point(88, 8)
        Me.rootPath.Name = "rootPath"
        Me.rootPath.Size = New System.Drawing.Size(504, 20)
        Me.rootPath.TabIndex = 1
        Me.rootPath.Text = "C:\"
        '
        'cmdRootPath
        '
        Me.cmdRootPath.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.cmdRootPath.Location = New System.Drawing.Point(600, 8)
        Me.cmdRootPath.Name = "cmdRootPath"
        Me.cmdRootPath.Size = New System.Drawing.Size(72, 24)
        Me.cmdRootPath.TabIndex = 2
        Me.cmdRootPath.Text = "Select Path"
        '
        'files
        '
        Me.files.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.files.Location = New System.Drawing.Point(88, 40)
        Me.files.Multiline = True
        Me.files.Name = "files"
        Me.files.Size = New System.Drawing.Size(504, 112)
        Me.files.TabIndex = 3
        Me.files.Text = ""
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(8, 40)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(72, 16)
        Me.Label2.TabIndex = 4
        Me.Label2.Text = "Target Files:"
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'outputPath
        '
        Me.outputPath.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.outputPath.Location = New System.Drawing.Point(88, 160)
        Me.outputPath.Name = "outputPath"
        Me.outputPath.Size = New System.Drawing.Size(504, 20)
        Me.outputPath.TabIndex = 5
        Me.outputPath.Text = "C:\MyFiles"
        '
        'cmdOutputPath
        '
        Me.cmdOutputPath.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.cmdOutputPath.Location = New System.Drawing.Point(600, 160)
        Me.cmdOutputPath.Name = "cmdOutputPath"
        Me.cmdOutputPath.Size = New System.Drawing.Size(72, 24)
        Me.cmdOutputPath.TabIndex = 6
        Me.cmdOutputPath.Text = "Select Path"
        '
        'Label3
        '
        Me.Label3.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.Label3.Location = New System.Drawing.Point(8, 160)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(72, 16)
        Me.Label3.TabIndex = 7
        Me.Label3.Text = "Output Path:"
        Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'cmdSearch
        '
        Me.cmdSearch.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.cmdSearch.Location = New System.Drawing.Point(600, 40)
        Me.cmdSearch.Name = "cmdSearch"
        Me.cmdSearch.Size = New System.Drawing.Size(72, 24)
        Me.cmdSearch.TabIndex = 8
        Me.cmdSearch.Text = "Search"
        '
        'Label4
        '
        Me.Label4.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.Label4.Location = New System.Drawing.Point(8, 232)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(72, 24)
        Me.Label4.TabIndex = 9
        Me.Label4.Text = "Latest File:"
        Me.Label4.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'lblLatestFile
        '
        Me.lblLatestFile.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lblLatestFile.Location = New System.Drawing.Point(88, 232)
        Me.lblLatestFile.Name = "lblLatestFile"
        Me.lblLatestFile.Size = New System.Drawing.Size(504, 32)
        Me.lblLatestFile.TabIndex = 10
        '
        'lblCreated
        '
        Me.lblCreated.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lblCreated.Location = New System.Drawing.Point(88, 280)
        Me.lblCreated.Name = "lblCreated"
        Me.lblCreated.Size = New System.Drawing.Size(504, 16)
        Me.lblCreated.TabIndex = 12
        Me.lblCreated.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'Label7
        '
        Me.Label7.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.Label7.Location = New System.Drawing.Point(8, 280)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(72, 16)
        Me.Label7.TabIndex = 11
        Me.Label7.Text = "Created:"
        Me.Label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'lblCurrentSearchPath
        '
        Me.lblCurrentSearchPath.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lblCurrentSearchPath.Location = New System.Drawing.Point(88, 192)
        Me.lblCurrentSearchPath.Name = "lblCurrentSearchPath"
        Me.lblCurrentSearchPath.Size = New System.Drawing.Size(504, 32)
        Me.lblCurrentSearchPath.TabIndex = 14
        '
        'Label6
        '
        Me.Label6.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.Label6.Location = New System.Drawing.Point(8, 192)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(72, 24)
        Me.Label6.TabIndex = 13
        Me.Label6.Text = "Searching:"
        Me.Label6.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(680, 302)
        Me.Controls.Add(Me.lblCurrentSearchPath)
        Me.Controls.Add(Me.Label6)
        Me.Controls.Add(Me.lblCreated)
        Me.Controls.Add(Me.Label7)
        Me.Controls.Add(Me.lblLatestFile)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.cmdSearch)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.cmdOutputPath)
        Me.Controls.Add(Me.outputPath)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.files)
        Me.Controls.Add(Me.cmdRootPath)
        Me.Controls.Add(Me.rootPath)
        Me.Controls.Add(Me.Label1)
        Me.Name = "Form1"
        Me.Text = "Get files from directories solution"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private latestFile As FileInfo

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        checkInputs()
    End Sub

    Private Sub cmdRootPath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRootPath.Click
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            rootPath.Text = FolderBrowserDialog1.SelectedPath
        End If
    End Sub

    Private Sub cmdOutputPath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOutputPath.Click
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            outputPath.Text = FolderBrowserDialog1.SelectedPath
        End If
    End Sub

    Private Sub inputsChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rootPath.TextChanged, outputPath.TextChanged, files.TextChanged
        checkInputs()
    End Sub

    Private Function checkInputs() As Boolean
        cmdSearch.Enabled = Directory.Exists(rootPath.Text) And _
            Directory.Exists(outputPath.Text) And files.Text.Trim.Length > 0
        Return cmdSearch.Enabled
    End Function

    Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
        If Not checkInputs() Then
            MsgBox("You have either an invalid Path or no target files specified", MsgBoxStyle.Information, "Invalid Input")
            Exit Sub
        End If

        cmdSearch.Enabled = False
        latestFile = Nothing
        lblCurrentSearchPath.Text = ""
        lblLatestFile.Text = ""
        lblCreated.Text = ""
        If Not rootPath.Text.EndsWith("\") Then
            rootPath.Text = rootPath.Text & "\"
        End If
        searchForTargetFiles(Split(files.Text, vbCrLf), rootPath.Text)
        If latestFile Is Nothing Then
            MsgBox("No Matching Files Found")
        Else
            Try
                If Not outputPath.Text.EndsWith("\") Then
                    outputPath.Text = outputPath.Text & "\"
                End If
                File.Move(latestFile.FullName, outputPath.Text & latestFile.Name)
                Dim sw As StreamWriter = New StreamWriter(outputPath.Text & "FileSourcePath.txt", False)
                sw.WriteLine(latestFile.FullName)
                sw.Close()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Error Moving File")
            Finally
                MsgBox("Search and Move Complete")
            End Try
        End If
        lblCurrentSearchPath.Text = ""
        cmdSearch.Enabled = True
    End Sub

    Private Sub searchForTargetFiles(ByVal targetFiles() As String, ByVal pathToSearch As String)
        Dim targetFile As String
        Dim subDirectory As String
        Dim fi As FileInfo

        If Not pathToSearch.EndsWith("\") Then
            pathToSearch = pathToSearch & "\"
        End If

        lblCurrentSearchPath.Text = pathToSearch
        lblCurrentSearchPath.Refresh()
        For Each targetFile In targetFiles
            If targetFile.Length > 0 Then
                If File.Exists(pathToSearch & targetFile) Then
                    fi = New FileInfo(pathToSearch & targetFile)
                    If latestFile Is Nothing Then
                        latestFile = fi
                        lblLatestFile.Text = fi.FullName
                        lblCreated.Text = fi.CreationTime
                    Else
                        If fi.CreationTime > latestFile.CreationTime Then
                            latestFile = fi
                            lblLatestFile.Text = fi.FullName
                            lblCreated.Text = fi.CreationTime
                        End If
                    End If
                End If
            End If
            Application.DoEvents()
        Next

        For Each subDirectory In Directory.GetDirectories(pathToSearch)
            searchForTargetFiles(targetFiles, subDirectory)
        Next
    End Sub

End Class
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11857618
Heres my take

Private Sub ShowFolders()
  Dim s() As String

  s = System.IO.Directory.GetDirectories("c:\")

  Dim en As System.Collections.IEnumerator

  en = s.GetEnumerator

  While en.MoveNext
     Console.WriteLine(CStr(en.Current))
  End While
End Sub

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' make a reference to a directory
    Dim di As New IO.DirectoryInfo("c:\")
    Dim diar1 As IO.FileInfo() = di.GetFiles()
    Dim dra As IO.FileInfo

   'list the names of all files in the specified directory
    For Each dra In diar1
        ListBox1.Items.Add(dra)
    Next
End Sub

To filter search change di.GetFiles() to di.GetFiles(“.extionsion”)

Regards,

Aeros
0
 
LVL 1

Author Comment

by:Vartana
ID: 11871998
Idle_Mind this moves the latest file out of all the files i listed only.  Can we do it so it moves
the latest file for each file i list ?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 11872595
Sure...here ya go:

Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents FolderBrowserDialog1 As System.Windows.Forms.FolderBrowserDialog
    Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
    Friend WithEvents cmdRootPath As System.Windows.Forms.Button
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents rootPath As System.Windows.Forms.TextBox
    Friend WithEvents files As System.Windows.Forms.TextBox
    Friend WithEvents outputPath As System.Windows.Forms.TextBox
    Friend WithEvents cmdOutputPath As System.Windows.Forms.Button
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents cmdSearch As System.Windows.Forms.Button
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents lblLatestFile As System.Windows.Forms.Label
    Friend WithEvents lblCreated As System.Windows.Forms.Label
    Friend WithEvents Label7 As System.Windows.Forms.Label
    Friend WithEvents lblCurrentSearchPath As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.rootPath = New System.Windows.Forms.TextBox
        Me.cmdRootPath = New System.Windows.Forms.Button
        Me.FolderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog
        Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
        Me.files = New System.Windows.Forms.TextBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.outputPath = New System.Windows.Forms.TextBox
        Me.cmdOutputPath = New System.Windows.Forms.Button
        Me.Label3 = New System.Windows.Forms.Label
        Me.cmdSearch = New System.Windows.Forms.Button
        Me.Label4 = New System.Windows.Forms.Label
        Me.lblLatestFile = New System.Windows.Forms.Label
        Me.lblCreated = New System.Windows.Forms.Label
        Me.Label7 = New System.Windows.Forms.Label
        Me.lblCurrentSearchPath = New System.Windows.Forms.Label
        Me.Label6 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(8, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(80, 16)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Root Path:"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'rootPath
        '
        Me.rootPath.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.rootPath.Location = New System.Drawing.Point(88, 8)
        Me.rootPath.Name = "rootPath"
        Me.rootPath.Size = New System.Drawing.Size(504, 20)
        Me.rootPath.TabIndex = 1
        Me.rootPath.Text = "C:\"
        '
        'cmdRootPath
        '
        Me.cmdRootPath.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.cmdRootPath.Location = New System.Drawing.Point(600, 8)
        Me.cmdRootPath.Name = "cmdRootPath"
        Me.cmdRootPath.Size = New System.Drawing.Size(72, 24)
        Me.cmdRootPath.TabIndex = 2
        Me.cmdRootPath.Text = "Select Path"
        '
        'files
        '
        Me.files.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.files.Location = New System.Drawing.Point(88, 40)
        Me.files.Multiline = True
        Me.files.Name = "files"
        Me.files.Size = New System.Drawing.Size(504, 112)
        Me.files.TabIndex = 3
        Me.files.Text = ""
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(8, 40)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(72, 16)
        Me.Label2.TabIndex = 4
        Me.Label2.Text = "Target Files:"
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'outputPath
        '
        Me.outputPath.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.outputPath.Location = New System.Drawing.Point(88, 160)
        Me.outputPath.Name = "outputPath"
        Me.outputPath.Size = New System.Drawing.Size(504, 20)
        Me.outputPath.TabIndex = 5
        Me.outputPath.Text = "C:\MyFiles"
        '
        'cmdOutputPath
        '
        Me.cmdOutputPath.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.cmdOutputPath.Location = New System.Drawing.Point(600, 160)
        Me.cmdOutputPath.Name = "cmdOutputPath"
        Me.cmdOutputPath.Size = New System.Drawing.Size(72, 24)
        Me.cmdOutputPath.TabIndex = 6
        Me.cmdOutputPath.Text = "Select Path"
        '
        'Label3
        '
        Me.Label3.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.Label3.Location = New System.Drawing.Point(8, 160)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(72, 16)
        Me.Label3.TabIndex = 7
        Me.Label3.Text = "Output Path:"
        Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'cmdSearch
        '
        Me.cmdSearch.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.cmdSearch.Location = New System.Drawing.Point(600, 40)
        Me.cmdSearch.Name = "cmdSearch"
        Me.cmdSearch.Size = New System.Drawing.Size(72, 24)
        Me.cmdSearch.TabIndex = 8
        Me.cmdSearch.Text = "Search"
        '
        'Label4
        '
        Me.Label4.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.Label4.Location = New System.Drawing.Point(8, 232)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(72, 24)
        Me.Label4.TabIndex = 9
        Me.Label4.Text = "Latest File:"
        Me.Label4.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'lblLatestFile
        '
        Me.lblLatestFile.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lblLatestFile.Location = New System.Drawing.Point(88, 232)
        Me.lblLatestFile.Name = "lblLatestFile"
        Me.lblLatestFile.Size = New System.Drawing.Size(504, 32)
        Me.lblLatestFile.TabIndex = 10
        '
        'lblCreated
        '
        Me.lblCreated.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lblCreated.Location = New System.Drawing.Point(88, 280)
        Me.lblCreated.Name = "lblCreated"
        Me.lblCreated.Size = New System.Drawing.Size(504, 16)
        Me.lblCreated.TabIndex = 12
        Me.lblCreated.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'Label7
        '
        Me.Label7.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.Label7.Location = New System.Drawing.Point(8, 280)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(72, 16)
        Me.Label7.TabIndex = 11
        Me.Label7.Text = "Created:"
        Me.Label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'lblCurrentSearchPath
        '
        Me.lblCurrentSearchPath.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lblCurrentSearchPath.Location = New System.Drawing.Point(88, 192)
        Me.lblCurrentSearchPath.Name = "lblCurrentSearchPath"
        Me.lblCurrentSearchPath.Size = New System.Drawing.Size(504, 32)
        Me.lblCurrentSearchPath.TabIndex = 14
        '
        'Label6
        '
        Me.Label6.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.Label6.Location = New System.Drawing.Point(8, 192)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(72, 24)
        Me.Label6.TabIndex = 13
        Me.Label6.Text = "Searching:"
        Me.Label6.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(680, 302)
        Me.Controls.Add(Me.lblCurrentSearchPath)
        Me.Controls.Add(Me.Label6)
        Me.Controls.Add(Me.lblCreated)
        Me.Controls.Add(Me.Label7)
        Me.Controls.Add(Me.lblLatestFile)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.cmdSearch)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.cmdOutputPath)
        Me.Controls.Add(Me.outputPath)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.files)
        Me.Controls.Add(Me.cmdRootPath)
        Me.Controls.Add(Me.rootPath)
        Me.Controls.Add(Me.Label1)
        Me.Name = "Form1"
        Me.Text = "Get files from directories solution"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private latestFiles As Hashtable

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        checkInputs()
    End Sub

    Private Sub cmdRootPath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRootPath.Click
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            rootPath.Text = FolderBrowserDialog1.SelectedPath
        End If
    End Sub

    Private Sub cmdOutputPath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOutputPath.Click
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            outputPath.Text = FolderBrowserDialog1.SelectedPath
        End If
    End Sub

    Private Sub inputsChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rootPath.TextChanged, outputPath.TextChanged, files.TextChanged
        checkInputs()
    End Sub

    Private Function checkInputs() As Boolean
        cmdSearch.Enabled = Directory.Exists(rootPath.Text) And _
            Directory.Exists(outputPath.Text) And files.Text.Trim.Length > 0
        Return cmdSearch.Enabled
    End Function

    Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
        If Not checkInputs() Then
            MsgBox("You have either an invalid Path or no target files specified", MsgBoxStyle.Information, "Invalid Input")
            Exit Sub
        End If

        Dim de As DictionaryEntry
        Dim fi As FileInfo

        cmdSearch.Enabled = False
        latestFiles = New Hashtable
        lblCurrentSearchPath.Text = ""
        lblLatestFile.Text = ""
        lblCreated.Text = ""
        If Not rootPath.Text.EndsWith("\") Then
            rootPath.Text = rootPath.Text & "\"
        End If
        searchForTargetFiles(Split(files.Text, vbCrLf), rootPath.Text)
        If latestFiles.Count = 0 Then
            MsgBox("No Matching Files Found")
        Else
            For Each de In latestFiles
                fi = CType(de.Value, FileInfo)
                Try
                    If Not outputPath.Text.EndsWith("\") Then
                        outputPath.Text = outputPath.Text & "\"
                    End If
                    File.Move(fi.FullName, outputPath.Text & fi.Name)
                    Dim sw As StreamWriter = New StreamWriter(outputPath.Text & "SourcePath_" & fi.Name & ".txt", False)
                    sw.WriteLine(fi.FullName)
                    sw.Close()
                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Critical, "Error Moving File " & fi.FullName)
                End Try
            Next
            MsgBox("Search and Move Complete")
        End If
        lblCurrentSearchPath.Text = ""
        cmdSearch.Enabled = True
    End Sub

    Private Sub searchForTargetFiles(ByVal targetFiles() As String, ByVal pathToSearch As String)
        Dim targetFile As String
        Dim subDirectory As String
        Dim fi As FileInfo
        Dim lastFi As FileInfo

        If Not pathToSearch.EndsWith("\") Then
            pathToSearch = pathToSearch & "\"
        End If

        lblCurrentSearchPath.Text = pathToSearch
        lblCurrentSearchPath.Refresh()
        For Each targetFile In targetFiles
            If targetFile.Length > 0 Then
                If File.Exists(pathToSearch & targetFile) Then
                    fi = New FileInfo(pathToSearch & targetFile)
                    If Not latestFiles.ContainsKey(targetFile) Then
                        latestFiles.Add(targetFile, fi)
                    Else
                        lastFi = CType(latestFiles.Item(targetFile), FileInfo)
                        If fi.CreationTime > lastFi.CreationTime Then
                            latestFiles.Item(targetFile) = fi
                            lblLatestFile.Text = fi.FullName
                            lblCreated.Text = fi.CreationTime
                        End If
                    End If
                End If
            End If
            Application.DoEvents()
        Next

        For Each subDirectory In Directory.GetDirectories(pathToSearch)
            searchForTargetFiles(targetFiles, subDirectory)
        Next
    End Sub

End Class
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question