遍历指定文件下所有的文件夹和文件的路径,文件名,修改日期,类型

Imports System.IO
Public Class ファイル選択
    Dim a As Integer
    Dim Files() As String
    Dim FilesArray As New ArrayList
    Dim FilesInfo As New ArrayList
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub
    Private Sub TransFiles(ByVal strDirect As String)
        Try
            If Not (strDirect Is Nothing) Then
                Dim mFileInfo As System.IO.FileInfo
                Dim mDir As System.IO.DirectoryInfo
                Dim mDirInfo As New System.IO.DirectoryInfo(strDirect)
                For Each mFileInfo In mDirInfo.GetFiles() '取文件夹目录
                    Debug.Print(mFileInfo.FullName)
                    'FilesArray.Add(mFileInfo.FullName + "   大小:  " + (Int(mFileInfo.Length.ToString / 1024) + 1).ToString + "kb" + "  最后修改时间: " + mFileInfo.LastWriteTime.ToString())
                    FilesInfo.Add(mFileInfo)
                Next
                For Each mDir In mDirInfo.GetDirectories '取文件夹
                    TransFiles(mDir.FullName)
                Next
            End If
        Catch ex As Exception


        End Try


    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If String.IsNullOrEmpty(TextBox1.Text) Then
            MessageBox.Show("目標選択してください!!")
            TextBox1.Focus()
            Return
        End If
        If Not System.IO.Directory.Exists(TextBox1.Text) Then
            MessageBox.Show("目標不正!!")
            TextBox1.Focus()
            TextBox1.Select()
            Return
        End If
        FileView.Items.Clear() 'clear
        FilesInfo.Clear()
        TransFiles(TextBox1.Text)
        Dim listitem As ListViewItem
        Dim indexile As Integer = 0
        indexile = TextBox1.Text.LastIndexOf("\")
        Dim fieselename As String = TextBox1.Text.Substring(0, indexile)
        For a = 0 To FilesInfo.Count - 1
            indexile = CType(FilesInfo.Item(a), System.IO.FileInfo).FullName.LastIndexOf("\")
            listitem = FileView.Items.Add(CType(FilesInfo.Item(a), System.IO.FileInfo).FullName.Replace(CType(FilesInfo.Item(a), System.IO.FileInfo).Name.ToString, "").Replace(fieselename, ""))
            listitem.SubItems.Add(CType(FilesInfo.Item(a), System.IO.FileInfo).Name)
            listitem.SubItems.Add(Format(CType(CType(FilesInfo.Item(a), System.IO.FileInfo).LastWriteTime.ToString(), Date), "yyyy/mm/dd hh:mm"))
            listitem.SubItems.Add((Int(CInt(CType(FilesInfo.Item(a), System.IO.FileInfo).Length.ToString) / 1024) + 1).ToString)
            Dim indexPosint As Integer = CType(FilesInfo.Item(a), System.IO.FileInfo).Name.ToString.IndexOf(".")
            listitem.SubItems.Add(CType(FilesInfo.Item(a), System.IO.FileInfo).Name.Substring(indexPosint + 1))
        Next
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            TextBox1.Text = FolderBrowserDialog1.SelectedPath()
        End If
    End Sub


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.Close()
    End Sub


    Private Sub btn_csv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_csv.Click
        Dim filestream As StreamWriter
        If String.IsNullOrEmpty(csvpath.Text) Then
            MessageBox.Show("CSV標選択してください!!")
            Return
        End If
        Dim w_filePath As String = csvpath.Text
        Dim w_filename As String = "CSV一覧" + Format(Now(), "yyyymmddhhmmss") + ".csv"
        filestream = createCsvFile(w_filePath & w_filename)
        'CSV明細を設定
        Call writeCsvDetail(filestream)
        filestream.Flush()
        filestream.Close()
        'MessageBox.Show("CSV出力完了詳細: " + w_filePath & w_filename + "参照してください!!!")
        Dim Message As String = "CSV出力完了詳細: " + w_filePath & w_filename + "参照しますか?"
        Dim Caption As String = "Error Detected in Input"
        Dim Buttons As MessageBoxButtons = MessageBoxButtons.YesNo
        Dim Result As DialogResult
        Result = MessageBox.Show(Message, Caption, Buttons)
        If Result = System.Windows.Forms.DialogResult.Yes Then
            Dim obj_xls As Object
            obj_xls = CreateObject("Excel.Application")
            obj_xls.displayAlerts = False
            obj_xls.Application.Visible = True
            Call obj_xls.workbooks.open(w_filePath & w_filename)         '打开CSV
        End If
    End Sub
    Private Function createCsvFile(ByVal path As String) As StreamWriter
        Dim filestream As StreamWriter = Nothing
        Try
            If (System.IO.File.Exists(path)) Then
                System.IO.File.Delete(path)
            End If
            filestream = New StreamWriter(path, True, System.Text.UnicodeEncoding.Default)
            Return filestream
        Catch ex As Exception
            If Not IsNothing(filestream) Then
                filestream.Close()
            End If
            Throw ex
        Finally
        End Try
    End Function
    Private Sub writeCsvDetail(ByVal fs As StreamWriter)
        Dim indexPos As Integer = 0
        Dim indexile As Integer = 0
        indexile = TextBox1.Text.LastIndexOf("\")
        Dim fieselename As String = TextBox1.Text.Substring(0, indexile)
        fs.Write("完全名前,名前,サイズ,更新時間,種類")
        fs.Write(vbCrLf)
        For a = 0 To FilesInfo.Count - 1
            fs.Write(vbTab & CType(FilesInfo.Item(a), System.IO.FileInfo).FullName.ToString.Replace(CType(FilesInfo.Item(a), System.IO.FileInfo).Name.ToString, "").Replace(fieselename, ""))
            fs.Write(",")
            fs.Write(vbTab & CType(FilesInfo.Item(a), System.IO.FileInfo).Name)
            fs.Write(",")
            fs.Write(vbTab & (Int(CInt(CType(FilesInfo.Item(a), System.IO.FileInfo).Length.ToString) / 1024) + 1).ToString)
            fs.Write(",")
            fs.Write(vbTab & Format(CType(CType(FilesInfo.Item(a), System.IO.FileInfo).LastWriteTime.ToString(), Date), "yyyy/mm/dd hh:mm"))
            fs.Write(",")
            indexPos = CType(FilesInfo.Item(a), System.IO.FileInfo).Name.ToString.IndexOf(".")
            fs.Write(vbTab & CType(FilesInfo.Item(a), System.IO.FileInfo).Name.Substring(indexPos + 1))
            fs.Write(",")
            fs.Write(vbCrLf)
        Next
    End Sub


    Private Sub CsvSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CsvSelect.Click
        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            csvpath.Text = FolderBrowserDialog1.SelectedPath()
        End If
    End Sub
    'Private Sub filedisplay_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles filedisplay.SelectedIndexChanged
    '    Dim curFileDir As String
    '    Dim indexPos As Integer
    '    Dim pr As Process = New Process
    '    pr.StartInfo.FileName = "explorer.exe"
    '    indexPos = Me.filedisplay.SelectedItem.ToString.LastIndexOf("\")
    '    curFileDir = Me.filedisplay.SelectedItem.ToString.Substring(0, indexPos)
    '    pr.StartInfo.Arguments = Chr(34) & curFileDir & Chr(34)
    '    pr.Start()
    'End Sub


    '    Private Sub FileView_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileView.SelectedIndexChanged
    '        'Dim curFileDir As String
    '        'Dim indexPos As Integer
    '        'Dim pr As Process = New Process
    '        'pr.StartInfo.FileName = "explorer.exe"
    '        ''indexPos = Me.filedisplay.SelectedItem.ToString.LastIndexOf("\")
    '        ''curFileDir = Me.filedisplay.SelectedItem.ToString.Substring(0, indexPos)
    '        ''pr.StartInfo.Arguments = Chr(34) & curFileDir & Chr(34)
    '        pr.Start()
    '    End Sub
End Class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值