getting filenames and filesize of files through 2005

Posted on 2007-07-23
Last Modified: 2010-04-23

I have the following  2005 script for listing files in a directory.

 Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        files1 = System.IO.Directory.GetFiles(txtDirectory.Text)
        Dim sw As New System.IO.StreamWriter(txtFileName.Text)
        sw.Write(String.Join(ControlChars.CrLf, files1))
    End Sub

It works find , but how to modify it so that I can get the file size to appear next to the filename on the output like the following:
c:\sometext.txt 16
c:\othertext.txt 45

I think a "tab" between the filename and the filesize would be ok.



Question by:billyboy71
    LVL 96

    Expert Comment

    by:Bob Learned
    Dim fileSize As Long = New FileInfo(path).Length

    LVL 48

    Expert Comment

    Use System.IO.FileInfo and the Length propertie to get the file size
    LVL 8

    Expert Comment

    FileInfo finfo = new FileInfo("fileName");
                fileSize = finfo.Length ;
                fileName = finfo.Name
    LVL 69

    Expert Comment

    by:Éric Moreau
    LVL 85

    Accepted Solution

    Instead of using Directory.GetFiles(), which returns an Array of String...

    Create a DirectoryInfo() instance by passing it the folder name, and then call GetFiles on that instance.  This will return an Array of FileInfo instances representing each file:

        Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
            Dim di As New System.IO.DirectoryInfo(txtDirectory.Text)
            Dim sw As New System.IO.StreamWriter(txtFileName.Text)
            For Each fi As System.IO.FileInfo In di.GetFiles()
                sw.Write(fi.FullName & ", " & fi.Length)
        End Sub

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now