重命名一个目录中的多个文件

时间:2019-06-05 18:04:05

标签: asp.net vb.net

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

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

A123.txt B456.txt C567.txt

将可以通过以下操作重命名这些文件:

A_1.txt B_2.txt B_3.txt

1 个答案:

答案 0 :(得分:0)

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

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)