重命名文件夹和子文件夹中所有文件的结尾(*.log 到 *.txt)
Renaming ending of all files in a folder and subfolder (*.log to *.txt)
是否可以使用一个简单的程序重命名文件夹和子文件夹中的所有文件,使用 vb.NET
我很新手,不确定这是否可能。
D:\Main\ <--- 有 10 个文件。
D:\Main\Sub\ <--- 还有一些文件
...
文件总是以 "log" 结尾,例如 "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
请帮帮我!?!?!
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
GetFiles
方法获取完整的文件路径,而不仅仅是名称。 Path.Combine(sourcePath, fileName)
会产生像 "D:\MainD:\Main\Sub\test.log" 这样的值,而 Path.Combine(sourcePath, ".txt")
会产生 "D:\Main.txt"[=18 这样的值=]每次。
是否可以使用一个简单的程序重命名文件夹和子文件夹中的所有文件,使用 vb.NET 我很新手,不确定这是否可能。
D:\Main\ <--- 有 10 个文件。 D:\Main\Sub\ <--- 还有一些文件 ...
文件总是以 "log" 结尾,例如 "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
请帮帮我!?!?!
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
GetFiles
方法获取完整的文件路径,而不仅仅是名称。 Path.Combine(sourcePath, fileName)
会产生像 "D:\MainD:\Main\Sub\test.log" 这样的值,而 Path.Combine(sourcePath, ".txt")
会产生 "D:\Main.txt"[=18 这样的值=]每次。