Results 1 to 18 of 18

Thread: [RESOLVED] XML file search

  1. #1

    Thread Starter
    Addicted Member sotusotusotu's Avatar
    Join Date
    Aug 2001
    Location
    Plymouth, England
    Posts
    158

    Resolved [RESOLVED] XML file search

    Hey,

    I have a list box named lstMyPlaylists and when the cmdOk button is clicked i need to be able for it to search xml files inside a folder called playlists and display the xml filenames inside the lstbox?

    I can't seem to get anywhere with this, have tried searching the net for stuff but have been unsucessfull!

    any ideas?

    Thanks
    Living a Lie, Timmy

  2. #2
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: XML file search

    Try this...
    VB Code:
    1. Dim xmlfiles As String() = System.IO.Directory.GetFiles("C:\Playlists", "*.xml")
    2.  
    3. lstMyPlaylists.Items.AddRange(xmlfiles)
    Hope it helps you.
    Last edited by Andy_P; Mar 15th, 2006 at 03:01 PM.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  3. #3

    Thread Starter
    Addicted Member sotusotusotu's Avatar
    Join Date
    Aug 2001
    Location
    Plymouth, England
    Posts
    158

    Re: XML file search

    Quote Originally Posted by Andy_P
    Try this...
    VB Code:
    1. Dim xmlfiles As String() = System.IO.Directory.GetFiles("C:\Playlists", "*.xml")
    2.  
    3. lstMyPlaylists.Items.AddRange(xmlfiles)
    Hope it helps you.
    Sweet man! Works really well!

    I would however like it to only display the file name?

    Also do you know how i can make it find its own path to the directory as its in my documents and when i put it on another computer it will not be saved in the same directory. If that makes sense, having trouble with explaining?!?

    cheers
    Living a Lie, Timmy

  4. #4
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: XML file search

    VB Code:
    1. Dim xmlpath As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\Playlists"
    2.  
    3. Dim xmlfiles As String() = System.IO.Directory.GetFiles(xmlpath, "*.xml")
    4.  
    5. For Each xmlfile As String In xmlfiles
    6.     lstMyPlaylists.Items.Add(Path.GetFileName(xmlfile))
    7. Next xmlfile
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  5. #5

    Thread Starter
    Addicted Member sotusotusotu's Avatar
    Join Date
    Aug 2001
    Location
    Plymouth, England
    Posts
    158

    Re: XML file search

    Quote Originally Posted by Andy_P
    VB Code:
    1. Dim xmlpath As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\Playlists"
    2.  
    3. Dim xmlfiles As String() = System.IO.Directory.GetFiles(xmlpath, "*.xml")
    4.  
    5. For Each xmlfile As String In xmlfiles
    6.     lstMyPlaylists.Items.Add(Path.GetFileName(xmlfile))
    7. Next xmlfile
    I get error "path" is not declared?!?
    Living a Lie, Timmy

  6. #6
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: XML file search

    Apologies, you need to put 'System.IO.Path', in place of 'Path'

    VB Code:
    1. Dim xmlpath As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\Playlists"
    2.  
    3. Dim xmlfiles As String() = System.IO.Directory.GetFiles(xmlpath, "*.xml")
    4.  
    5. For Each xmlfile As String In xmlfiles
    6.     lstMyPlaylists.Items.Add([B][COLOR=Red]System.IO.Path[/COLOR][/B].GetFileName(xmlfile))
    7. Next xmlfile
    Alternatievely, type the line 'Imports System.IO' at the top of the form you are running this code in.


    Hope that helps!



    Edit: Seems you have asked the question again, sorry for taking a while to get back to you.
    Last edited by Andy_P; Mar 16th, 2006 at 02:02 PM.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  7. #7

    Thread Starter
    Addicted Member sotusotusotu's Avatar
    Join Date
    Aug 2001
    Location
    Plymouth, England
    Posts
    158

    Re: XML file search

    Code sort of works well, however i have to still type in "\Uni Work\Year 3\PRMM301\program\vb\eJukebox_New\Backup\Backup\bin\playlists" as it will only go as far as "C:\Documents and Settings\Andy\My Documents\"

    Here is my code:

    VB Code:
    1. Dim xmlpath As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\Uni Work\Year 3\PRMM301\program\vb\eJukebox_New\Backup\Backup\bin\playlists"
    2.  
    3.             Dim xmlfiles As String() = System.IO.Directory.GetFiles(xmlpath, "*.xml")
    4.  
    5.             For Each xmlfile As String In xmlfiles
    6.                 lstMyPlaylists.Items.Add(System.IO.Path.GetFileName(xmlfile))
    7.             Next xmlfile
    Living a Lie, Timmy

  8. #8
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: XML file search

    This will look in all directories under MyDocuments and find all XML files...

    VB Code:
    1. Dim userpath As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
    2.  
    3. Dim dirs As String() = System.IO.Directory.GetDirectories(userpath)
    4.  
    5. For Each dir As String In dirs
    6.  
    7.     Dim xmlfiles As String() = System.IO.Directory.GetFiles(dir, "*.xml")
    8.  
    9.     For Each xmlfile As String In xmlfiles
    10.         ListBox1.Items.Add(System.IO.Path.GetFileName(xmlfile))
    11.     Next xmlfile
    12.  
    13. Next dir

    Is that what you are looking to do?
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  9. #9

    Thread Starter
    Addicted Member sotusotusotu's Avatar
    Join Date
    Aug 2001
    Location
    Plymouth, England
    Posts
    158

    Re: XML file search

    Thanks mate.

    Well it finds another .xml file in this folder now: C:\Documents and Settings\Andy\My Documents\Adlm

    I was just wondering why it only goes as far as my documents rather than just finding looking inside the directory its in for the folder "playlists"?

    By the way i used to live in leighton buzzard, small world!
    Last edited by sotusotusotu; Mar 16th, 2006 at 04:38 PM.
    Living a Lie, Timmy

  10. #10
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: XML file search

    OK, try this code out.

    This should look for any directory called "Playlists" in the MyDocuments tree and pick out the XML files contained when you click the button.

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.  
    3.     lstMyPlaylists.Items.Clear()
    4.  
    5.     Dim userpath As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
    6.  
    7.     Dim startPath As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(userpath)
    8.  
    9.     SearchDirectory(startPath)
    10.  
    11. End Sub
    12.  
    13. Private Sub SearchDirectory(ByVal dir As System.IO.DirectoryInfo)
    14.  
    15.     If dir.FullName.EndsWith("\Playlists") Then
    16.  
    17.         For Each xmlfile As System.IO.FileInfo In dir.GetFiles("*.xml")
    18.             lstMyPlaylists.Items.Add(xmlfile)
    19.         Next xmlfile
    20.  
    21.     End If
    22.  
    23.     'Recursively search all subdirectories
    24.     For Each subdir As System.IO.DirectoryInfo In dir.GetDirectories
    25.         SearchDirectory(subdir)
    26.     Next subdir
    27.  
    28. End Sub



    PS It certainly is a small world, etc, etc!
    Last edited by Andy_P; Mar 17th, 2006 at 02:06 PM.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  11. #11

    Thread Starter
    Addicted Member sotusotusotu's Avatar
    Join Date
    Aug 2001
    Location
    Plymouth, England
    Posts
    158

    Re: XML file search

    Tried that code but nothing appears in lstMyPlaylists listbox for some reason?
    Living a Lie, Timmy

  12. #12
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: XML file search

    I just edited the post - I had the code looking for "\Temp" instead of "\Playlists".

    I also edited the name of the listbox to lstMyPlaylists

    Try it again with those corrections!
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  13. #13

    Thread Starter
    Addicted Member sotusotusotu's Avatar
    Join Date
    Aug 2001
    Location
    Plymouth, England
    Posts
    158

    Re: XML file search

    Being more specific, its writes the xml properly and saves in the dir, however the results are not display in the listbox. Also no errors compiling or running are displayed.

    cheers
    Living a Lie, Timmy

  14. #14

    Thread Starter
    Addicted Member sotusotusotu's Avatar
    Join Date
    Aug 2001
    Location
    Plymouth, England
    Posts
    158

    Re: XML file search

    Tried \Playlists and still nothing!
    Living a Lie, Timmy

  15. #15

    Thread Starter
    Addicted Member sotusotusotu's Avatar
    Join Date
    Aug 2001
    Location
    Plymouth, England
    Posts
    158

    Re: XML file search

    works! sorry mate my mistake i put in \Playlists instead of \playlists!

    Stupid me!

    Thanks alot.

    Is it possible to get it to display just the filemame and not the extension?
    Living a Lie, Timmy

  16. #16
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: XML file search

    Change the list box items add line to this:
    VB Code:
    1. lstMyPlaylists.Items.Add(System.IO.Path.GetFileNameWithoutExtension(xmlfile.ToString))
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  17. #17

    Thread Starter
    Addicted Member sotusotusotu's Avatar
    Join Date
    Aug 2001
    Location
    Plymouth, England
    Posts
    158

    Re: XML file search

    Perfect!

    your a good man!

    Regards, Andy
    Living a Lie, Timmy

  18. #18
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: [RESOLVED] XML file search

    No probs.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


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