How to get a list of all files of a directory (subdirectories included) (Daniel Walzenbach)?

Published 23 April 09 11:53 AM

I needed to create a list of all Code Snippets we ship in Visual Studio the other day containing their title, description and path on disc and size. As you might know, Code Snippets are stored in multiple directories below "%ProgramFiles%\Microsoft Visual Studio 10.0" so I had to traverse all the subdirectories of the aforementioned path to find all snippets.

Fortunately – linq to the rescue – this came down to a few lines of code :-)

Dim query = From file In My.Computer.FileSystem.GetFiles("C:\Program Files\Microsoft Visual Studio 10.0", FileIO.SearchOption.SearchAllSubDirectories) _

            Where file.EndsWith(".snippet") _

            Order By file

This statement gives you a List of Strings (or, to be a bit more precise, a System.Linq.IOrderedEnumerable(Of String)) which you can walk over to do all kinds of crazy things.

Here is what I did to solve the problem I described above:

Imports <xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> 

 

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim query = From file In My.Computer.FileSystem.GetFiles(txtPath.Text, FileIO.SearchOption.SearchAllSubDirectories) _

                    Where file.EndsWith(".snippet") _

                    Order By file

        Dim snippets As New List(Of Snippet)

        Dim snippetDocument As XElement

        Dim snippet As Snippet

        For Each item In query

            snippetDocument = XElement.Load(item)

            If snippetDocument...<Title>.Value IsNot Nothing Then

                snippet = New Snippet With {.Title = snippetDocument...<Title>.Value.ToString _

                                            , .Description = snippetDocument...<Description>.Value.ToString _

                                            , .Path = item _

                                            , .Size = New System.IO.FileInfo(item).Length}

                snippets.Add(snippet)

            End If

        Next

        DataGridViewSnippets.AutoGenerateColumns = True

        DataGridViewSnippets.DataSource = snippets

    End Sub

End Class

 

Public Class Snippet

    Private _Title As String

    Private _Description As String

    Private _Path As String

    Private _Size As Long

    Public Property Title() As String

        Get

            Return _Title

        End Get

        Set(ByVal value As String)

            _Title = value

        End Set

    End Property

    Public Property Description() As String

        Get

            Return _Description

        End Get

        Set(ByVal value As String)

            _Description = value

        End Set

    End Property

    Public Property Path() As String

        Get

            Return _Path

        End Get

        Set(ByVal value As String)

            _Path = value

        End Set

    End Property

    Public Property Size() As Long

        Get

            Return _Size

        End Get

        Set(ByVal value As Long)

            _Size = value

        End Set

    End Property

End Class

Btw, if you bind the result to a DataGridView you can copy&paste from there into excel. Needo :-)

Cheers!

   Daniel

Edited April 29, 2009: Included the "Imports" statement and did some cleanup work.

by VBTeam

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# How to get a list of all files of a directory (subdirectories included) (Daniel Walzenbach)? | Microsoft Share Point said on April 23, 2009 3:53 PM:

PingBack from http://microsoft-sharepoint.simplynetdev.com/how-to-get-a-list-of-all-files-of-a-directory-subdirectories-included-daniel-walzenbach/

# zzz said on April 24, 2009 1:44 AM:

that's not very robust or performant. ms should obsolete GetFiles(..., FileIO.SearchOption.SearchAllSubDirectories)

and give us a proper class that takes advantage of knowledge of the filesystem, the storage hardware and multiple cores and gives simple options to handle links and loops, networks, system directiories. so plinq support and lazy eval. it should also provide access to filesystem inside disk images.

# Jem said on April 24, 2009 3:19 AM:

Getting the *.snippet filter into the GetFiles call should improve greatly the performance (or at least I hope).

Of course then, it would make this exemple even more a poor use of Linq. Because, what's the big advantage of Linquing this code against a simple:

Dim files = My.Computer.FileSystem.GetFiles(txtPath.Text, FileIO.SearchOption.SearchAllSubDirectories, "*.snippet")

?

"It gets sorted", you might answer, yeah but it pretty much is already sorted out of the box, and you're not using the fact that filenames are sorted anyway.

# Jonathan Baker said on April 24, 2009 11:03 PM:

Why couldn't you include the dir c:\*.* /s and > to a file?

# Kevin Gallagher (Oregon Department of Revenue) said on April 28, 2009 9:56 AM:

I had to add

Imports <xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

to read the snippets, why is that?

# The Visual Basic Team said on April 29, 2009 8:33 PM:

Hi, I posted a bit of code the other day which I used to get a list of all Code Snippets we ship in Visual

# VBTeam said on April 29, 2009 8:36 PM:

Kevin,

sorry for the confusion! I missed to include this line in the code example. I just posted the answer to your question here:

http://blogs.msdn.com/vbteam/archive/2009/04/29/when-using-linq-to-xml-why-don-t-i-get-results-if-i-don-t-import-a-xml-namespace-daniel-walzenbach.aspx

Let me know if this helps!

Thanks!

  Daniel

# Bhavana said on May 5, 2009 7:08 AM:

I have multiple text logs (100) in one folder.

I am looking for code to copy individual txt log contents to copy & paste in a  different sheets of a workbook.

Can canyone help me ?

Bhavana

# Kevin Gallagher (Oregon Department of Revenue) said on May 11, 2009 10:50 AM:

Thanks for the explaination on why the import statement was needed. Also thanks for the code in general!!!

# Oleksandr said on June 19, 2009 1:16 PM:

There is an example of how to get registered code snippets at http://msdn.microsoft.com/en-us/library/bb165947(VS.80).aspx. Isn't it working in VS10?

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

This Blog

Syndication

Page view tracker