首页 > 解决方案 > 重命名一个目录中的多个文件

问题描述

是否可以使用 vb.NET 使用简单程序重命名文件夹中的所有文件

假设有一个包含文件的文件夹,每次加载文件都会有所不同:

A123.txt B456.txt C567.txt

是否可以在一次操作中重命名这些文件,如下所示:

A_1.txt B_2.txt B_3.txt

标签: asp.netvb.net

解决方案


这可以简化,但这里是长版本,每一步都有注释

Imports System.IO


' get your filenames to be renamed
Dim filenames = Directory.GetFiles("c:\path\with\files")
' order them by whatever rule you choose (here it is by modification date)
Dim orderedFilenames = filenames.OrderBy(Function(s) File.GetLastWriteTime(s))
' rename the files into a dictionary<oldname, newname>
' will result in filename X_Y.Z where X is first letter, Y is order index, Z is extension
Dim newNameSelector =
    Function(s As String, i As Integer)
        Return New KeyValuePair(Of String, String)(s,
            Path.Combine(Path.GetDirectoryName(s), $"{Path.GetFileName(s)(0)}_{i}{Path.GetExtension(s)}"))
    End Function
' get new names using the newNameSelector function
Dim newNames = orderedFilenames.Select(newNameSelector)
' define the operation to rename the file using a keyvaluepair<oldname, newname>
Dim renameMethod = Sub(kvp As KeyValuePair(Of String, String)) File.Move(kvp.Key, kvp.Value)
' either sequential rename ...
For Each newName In newNames
    renameMethod(newName)
Next
' ... or it could be multi threaded
Parallel.ForEach(newNames, renameOperation)

笔记:

  • 基于使用文件索引的重命名规则,不需要进行 File.Exists 检查。如果您更改规则,则可能有必要。您的问题正文中的规则不明确。这假设它只运行一次。
  • 字典键将是唯一的,因为 Windows 不会有重复的文件名

要回答您的问题,Will it be possible to rename these files in one operation[?]不,但我们可以在一个“行”中完成,甚至可以使用多个线程。只需修改路径字符串“c:\path\with\files”并运行此代码

Parallel.ForEach(Directory.GetFiles("c:\path\with\files").OrderBy(Function(s) File.GetLastWriteTime(s)).Select(Function(s, i) New KeyValuePair(Of String, String)(s, Path.Combine(Path.GetDirectoryName(s), $"{Path.GetFileName(s)(0)}_{i}{Path.GetExtension(s)}"))), Sub(kvp) File.Move(kvp.Key, kvp.Value))

我认为您可能需要制定重命名逻辑。这就是我在长代码示例中将其分开的原因。只需处理里面的逻辑newNameSelector

如果您想按名称订购,只需

Dim orderedFilenames = filenames.OrderBy(Function(s) s)

这将从您的文件名中删除“_20190607”

Dim newNameSelector =
    Function(s As String)
        Return New KeyValuePair(Of String, String)(s,
            Path.Combine(Path.GetDirectoryName(s), String.Format("{0}{1}", Path.GetFileName(s).Split("_"c)(0), Path.GetExtension(s))))
    End Function

我知道这个答案越来越难,但 OP 不断提供新信息,这意味着问题正在发生变化,因此答案也是如此。根据他最近的评论,应该只重命名每个前缀中最早的一个。

' group the filenames into groups using key: "prefix_"
Dim groupedFilenames = Directory.GetFiles("c:\path\with\files").GroupBy(Function(s) Path.GetFileName(s).Split("_"c)(0))
Dim filenames As New List(Of String)()
' add the min from each group, using the name as the comparer
For Each g In groupedFilenames
    filenames.Add(g.Min(Function(s) s))
Next
' rename the files into a dictionary<oldname, newname>
' will result in filename X_Y.Z where X is first letter, Y is order index, Z is extension
Dim newNameSelector =
    Function(s As String)
        Return New KeyValuePair(Of String, String)(s,
            Path.Combine(Path.GetDirectoryName(s), String.Format("{0}{1}", Path.GetFileName(s).Split("_"c)(0), Path.GetExtension(s))))
    End Function
' get new names using the newNameSelector function
Dim newNames = filenames.Select(newNameSelector)
' define the operation to rename the file using a keyvaluepair<oldname, newname>
Dim renameOperation = Sub(kvp As KeyValuePair(Of String, String)) File.Move(kvp.Key, kvp.Value)
' ... or it could be multi threaded
Parallel.ForEach(newNames, renameOperation)

推荐阅读