遍历指定文件下所有的文件夹和文件的路径,文件名,修改日期,类型
2014-04-16 16:03
806 查看
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
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
相关文章推荐
- 获取文件夹中指定类型的所有文件名和文件大小
- 黑马程序员—这是一个能够键盘录入路径去批量修改指定文件夹下所有指定文件内容的方法
- 遍历读取指定文件夹下指定类型的所有文件
- VC-获取文件夹中的指定类型的所有文件名和文件大小
- Python获取指定路径下的某类型文件的所有文件名
- C#实现把指定文件夹下的所有文件复制到指定路径下以及修改指定文件的后缀名
- 获取指定目录下指定类型所有文件的全路径及文件名
- 遍历读取指定文件夹下指定类型的所有文件
- php遍历指定路径下的所有文件和文件夹
- c#如何实现对指定路径文件夹的遍历并复制里面所有文件到指定路径
- VC-获取文件夹中的指定类型的所有文件名和文件大小
- 读取指定文件夹中所有文件名以及文件路径,并读到matlab
- C#磁盘类型判断 所有磁盘遍历出所有文件夹及文件夹路径 所有文件及文件路径
- 输出指定文件夹中指定类型的所有文件路径
- 启动遍历文件夹中的所有指定的文件 ( 执行exe文件 或者加载dll )
- 遍历文件夹下面所有文件(或者指定文件)
- C# 使用foreach语句遍历指定文件夹中的所有文件或文件夹
- TreeView 显示指定路径下的所有文件和文件夹
- 非递归遍历文件夹下指定类型的文件-Java栈实现
- 通过递归查询指定目录下的所有文件和子文件夹下的指定文件名