VB.net FileSystem.rename - 只知道一个子字符串的旧路径
VB.net FileSystem.rename - old path knowing only a substring
如何重命名一个我不知道全名但只知道它以基本字符串开头的文件?
我必须重命名文件夹中的文件,该文件以默认字符串开头,然后有额外的未知字符。我确定该文件夹中只有一个以该字符串开头的文件。
这类似于搜索 "string*.txt" 并用 "string.txt" 重命名它,但是 FileSystem.rename 不接受以“*”作为参数的旧路径。
您需要遍历给定目录中的所有文件,如果一个名称匹配,那么您就知道这是您的文件。
代码结构可能如下所示:
Function LookForName(Path As String) As String
'For Each File in your path
'If the name starts with "string" and ends with ".txt"
'You can return this filename
End Function
'You call LookForName with a given path
'You rename the returned file
Dim _files as String() = IO.Directory.GetFiles("c:\temp\", "string*.txt")
IO.File.Move(_files(0), "c:\temp\newfilename.txt")
仍然需要检查是否找到文件等,但这应该可行
如何重命名一个我不知道全名但只知道它以基本字符串开头的文件? 我必须重命名文件夹中的文件,该文件以默认字符串开头,然后有额外的未知字符。我确定该文件夹中只有一个以该字符串开头的文件。 这类似于搜索 "string*.txt" 并用 "string.txt" 重命名它,但是 FileSystem.rename 不接受以“*”作为参数的旧路径。
您需要遍历给定目录中的所有文件,如果一个名称匹配,那么您就知道这是您的文件。
代码结构可能如下所示:
Function LookForName(Path As String) As String
'For Each File in your path
'If the name starts with "string" and ends with ".txt"
'You can return this filename
End Function
'You call LookForName with a given path
'You rename the returned file
Dim _files as String() = IO.Directory.GetFiles("c:\temp\", "string*.txt")
IO.File.Move(_files(0), "c:\temp\newfilename.txt")
仍然需要检查是否找到文件等,但这应该可行