摘要:檔案目錄自動整理(1)
有些時候我們的檔案太多了,EX:抓了太多的圖形檔, 導致目錄亂七八糟
所以我自己有寫了個程式可以自動把檔案依照類別, 日期和大小去歸類
並能自動整理到依照年月日建立出來的目錄裡面
這樣做對於喜歡使用數位相機來照相的人會有些幫助(我個人一個月約照個幾千張 )
尤其記憶卡越來越大, 每次加入照片都會很頭痛
下面先來個簡單的範例
01
' 按下這個按鈕以後就會自動的把目標目錄的檔案
02
' 整理到PIC目錄中, 並且會按照年月當目錄來存放
03
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
04
If Me.FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
05
Dim 來源目錄 As String = Me.FolderBrowserDialog1.SelectedPath
06
07
整理檔案(來源目錄)
08
End If
09
End Sub
10
11
' 自動把這個路徑裡面的檔案遞迴式的搜尋出來
12
' 並且全部寫到PIC裡面去
13
Private Sub 整理檔案(ByVal 路徑 As String)
14
Dim a As Integer
15
Dim F() As String = IO.Directory.GetFiles(路徑)
16
For a = 0 To UBound(F)
17
放置到正確位置(F(a))
18
Next
19
20
Dim D() As String = IO.Directory.GetDirectories(路徑)
21
For a = 0 To UBound(D)
22
整理檔案(D(a))
23
Next
24
End Sub
25
26
Private Sub 放置到正確位置(ByVal 檔案 As String)
27
Dim d As Date = IO.File.GetLastWriteTime(檔案)
28
Dim 日期 As String = d.Year.ToString + Format(d.Month, "0#")
29
If IO.Directory.Exists("pic\" + 日期) = False Then
30
IO.Directory.CreateDirectory("pic\" + 日期)
31
End If
32
33
IO.File.Copy(檔案, "pic\" + 日期 + "\" + IO.Path.GetFileName(檔案))
34
End Sub
' 按下這個按鈕以後就會自動的把目標目錄的檔案 02
' 整理到PIC目錄中, 並且會按照年月當目錄來存放 03
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 04
If Me.FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then 05
Dim 來源目錄 As String = Me.FolderBrowserDialog1.SelectedPath 06
07
整理檔案(來源目錄) 08
End If 09
End Sub 10
11
' 自動把這個路徑裡面的檔案遞迴式的搜尋出來 12
' 並且全部寫到PIC裡面去 13
Private Sub 整理檔案(ByVal 路徑 As String) 14
Dim a As Integer 15
Dim F() As String = IO.Directory.GetFiles(路徑) 16
For a = 0 To UBound(F) 17
放置到正確位置(F(a)) 18
Next 19
20
Dim D() As String = IO.Directory.GetDirectories(路徑) 21
For a = 0 To UBound(D) 22
整理檔案(D(a)) 23
Next 24
End Sub 25
26
Private Sub 放置到正確位置(ByVal 檔案 As String) 27
Dim d As Date = IO.File.GetLastWriteTime(檔案) 28
Dim 日期 As String = d.Year.ToString + Format(d.Month, "0#") 29
If IO.Directory.Exists("pic\" + 日期) = False Then 30
IO.Directory.CreateDirectory("pic\" + 日期) 31
End If 32
33
IO.File.Copy(檔案, "pic\" + 日期 + "\" + IO.Path.GetFileName(檔案)) 34
End Sub上面這個範例使用了遞迴的技巧, 其實遞迴可以做很多事情
畫面上需要拉一個按鈕去加上第一段程式碼
還需要一個FolderBrowserDialog1 物件來做目錄的瀏覽動作
這樣就可以像我一樣, 相機天天亂照一通, 然後每隔一段時間就把那8G的內容(約5000張)
給全自動整理進硬碟裡, 日後可以用另外的讀取程式來輕易依照日期和格式分類來搜尋/瀏覽檔案
順著天賦做事,逆著個性做人生命, 就該浪費在美好的事物上
Private
Private