2016-10-05 1 views
0

폴더의 파일 목록을 특정 순서로 목록 상자에 추가하려고합니다. 현재 숫자 태그를 기반으로 탐색기보기에서 파일을 정렬 한 다음 각 파일의 이름을 (1) .docx, (2) .docx, (3) .docx 등으로 이름을 바꿉니다. 내 문제는 (10) .docx와 (11) .docx를 칠 때 일어난다. 순서는 알파벳이 아니고 숫자가 아니기 때문이다. 파일이 목록 상자에 들어가면 (1), (10), (11), (12) 등의 순서로 나타납니다. 이 주문 문제를 해결하는 쉬운 방법이 있습니까? 등 의도 한대로 코드가 작동이 방법 코드 발췌목록 상자에 파일 정렬 알파벳순으로 및 숫자가 아닌 것

Private Sub GetFiles(strFolder As String) 
    If Right(strFolder, 1) <> Chr(92) Then strFolder = strFolder & Chr(92) 
    On Error GoTo lbl_Exit: 
    sName = Dir$(strFolder) 
    Me.ListFiles.Clear   'ListFiles is my listbox 
    Do While sName <> "" 
     AddItems Me.ListFiles, sName, strFolder 
     sName = Dir$ 
    Loop 
lbl_Exit: 
    Exit Sub 
End Sub 

답변

1

내가 해결 문제가 단지 이름을 지정하는 것입니다 볼 수있는 가장 쉬운 방법은 아래를 참조하십시오, A (01) DOCX을-파일 A (02). 어떤 의미가

Private Sub GetFiles(strFolder As String) 

    Dim DirArray() As Variant 
    ReDim Preserve DirArray(0 To 0) As Variant 

    Me.ListFiles.Clear 

    '---- Get the filenames into an array ---- 
    Dim sFileName As String 
    sFileName = Dir$(strFolder) 

    Do While sFileName <> vbNullString 

     DirArray(UBound(DirArray)) = sFileName 

     sFileName = Dir$ 

     If sFileName <> vbNullString Then 
      ReDim Preserve DirArray(0 To UBound(DirArray) + 1) As Variant 
     End If 
    Loop 

    '---- Sort the array ---- 
    Dim i As Integer 
    Dim j As Integer 

    Dim CompareTemp1 As String 
    Dim CompareTemp2 As String 

    For i = LBound(DirArray) To UBound(DirArray) 
     For j = i To UBound(DirArray) 

      '---- Compare two neighbouring filename-numbers 
      '---- If the one lower in the list has a lower number, they'll be switched around ---- 
      If GetNumbersFromFileName(CStr(DirArray(j))) < GetNumbersFromFileName(CStr(DirArray(i))) Then 

       '---- Change the neighbouring filenames order ---- 
       CompareTemp1 = DirArray(i) 
       CompareTemp2 = DirArray(j) 
       DirArray(i) = CompareTemp2 
       DirArray(j) = CompareTemp1 

      End If 
     Next j 
    Next i 
    '---- Once all this is done, the DirArray should have the desired order, with the undesireables on top ---- 

    '---- Display the new filename-order in your ListBox ---- 
    For i = 0 To UBound(DirArray) - 1 

     '---- Display only filenames with (##) ---- 
     If GetNumbersFromFileName(CStr(DirArray(i))) <> -1 Then 
      AddItems Me.ListFiles, (CStr(DirArray(i))), strFolder 
     End If 

    Next i 

    ReDim DirArray(0) As Variant 

End Sub 

Private Function GetNumbersFromFileName(sFileNameToCheck As String) As Integer 

    Dim iOpenBracketPosition As Integer 
    Dim iClosedBracketPosition As Integer 

    '---- Get the bracket-positions ---- 
    iOpenBracketPosition = InStr(1, sFileNameToCheck, "(") 
    iClosedBracketPosition = InStr(1, sFileNameToCheck, ")") 

    '---- In case one of the brackets is missing, the file will be pushed to the top ---- 
    If iOpenBracketPosition = 0 Or iClosedBracketPosition = 0 Then 
     GetNumbersFromFileName = -1 
     Exit Function 
    End If 

    '---- Return the Number in between the brackets ---- 
    GetNumbersFromFileName = CInt(Mid$(sFileNameToCheck, iOpenBracketPosition + 1, iClosedBracketPosition - iOpenBracketPosition - 1)) 

End Function 

희망 :; 나를 위해 적어도

마르쿠스

OK), 그래서 여기에 내가 이름 바꾸기가 옵션이 아닌 경우와 함께 제공되는 코드입니다! 건배! Markus

+0

(99)까지만 작동합니다. 한 번 (10) 다음에 (11) 등으로 정렬됩니다. 여러 번 접두사를 붙일 필요가 없습니다. 0s 내 파일 이름 ... – Malteaser6900

+0

알았습니다. 일부 코드가 완료되었습니다. 그러면 문제가 해결됩니다. 지금 당장 시험 할 수는 없어요. 왜냐하면 나는 집에서 전망이 없기 때문입니다. 내일 다시 너에게 간다! –

+0

그럼. 코드가 있으며 작동해야합니다! 희망이 도움이됩니다. –