Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

getting filenames and filesize of files through vb.net 2005

Hi,

I have the following vb.net  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.

Thanks.

Peter


0
billyboy71
Asked:
billyboy71
1 Solution
 
Bob LearnedCommented:
Dim fileSize As Long = New FileInfo(path).Length

Bob
0
 
jpaulinoCommented:
Use System.IO.FileInfo and the Length propertie to get the file size
0
 
sabeeshCommented:
FileInfo finfo = new FileInfo("fileName");
            fileSize = finfo.Length ;
            fileName = finfo.Name
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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)
        Next
        sw.Close()
    End Sub
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now