Click here to Skip to main content
15,885,910 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
Hello Friends,

i have lot of pdf files in folder. i need to print the pdf file names in text file. i am using VB6 code after print the file names i am not get in proper order.

Files in Folder

CR 6.1.09.pdf
CR 9.1.09.pdf
CR 13.08.08.pdf
CR 14.1.09.pdf
CR 16.01.09.pdf
CR to GB.pdf
Email 21.09.07.1.pdf
HW notes 4.02.09.pdf
Ledger.pdf
MC 30.10.08.pdf
SH letter10.3.09.pdf

After printing the files name as below

CR 13.08.08.pdf
CR 14.1.09.pdf
CR 16.01.09.pdf
CR 6.1.09.pdf
CR 9.1.09.pdf
CR to GB.pdf
Email 21.09.07.1.pdf
HW notes 4.02.09.pdf
Ledger.pdf
MC 30.10.08.pdf
SH letter10.3.09.pdf

i need to print the file name in folder order. Hope clear

any one send the correct code.

Thanks
Posted
Comments
Mahesh Bailwal 7-Oct-13 1:30am    
Please share your code
Pandiarajan A 7-Oct-13 1:39am    
Hello Find below my code

Private Sub Command1_Click()
Close #1
Open App.Path & "\PDF.txt" For Output As #1
Mypath = InputBox("LFP", "EnterPathName")
Dir1.Path = Mypath
File1.Path = Dir1.Path
If File1.ListCount = 0 Then MsgBox "No Files", vbInformation
For i = 0 To File1.ListCount - 1
Print #1, Dir1.Path & Chr(9) & File1.List(Y)
End If
End Sub
Mahesh Bailwal 7-Oct-13 2:59am    
In your folder you need to check by which field your files are ordered/sorted whether its by file name,file type, date modified, etc.

Below link give you information about changing order of file in a folder .
http://www.eightforums.com/tutorials/9743-sort-group-arrange-items-windows-8-file-explorer.html

Once you know by which order your files are showing up in folder. You can sort your File List in your code with same order and print it.

1 solution

This function will sort alphanumerically:
VB
Private Sub SortAlphaNum(List() As String)
    Dim TagedList() As String       ' List with "tags" in the numbers.
    ReDim taggedlist(UBound(List))

    Dim item As String              ' One item with tags.
    Dim ch As String                ' One character.
    Dim num As String               ' A number.  Can be multidigit.
    Dim digitCount As Integer       ' The number of digits.
    Dim i, j As Integer

    ' Visit each item in the list.
    For i = 0 To UBound(List) - 1
        ' Init.
        item = ""
        inNum = False
        num = ""
        digitCount = 0
        j = 1

        ' Examine each character.
        While j <= Len(List(i))
            ' Get one character.
            ch = Mid(List(i), j, 1)

            ' If the character is not a digit put it in the item.
            If ch < "0" Or ch > "9" Then
                item = item & ch

            ' Else, this is a digit.
            Else
                ' Keep reading digits.
                While ch >= "0" And ch <= "9"
                    ' Increase the count and add digit to num.
                    digitCount = digitCount + 1
                    num = num & ch
                    j = j + 1

                    ' Get another character.
                    ch = Mid(List(i), j, 1)
                Wend

                ' Add the tag and digit to the item.
                item = item & digitCount
                item = item & num

                ' If this is not the end of the line, add the last character read to the item.
                If j <= Len(List(i)) Then
                    item = item & ch
                End If
            End If
            j = j + 1
        Wend
        ' update the tagged list with the item.
        taggedlist(i) = item
    Next i

    ' Sort the tagged list (along with the real list).
    ' This is just a bubble sort.  Probably should replace.
    For i = 0 To UBound(taggedlist) - 2
        For j = 0 To UBound(taggedlist) - 2
            If taggedlist(j) > taggedlist(j + 1) Then
                item = taggedlist(j)
                taggedlist(j) = taggedlist(j + 1)
                taggedlist(j + 1) = item
                item = List(j)
                List(j) = List(j + 1)
                List(j + 1) = item
            End If
        Next j
    Next i
End Sub



To call it use something like this:
VB
Private Sub Command1_Click()
    Dim i As Integer
    Dim mylist() As String
    ReDim mylist(List1.ListCount)

    ' Get list from listbox1.
    For i = 0 To List1.ListCount - 1
        mylist(i) = List1.List(i)
    Next i

    ' Sort the list.
    Call SortAlphaNum(mylist)

    ' Display in listbox2.
    List2.Clear
    For i = 0 To List1.ListCount
        Call List2.AddItem(mylist(i))
    Next i
End Sub


This sorts by putting a "tag" in front of digit groups. For example: the two files
CR 9.1.09.pdf
CR 13.08.08.pdf
will be tagged as
CR 19.11.209.pdf
CR 213.208.208.pdf
This can now be sorted normally and then redisplayed using the original list.
I have made the assumption (probably bad) that all arrays start at 0.
 
Share this answer
 
v2
Comments
Pandiarajan A 8-Oct-13 21:16pm    
Thanks Friend. your code fulfilled my request. thanks for your timing help
Hex01 8-Oct-13 21:59pm    
Glad it worked!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900