目的是移动指定文件夹中的文件,如果日期至少是今天的一天。我在理论上找不到功能 FirstIndexOf 与 LastIndexOf 类似,我想在Dim文件名中对文件路径的开头进行子串,以获取名称该文件并将其解析为日期类型。由于动态原因,我不想删除35个字符,但我不知道还能做什么。 VS2005 .NET 2.0
Sub CopytoArchive(ByVal mydirpath As String)
'Dim mydirpath As String = "C:\UTResults\"
'C:\UTResults\Press3\sv70206655\data07012015.txt is an example of txtFileList
'12345678901234567890123456789012345
Dim txtFileList As String() = Directory.GetFiles(mydirpath, "*.txt", SearchOption.AllDirectories) 'Search all files in the given path with .txt type
For Each txtName As String In txtFileList 'C:\UTResults\Press3\sv70206655\data07012015.txt 'is an example of txtFileList
Dim pressname As String = txtName.Substring(0, txtName.LastIndexOf("\")) 'C:\UTResults\Press3\sv70206655 'take out the file extension
pressname = pressname.Substring(0, pressname.LastIndexOf("\")) 'C:\UTResults\Press3 'take out the folder after the press folder
pressname = pressname.Remove(0, 13) 'Press3 'for a clean "PRESS" look
Dim folderexists As String = "C:\writetest\" + pressname
Dim filename = txtName.Substring(txtName.LastIndexOf("."))
filename = filename.Remove(0, 35)
Dim datestring = DateTime.ParseExact(filename, "MMddyyyy", Nothing)
If datestring < Now.AddDays(-1) Then
My.Computer.FileSystem.CreateDirectory(folderexists)
My.Computer.FileSystem.MoveFile(txtName, folderexists + "\" + "data" + filename + ".txt")
End If
Next
End Sub
答案 0 :(得分:0)
Dim filename = Path.GetFileName(txtName)