我認爲這應該做的伎倆。使用Directory.GetFiles(..)查找特定文件。枚舉for..each和move(又名)文件的結果爲新名稱。你將不得不調整sourcePath
和searchPattern
爲你工作。
Private Sub renameFilesInFolder()
Dim sourcePath As String = "e:\temp\demo"
Dim searchPattern As String = "*.txt"
Dim i As Integer = 0
For Each fileName As String In Directory.GetFiles(sourcePath, searchPattern, SearchOption.AllDirectories)
File.Move(Path.Combine(sourcePath, fileName), Path.Combine(sourcePath, "txtFile_" & i & ".txt"))
i += 1
Next
End Sub
在您的標題中,您按照時間順序介紹了某些內容,但在您的問題中,您再也沒有提及過它。所以我做了另一個例子由creationTime排序文件。
Private Sub renameFilesInFolderChronologically()
Dim sourcePath As String = "e:\temp\demo"
Dim searchPattern As String = "*.txt"
Dim curDir As New DirectoryInfo(sourcePath)
Dim i As Integer = 0
For Each fi As FileInfo In curDir.GetFiles(searchPattern).OrderBy(Function(num) num.CreationTime)
File.Move(fi.FullName, Path.Combine(fi.Directory.FullName, "txtFile_" & i & ".txt"))
i += 1
Next
End Sub
我從來沒有在VB.net做過Lambdas,但測試過我的代碼,它按預期工作。如果出現任何問題,請告訴我。
使用FileInfo類和DirectoryInfo類,獲取文件,使用desidered Date屬性對它們進行排序,然後啓動重命名操作。當你的嘗試有問題時回到這裏 – Steve 2015-03-08 18:41:30