vb.net 重命名文件夹中的所有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28930272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Renaming all files in a folder
提问by Freshman
I'm wondering if it's possible to rename all the files in a folder with a simple program, using vb.NET
我想知道是否可以使用 vb.NET 使用简单的程序重命名文件夹中的所有文件
I'm quite green and not sure if this is even possible. Lets say there is a folder containing the files:
我很绿色,不确定这是否可能。假设有一个包含文件的文件夹:
Text_Space_aliens.txt, fishing_and_hunting_racoons.txt and mapple.txt.
Text_Space_aliens.txt、fishing_and_hunting_racoons.txt 和 mapple.txt。
Using a few credentials:
使用一些凭据:
Dim outPut as String = "TextFile_"
Dim fileType as String = ".txt"
Dim numberOfFiles = My.Computer.FileSystem.GetFiles(LocationFolder.Text)
Dim filesTotal As Integer = CStr(numberOfFiles.Count)
Will it be possible to rename these, regardless of previous name, example:
无论以前的名称如何,是否可以重命名这些,例如:
TextFile_1.txt, TextFile_2.txt & TextFile_3.txt
TextFile_1.txt、TextFile_2.txt 和 TextFile_3.txt
in one operation?
在一次手术中?
回答by Pilgerstorfer Franz
I think this should do the trick. Use Directory.GetFiles(..)to look for specific files. Enumerate results with a for..each and move(aka rename) files to new name. You will have to adjust sourcePathand searchPatternto work for you.
我认为这应该可以解决问题。使用Directory.GetFiles(..)查找特定文件。使用 for..each 枚举结果并将(又名重命名)文件移动到新名称。你将不得不调整并为你工作。sourcePathsearchPattern
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
In your title you state something about chronologically, but within your question you never mentioned it again. So I did another example ordering files by creationTime.
在您的标题中,您按时间顺序陈述了一些内容,但在您的问题中,您再也没有提到过。所以我做了另一个通过 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
I've never done Lambdas in VB.net but tested my code and it worked as intended. If anything goes wrong please let me know.
我从来没有在 VB.net 中做过 Lambdas,但测试了我的代码,它按预期工作。如果出现任何问题,请告诉我。

