重命名文件夹和子文件夹中所有文件的结尾(从* .log到* .txt)

问题描述 投票:0回答:1

是否可以使用vb.NET通过简单程序重命名文件夹和子文件夹中的所有文件。我很绿,不确定这是否可能。

D:\ Main \

文件始终以“ this is a test.log”或“ this_is_the_second.log”结尾。所有结尾的文件都重命名为“ txt”,例如“ this is a test.txt”或“ this_is_the_second.txt”。

我已经尝试了所有这段代码,但是没有用:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    renameFilesInFolder()
End Sub
Private Sub renameFilesInFolder()
    Dim sourcePath As String = "D:\Main"
    Dim searchPattern As String = "*.log"
    For Each fileName As String In Directory.GetFiles(sourcePath, searchPattern, SearchOption.AllDirectories)
        File.Move(Path.Combine(sourcePath, fileName), Path.Combine(sourcePath, ".txt"))

    Next
End Sub

请帮助我!?!?!

vb.net file-rename
1个回答
0
投票
Private Sub RenameFilesInFolder()
    Dim sourcePath As String = "D:\Main"
    Dim searchPattern As String = "*.log"

    For Each currentFilePath As String In Directory.GetFiles(sourcePath, searchPattern, SearchOption.AllDirectories)
        Dim newFilePath = Path.ChangeExtension(currentFilePath, ".txt")

        File.Move(currentFilePath, newFilePath)
    Next
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.