Results 1 to 24 of 24

Thread: Visual Basic 2005 - Sorting Files by Names

  1. #1

    Thread Starter
    New Member j0sh77's Avatar
    Join Date
    Jan 2012
    Posts
    15

    Visual Basic 2005 - Sorting Files by Names

    Hello, I'm new to these forums and I'm looking for some help.

    For my programming class we must make a word guessing game with scoring for a project. We aren't required to log top scores or anything like that, but I would like to learn how to for future references. I've looked around and all I've found was ways to log in Excel and SQL servers, which I don't have the ability to do at school.



    I had the idea to create a text file with the user's score as the title each time a game is won. Then I'd like to write to the file (which I know how to do) with the word that was used in the game and the user's name.

    What I don't know how to do is find the file name with the highest integer value. I've looked around and couldn't find any way how to do this.



    Another way I thought of doing this was to just use one text file, but insert a line of text per each game played. Then I'd have to find where to insert each line to make sure they're in order, so I could just display the text file in a textbox, which would be easy enough.

    Example:
    #1 310 - Josh with the word "Cat"
    #2 265 - Josh with the word "Dog"


    I know a lot of people here don't like to hand out code to people, and have them learn for themselves. I support this and would like to learn for myself, but I don't know what commands to use to get a list of file names or insert lines and certain places in a text file.

    Thanks in advance.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Visual Basic 5 - Sorting Files by Names

    Tip: Display the high scores in a listbox with the Sorted property set to True. Now, listboxes don't sort numbers as numbers, they sort them as text. If your format your scores with leading zeroes, then it resolves the sorting issue. Example:
    Code:
     ' in a loop that reads your scores
    ... loop start
       ... read a score & format like so: sText is a line read from the file
       List1.AddItem Format$(sText, "000000") ' add more zeroes if scores can be over 1 million
    ... loop end
    In my signature below is a link to the VB FAQs section. There you can find examples of reading/writing text files
    Good luck with your homework

    Edited. You are taking a programming class and Visual Basic version 5 is being used? Wow!!!
    Last edited by LaVolpe; Jan 8th, 2012 at 08:21 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

  4. #4

    Thread Starter
    New Member j0sh77's Avatar
    Join Date
    Jan 2012
    Posts
    15

    Re: Visual Basic 5 - Sorting Files by Names

    Quote Originally Posted by LaVolpe View Post
    Tip: Display the high scores in a listbox with the Sorted property set to True. Now, listboxes don't sort numbers as numbers, they sort them as text. If your format your scores with leading zeroes, then it resolves the sorting issue. Example:
    Code:
     ' in a loop that reads your scores
    ... loop start
       ... read a score & format like so: sText is a line read from the file
       List1.AddItem Format$(sText, "000000") ' add more zeroes if scores can be over 1 million
    ... loop end
    In my signature below is a link to the VB FAQs section. There you can find examples of reading/writing text files
    Good luck with your homework

    Edited. You are taking a programming class and Visual Basic version 5 is being used? Wow!!!
    Sorry, I actually just started VB this year. I know how to write to a text file, but how would I sort it by the value of the score?


    Quote Originally Posted by RhinoBull View Post
    Are you sure you are using VB5 (aka 1997) and not VB 2005?
    Sorry, yeah, VB 2005!

  5. #5

  6. #6
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: VB 2005 - Sorting Files by Name

    The easiest way without linq (i think) would be:

    Just append your lines to your text file unsorted - without the #No in the front

    Then when you read your textfile put it in a SortedDictionary(of integer, string) object with the key being the score and the data being the rest...

    Then loop through the sorted dictionary to display it.

    PS i say without linq since you are just starting and your teacher may think it wasn't your's if u used it

    Kris

  7. #7

    Thread Starter
    New Member j0sh77's Avatar
    Join Date
    Jan 2012
    Posts
    15

    Re: VB 2005 - Sorting Files by Name

    Quote Originally Posted by i00 View Post
    The easiest way without linq (i think) would be:

    Just append your lines to your text file unsorted - without the #No in the front

    Then when you read your textfile put it in a SortedDictionary(of integer, string) object with the key being the score and the data being the rest...

    Then loop through the sorted dictionary to display it.

    PS i say without linq since you are just starting and your teacher may think it wasn't your's if u used it

    Kris

    How do I use a sorted dictionary? Sorry for asking these questions... :P

    It sounds pretty simplistic enough for me to use.

  8. #8
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,899

    Re: Visual Basic 5 - Sorting Files by Names

    Code:
    Dim i as Integer
    
    frmMain.File1.Sorted = True
    
    For i = 1 to frmMain.File1..........
       Blah
       Blah
    Next i
    Then loop through all of the FileListBox entries then add then to each a 3D Array, or something else to that matter...
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  9. #9

  10. #10
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Visual Basic 5 - Sorting Files by Names

    Moved to VB.Net Forum.

    Thanks for the reports guys!

    Gary

  11. #11
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Visual Basic 2005 - Sorting Files by Names


  12. #12
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Visual Basic 2005 - Sorting Files by Names

    Duplicate threads have been merged.

    j0sh77, please remember to only have one thread per question.

    Gary

  13. #13
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,899

    Re: Visual Basic 2005 - Sorting Files by Names

    -- Totally confused. Was this thread, a total waste of time in the VB6 COM Section??? But .NET works the same, in broad spectrum details. But it's the fine details, that make up COM and .NET different...

    PS: Also note that I cannot keep helping the OP, cause I don't know anything about .NET, only Visual Basic COM and also Visual Basic DOS. (Please don't ask me anymore about Visual Basic v1.00 & v2.00 for DOS. Cause I have stopped doing that, many years ago!!)
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  14. #14

    Thread Starter
    New Member j0sh77's Avatar
    Join Date
    Jan 2012
    Posts
    15

    Re: Visual Basic 2005 - Sorting Files by Names

    Quote Originally Posted by gep13 View Post
    Duplicate threads have been merged.

    j0sh77, please remember to only have one thread per question.

    Gary

    Sorry for the double post, I hadn't realized you were going to move it!


    Quote Originally Posted by ThEiMp View Post
    Code:
    Dim i as Integer
    
    frmMain.File1.Sorted = True
    
    For i = 1 to frmMain.File1..........
       Blah
       Blah
    Next i
    Then loop through all of the FileListBox entries then add then to each a 3D Array, or something else to that matter...
    Looking at the responses I'm getting, I'm assuming I won't be able to do this so easily at my level of programming. :P

    Is there a way to get a list of all the file names in a folder? I could name a txt file with the score the player received and then inside the file the name and the word the guessed... Then I could take the value of the name of the txt and sort it that way? I don't know if that would work, but its just an idea!

  15. #15
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,899

    Re: Visual Basic 2005 - Sorting Files by Names

    FileListBox, it comes as a Unbound Control inside VB6 COM. But I guess that you can also use that control inside VB .NET, as well...
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  16. #16
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Visual Basic 2005 - Sorting Files by Names

    Quote Originally Posted by ThEiMp View Post
    -- Totally confused. Was this thread, a total waste of time in the VB6 COM Section???
    Although originally posted in the Visual Basic Forum, the OP is actually using VB.Net, so I merged the threads, and left the question in VB.Net.

    Gary

  17. #17

    Thread Starter
    New Member j0sh77's Avatar
    Join Date
    Jan 2012
    Posts
    15

    Re: Visual Basic 2005 - Sorting Files by Names

    Or would there be a way to insert the scores played in one session into an array, and then write the array to a txt file? Then on form load it would recall the data from the txt and store it to an array again? Then I could sort them by their scores.

  18. #18
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,899

    Re: Visual Basic 2005 - Sorting Files by Names

    -- That could be one of the ways of doing, it. But then I could mention that there could be alot of processor usage and memory consumption, as well. Seeing that you will need to update the txt file, to about something like an .Interval of something like a second, or so at a time or even everytime the ListBox is then Changed, then that could also be one of the very good ideas, to work with...

    PS: Visual Basic, any version is very memory hungry, and also it's kernal for the Run-Time Support, is very slow and it uses alot of processor usage from Windows 32/64Bit, as well. But then I can vouch that the 64-Bit Windows, is able to work out the kinks in the Support, from the apps, that is written in VB.NET, but not VB6 COM...
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  19. #19

    Thread Starter
    New Member j0sh77's Avatar
    Join Date
    Jan 2012
    Posts
    15

    Re: Visual Basic 2005 - Sorting Files by Names

    Quote Originally Posted by ThEiMp View Post
    -- That could be one of the ways of doing, it. But then I could mention that there could be alot of processor usage and memory consumption, as well. Seeing that you will need to update the txt file, to about something like an .Interval of something like a second, or so at a time or even everytime the ListBox is then Changed, then that could also be one of the very good ideas, to work with...
    Memory usage is fine, but Im also lookin to learn how to do this for other programs!

  20. #20
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,899

    Re: Visual Basic 2005 - Sorting Files by Names

    Question: How many do you have, right now???
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  21. #21
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Visual Basic 2005 - Sorting Files by Names

    Quote Originally Posted by j0sh77 View Post
    I had the idea to create a text file with the user's score as the title each time a game is won. Then I'd like to write to the file (which I know how to do) with the word that was used in the game and the user's name.

    What I don't know how to do is find the file name with the highest integer value. I've looked around and couldn't find any way how to do this.



    Another way I thought of doing this was to just use one text file, but insert a line of text per each game played. Then I'd have to find where to insert each line to make sure they're in order, so I could just display the text file in a textbox, which would be easy enough.

    Example:
    #1 310 - Josh with the word "Cat"
    #2 265 - Josh with the word "Dog"


    I know a lot of people here don't like to hand out code to people, and have them learn for themselves. I support this and would like to learn for myself, but I don't know what commands to use to get a list of file names or insert lines and certain places in a text file.

    Thanks in advance.
    You could store the data as follows

    Document.txt save in the Bin\Debug folder of your app.
    Code:
    1,310,Josh with the word,Cat
    2,500,Josh with the word,Dog
    3,265,Josh with the word,Mouse
    Then use the TextFieldParser class to read in the file (see code below). For working with files in general look at the IO.File namespace. The code below uses the TextFileParser with the file structure above where the file is stored in the app folder as Document.txt. A function is used to get the max value for the second column of data, in this case 500.

    So this gives you a good starting point, some answers some direction. If something does not work as expected more likely than not I used something from VS2008 as I do not have VS2005 on my home machine, only VS2008.

    Code:
    Private Sub ReadFileDemo()
    
        Dim Values As Integer() = {}
    
        If Not IO.File.Exists("Document.txt") Then
            MessageBox.Show("Document.txt was not located")
            Exit Sub
        End If
        Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("Document.txt")
            MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
            MyReader.Delimiters = New String() {","}
            Dim currentRow As String()
    
            While Not MyReader.EndOfData
                Try
                    currentRow = MyReader.ReadFields()
    
                    Console.WriteLine("[{0}] [{1}] [{2}] [{3}]", _
                        currentRow(0), _
                        currentRow(1), _
                        currentRow(2), _
                        currentRow(3))
    
                    Array.Resize(Values, Values.Length + 1)
                    Values(Values.Length - 1) = CInt(currentRow(1))
                Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                    Console.WriteLine(ex.Message)
                End Try
            End While
        End Using
    
        For Each item In Values
            Console.WriteLine(item)
        Next
    
        Console.WriteLine("Max value {0}", MaxValue(Values))
    
    End Sub
    Private Function MaxValue(ByVal sender() As Integer) As Integer
        Dim Result As Integer = sender(0)
        For i As Integer = 1 To sender.Length - 1
            If sender(i) > Result Then
                Result = sender(i)
            End If
        Next
        Return Result
    End Function

  22. #22
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,899

    Re: Visual Basic 2005 - Sorting Files by Names

    -- I thought that .NET removed the Return command, cause that was connected to the GoSub, command???
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  23. #23
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Visual Basic 2005 - Sorting Files by Names

    Quote Originally Posted by ThEiMp View Post
    -- I thought that .NET removed the Return command, cause that was connected to the GoSub, command???
    Second link in a Google search for VB.NET Return
    http://msdn.microsoft.com/en-us/libr...v=VS.100).aspx

  24. #24
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,899

    Re: Visual Basic 2005 - Sorting Files by Names

    !! YIKES !!
    !! CRIKEY !!
    !! Thanks for the Input$ !!
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width