-1

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

任何建议,请。

4

2 回答 2

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)
于 2013-06-11T09:19:54.783 回答
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

不知怎的,我得到了我想要的。

于 2013-06-11T10:39:41.370 回答