2008年5月29日 星期四

VB.net Directory與DirectoryInfo的運用_資料夾管理工具

由於自己有許多的音樂CD以及音樂檔案,透過一個個資料夾去進行整理與分類,為了個人習慣的問題,因此設計一個資料夾工具,主要提供資料夾名稱更改的功能,需求如下:

1.選取資料夾 (開啟A資料夾)
2.匯入選取的資料夾中所含子資料夾 (匯入A中的AA BB CC三個資料夾)
3.更改子資料夾內的所含子資料夾的名稱,使其與子資料夾相同 (將AA中的子資料夾改名為AA,將BB中的子資料夾改名為BB,將CC中的子資料夾改名為CC)

真遶口...算了 直接紀錄吧!!



Imports System.IO
Public Class 資料夾名稱管理
  Dim dir_Directory As DirectoryInfo '將最裡層的選擇資料夾 設為 DirectoryInfo物件 名為dir_Directory
  'd_str_tempFolderPath - 左邊list裡資料夾的路徑
  'd_str_tempFolderName - 左邊list裡選中的資料夾名稱
  Dim d_str_tempFolderPath, d_str_tempFolderName As String
  Private Sub obj_開啟_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles obj_開啟.Click
    obj_folderBD.ShowDialog()
  If obj_folderBD.SelectedPath.Count > 0 Then obj_listbox1.DataSource = Directory.GetDirectories(obj_folderBD.SelectedPath, "*.*")
    sub_歸零()
  End Sub 'obj_bt1_Click

  Private Sub obj_改名_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles obj_改名.Click
    Do While Directory.Exists(d_str_tempFolderPath & "\" & d_str_tempFolderName)
      d_str_tempFolderName &= "I"
    Loop
    dir_Directory.MoveTo(d_str_tempFolderPath & "\" & d_str_tempFolderName)
    obj_文字.Text = "完成"
    sub_歸零()
  End Sub 'obj_改名_Click

  Private Sub obj_listbox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles obj_listbox1.Click, obj_listbox1.SelectedIndexChanged
        sub_歸零()
    If obj_listbox1.Items.Count > 0 Then
      '將listBox2的來源設為 listbox1中選取的資料夾
      obj_ListBox2.DataSource = Directory.GetDirectories(obj_listbox1.Items(obj_listbox1.SelectedIndex.ToString).ToString, "*.*")
      '給定d_str_tempFolderPath與d_str_tempFolderName的值
      d_str_tempFolderPath = obj_listbox1.Items(obj_listbox1.SelectedIndex.ToString).ToString
      Dim dir_temp As New DirectoryInfo(obj_listbox1.Items(obj_listbox1.SelectedIndex.ToString).ToString)
      d_str_tempFolderName = dir_temp.Name
      obj_文字_資料夾.Text = dir_temp.Name
    End If
  End Sub 'obj_listbox1_Click

  Private Sub obj_ListBox2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles obj_ListBox2.Click, obj_ListBox2.SelectedIndexChanged
    If obj_ListBox2.Items.Count > 0 Then
      obj_改名.Enabled = True
      '將最裡層的選擇資料夾 設為 DirectoryInfo物件 名為dir_Directory
      dir_Directory = New DirectoryInfo(obj_ListBox2.Items(obj_ListBox2.SelectedIndex.ToString).ToString)
      obj_文字_子資料夾.Text = dir_Directory.Name
    End If
  End Sub 'obj_ListBox2_Click

  Private Sub sub_歸零()
    d_str_tempFolderPath = Nothing
    d_str_tempFolderName = Nothing
    If d_str_tempFolderPath = Nothing Or d_str_tempFolderName = Nothing Then
      obj_改名.Enabled = False
    End If
  End Sub 'sub_歸零()


  Private Sub obj_批次改名_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles obj_批次改名.Click
    'dir_tempDir_1 表開啟之資料夾的類別
    'dir_tempDir_2 表開啟之資料夾中子資料夾的類別
    Dim dir_tempDir_1 As New DirectoryInfo(obj_folderBD.SelectedPath)
    Dim Ary_dir_1 As DirectoryInfo() = dir_tempDir_1.GetDirectories
    For i As Integer = 0 To Ary_dir_1.Length - 1
      Dim dir_tempDir_2 As New DirectoryInfo(Ary_dir_1(i).FullName)
      Dim Ary_dir_2 As DirectoryInfo() = dir_tempDir_2.GetDirectories
      '至此    Ary_dir_1 表左邊list的資料夾完整路徑 array
      '        Ary_dir_2 表右邊list的資料夾完整路徑 array
      d_str_tempFolderPath = dir_tempDir_2.FullName
      d_str_tempFolderName = dir_tempDir_2.Name
      If Ary_dir_2.Length > 0 Then
        For Each tmp In Ary_dir_2
          Do While Directory.Exists(d_str_tempFolderPath & "\" & d_str_tempFolderName)
            d_str_tempFolderName &= "I"
          Loop
        tmp.MoveTo(d_str_tempFolderPath & "\" & d_str_tempFolderName)
        Next
      End If
    Next
    obj_文字.Text = "完成批次更名"
  End Sub
End Class


'obj_listbox1.Items(0).ToString 得到listbox中第一個選項目
'obj_listbox1.SelectedIndex.ToString 得到listbox被選取的項目的index


'--------宣告法一
'    Dim dir_temp As DirectoryInfo
'    dir_temp = New DirectoryInfo("C:\Documents and Settings\g_user\桌面\誠工具")
'--------宣告法二
'    Dim dir_temp2 As New DirectoryInfo("C:\Documents and Settings\g_user\桌面\測試dir")
'
'--------宣告並同時建立子資料夾
'    Dim dir_temp2 As New DirectoryInfo("C:\Documents and Settings\g_user\桌面\測試dir")
'    Dim dir_sub1 As DirectoryInfo = dir_temp2.CreateSubdirectory("subDir1")
'    Dim dir_sub2 As DirectoryInfo = dir_temp2.CreateSubdirectory("subDir2")
'    Dim dir_sub3 As DirectoryInfo = dir_temp2.CreateSubdirectory("subDir3")

沒有留言: