我有一种情况,我有一个以YearMonthDate约定格式命名的多个文件,我想根据月份移动到一个目录,然后根据文件名中列出的日期移动到子目录。在我的帮助下,我得到了以下信息:
Public Class Form1
Function MakeDirectoryPath(rootFolder As String, fileName As String) As String
Dim monthDirectory As String = DateAndTime.MonthName(Asc(fileName(2)) - 96)
Dim dayDirectory As String = fileName.Substring(3, 2)
Return IO.Path.Combine(rootFolder, monthDirectory, dayDirectory)
End Function
Sub CreateDirectory(rootSource As String, rootDestination As String)
For Each s As String In IO.Directory.GetFiles(rootSource)
Dim fileName As String = IO.Path.GetFileName(s)
Dim destinationDirectory As String = MakeDirectoryPath(rootDestination, fileName)
If Not IO.Directory.Exists(destinationDirectory) Then
IO.Directory.CreateDirectory(destinationDirectory)
End If
**IO.File.Move(s, destinationDirectory)**
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
CreateDirectory(".....", "......")
End Sub
End Class
函数从文件名中提取我想要的内容,Sub CreateDirectory创建我的月目录,然后创建我的子目录。问题是我正在将实际文件移动到他们受尊重的目录中。
简单地掉线 IO.File.Move(destinationDirectory) 由于目录已经存在,For Each循环内部会产生异常,而且我对IO.File.Copy()没有更好的运气。
非常感谢帮助。
答案 0 :(得分:1)
.Move采用路径和文件名。在您的示例中,目标只是一个路径,您必须添加文件名。
IO.File.Move(s, IO.Path.Combine(destinationDirectory, fileName))