簡體   English   中英

從列表框中進行多項選擇以重命名文件夾

[英]Multiple Selections from Listbox to rename Folders

有一個 vb.net 用戶表單,列表框設置為多選。 這是供個人使用。 當表單加載指定文件夾中的所有子文件夾(僅按名稱)時,列表框會自行填充。 我想要 append 列表框中每個選定文件夾的前綴。 默認情況下,所有文件夾都有前綴,比方說 X。因此,例如,如果選擇 Xfolder1 並按下提交按鈕(而不是在列表框更改時),則 Xfolder1 變為 folder1。

到目前為止,這是我的代碼和偽代碼。 獲取字符串錯誤。 只有第一個子,加載表單和填充列表有效。 非常感謝您的幫助。 在此大流行期間,所有人的健康和安全。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For Each folder As String In System.IO.Directory.GetDirectories("D:\TestFolder\")
        ListBox1.Items.Add(Path.GetFileName(folder))
    Next
End Sub

這是ofc偽代碼

Private Sub RenameFolders(sender As Object, e As EventArgs) Handles Button1.Click
    For i = 0 To ListBox1.Items.Count - 1         
        If Prefix Exists Then
            FileIO.FileSystem.RenameDirectory(Prefix & FolderName, FolderName)
        Else
            FileIO.FileSystem.RenameDirectory(FolderName, Prefix & FolderName)
        End If
    Next
End Sub

以上是什么,還有很多研究。 問題可能在於我的字符串是完整路徑還是只是文件夾名稱。 列表框返回文件夾名稱,但這是代碼返回的內容嗎? 非常困惑。

你好。 抱歉,不清楚。 因為列表框是在加載時填充的,所以它將顯示文件夾的當前 state。 這可能是 Xfolder1、folder2、xfolder3 等。它將是當前存在的文件夾名稱。

另一種看待它的方式。

選擇文件夾將在點擊提交時從所有選定的文件夾中刪除任何前綴。 當點擊提交時,不選擇文件夾會將前綴添加到所有未選擇的文件夾中。

如果 xfolder1 出現在列表框中並被選中,它將成為 folder1。 如果 xfolder1 出現在列表框中但未被選中,它仍然是 xfolder1。 如果 folder1 出現在列表框中並且被選中,它仍然是 folder1。 如果 folder1 出現在列表框中但未被選中,它將更改為 xfolder1

我希望這更有意義?

假設我現在正確理解您的問題,我建議您使用DirectoryInfo class。您可以為父文件夾創建一個,然后為子文件夾獲取一個數組。 然后您可以將該數組綁定到ListBox並顯示Name屬性,它只是文件夾名稱,同時仍然可以訪問FullName屬性,它是完整路徑。 它還具有一個Exists屬性和一個用於重命名的MoveTo方法。 以下是我將如何做你所要求的:

Imports System.IO

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim folderPath = Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "Test")
        Dim folder As New DirectoryInfo(folderPath)
        Dim subFolders = folder.GetDirectories()

        With ListBox1
            .DisplayMember = "Name"
            .DataSource = subFolders
        End With
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Const prefix As String = "X"

        For Each selectedSubFolder As DirectoryInfo In ListBox1.SelectedItems
            'This is required to update the Exists property if the folder has been deleted since loading.
            selectedSubFolder.Refresh()

            If selectedSubFolder.Exists Then
                Dim parentFolderPath = selectedSubFolder.Parent.FullName
                Dim folderName = selectedSubFolder.Name

                If folderName.StartsWith(prefix) Then
                    folderName = folderName.Substring(prefix.Length)
                Else
                    folderName = prefix & folderName
                End If

                Dim folderPath = Path.Combine(parentFolderPath, folderName)

                selectedSubFolder.MoveTo(folderPath)
            End If
        Next
    End Sub

End Class

請注意,這不會按原樣更新ListBox 如果您也想要它,那么我將通過添加BindingSource來實現:

Imports System.IO

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim folderPath = Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "Test")
        Dim folder As New DirectoryInfo(folderPath)
        Dim subFolders = folder.GetDirectories()

        BindingSource1.DataSource = subFolders

        With ListBox1
            .DisplayMember = "Name"
            .DataSource = BindingSource1
        End With
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Const prefix As String = "X"

        For Each selectedSubFolder As DirectoryInfo In ListBox1.SelectedItems
            'This is required to update the Exists property if the folder has been deleted since loading.
            selectedSubFolder.Refresh()

            If selectedSubFolder.Exists Then
                Dim parentFolderPath = selectedSubFolder.Parent.FullName
                Dim folderName = selectedSubFolder.Name

                If folderName.StartsWith(prefix) Then
                    folderName = folderName.Substring(prefix.Length)
                Else
                    folderName = prefix & folderName
                End If

                Dim folderPath = Path.Combine(parentFolderPath, folderName)

                selectedSubFolder.MoveTo(folderPath)
            End If
        Next

        BindingSource1.ResetBindings(False)
    End Sub

End Class

請注意,這仍然不會求助於數據,但如果您需要,我會把它留給您。 原因是您可以對Name上的DirectoryInfo數組進行簡單排序,但這將執行直接字母排序, ListBox已經可以做到這一點。 如果你想要像文件資源管理器那樣的邏輯排序,其中文件夾名稱中的實際數字按數字排序,那么你也需要使用 Windows APi function,這超出了這個問題的 scope。 如果需要,請參閱此處了解更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM