listbox1列出包含文件的子文件夹。
listbox2有文件列表。
当按下button1时,我希望为listbox2中的每个文件创建文件夹(文件夹名称应与文件名相同),然后将相应的文件移动到该目录。
eg)
listbox1
d:\data\sub1\
listbox2
d:\data\sub1\a.7z
d:\data\sub1\ab.7z
when button1 is pushed
we can find the files in...
d:\data\sub1\a\a.7z
d:\data\sub1\ab\a.7z
我很难做到。我知道如何在列表框中列出文件,但我不知道如何处理它们。
另外,如果我尝试使用以下代码删除目录名中的7z扩展名,则表示它不能用于列表框。
If folderslist.SelectedItem IsNot Nothing Then
' selected item is filepath
Dim filePath = folderslist.SelectedItem.ToString
The string you are searching
Dim s As String = filePath
Find index of uppercase letter 'B'
Dim i As String = 0
Dim j As String = s.IndexOf("."c)
This new string contains the substring starting at B
part = s.Substring(i, j - i + 1)
If (s.IndexOf(".") = -1) Then
part = "Not found"
End If
请提出任何建议。
答案 0 :(得分:1)
您无需手动拆分路径即可获取单独的字符串。使用
System.IO.Path.GetFileName
和System.IO.Path.GetFileNameWithoutExtension
和System.IO.Path.GetDirectoryName
。之类的。
给出一个直接的,明确的例子有点困难,因为根据你所展示的内容,你不清楚你想做什么。但是作为一个示例......
Dim basePath as String = "d:\data\sub1\"
Dim fullName as String = folderslist.SelectedItem.ToString()
Dim fileName as String = Path.GetFileName(fullName)
Dim partialName as String = Path.GetFileNameWithoutExtension(fullName)
Dim newPath as String = Path.Combine(basePath, partialName)
newPath = newPath + Path.Combine(newPath, fileName)
答案 1 :(得分:0)
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
If folderslist.SelectedItem IsNot Nothing Then
Dim filePath = folderslist.SelectedItem.ToString
fileslist.Items.Clear()
Dim dirf As String() = Directory.GetFiles(filePath, "*.7z")
Dim dira As String
For Each dira In dirf
fileslist.Items.Add(dira)
Next
If mkfold = 1 Then
For Each dira In dirf
Dim pName As String = Path.GetFileNameWithoutExtension(dira)
Dim strDir As String
strDir = filePath & "\" & pName
If DirExists(Trim(strDir)) = False Then
MkDir(Trim(strDir))
End If
Dim f_name As String = Path.GetFileName(dira)
My.Computer.FileSystem.MoveFile(dira, strDir & "\" & f_name)
Next
mkfold = 0
End If
End If
Private Sub mkfol_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mkfol.Click
mkfold = 1
End Sub
不知怎的,我得到了我想要的东西。