Imports System.IO
Imports WK.Libraries.BetterFolderBrowserNS
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim searchName As String
        Dim searchDate As String
        Dim searchFlag As Boolean
        Dim searchword As String

        If stn1.Text = "" Or date1.Text = "" Or searchfolder.Text = "" Or outputfolder.Text = "" Then
            MsgBox("未入力項目があります。(店舗名の1つ目と日付の一つ目、検索対象フォルダ、出力先フォルダは必ず入力してください)")
            Exit Sub
        End If

        For i = 1 To 5
            If i = 1 Then
                searchName = stn1.Text
            ElseIf i = 2 Then
                If stn2.Text = "" Then
                    Continue For
                Else
                    searchName = stn2.Text
                End If

            ElseIf i = 3 Then
                If stn3.Text = "" Then
                    Continue For
                Else
                    searchName = stn3.Text
                End If
            ElseIf i = 4 Then
                If stn4.Text = "" Then
                    Continue For
                Else
                    searchName = stn4.Text
                End If
            ElseIf i = 5 Then
                If stn5.Text = "" Then
                    Continue For
                Else
                    searchName = stn5.Text
                End If
            End If

            searchDate = date1.Text
            searchword = searchName + searchDate + ".txt"
            MsgBox(searchword)
            search(searchword)

            If date2.Text = "" Then
                Continue For
            Else
                searchDate = date2.Text
                searchword = searchName + searchDate + ".txt"
                MsgBox(searchword)
                search(searchword)
            End If
        Next
    End Sub

    Public Sub search(searchword As String)
        Dim dirPath As String = searchfolder.Text
        Dim output As String = outputfolder.Text & "\"
        Dim filename As String
        Dim fileList As String() = Directory.GetFileSystemEntries(dirPath, searchword)
        Console.WriteLine("file num = " + fileList.Length.ToString())
        For Each filePath As String In fileList
            ' ファイルパスを出力
            MsgBox("file path = " + filePath)
            filename = Strings.Right(filePath, 10)
            Try
                My.Computer.FileSystem.CopyFile(filePath, output & filename)
            Catch ex As Exception
                MsgBox("すでに同じファイルが存在しています。 ファイル名は" & filename & "です。")
                Exit Sub
            End Try
        Next
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim Ret As DialogResult

        Try

            Using Dialog As New FolderBrowserDialog()

                With Dialog
                    .Description = "フォルダを選択してください。"
                End With

                Ret = Dialog.ShowDialog()

                If Ret = DialogResult.OK Then

                    searchfolder.Text = Dialog.SelectedPath

                End If

            End Using

        Catch ex As Exception

            Exit Sub

        End Try
    End Sub
End Class