Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Imports - Count files in a folder

Imports - Count files in a folder

Scheduled Pinned Locked Moved Visual Basic
csharphelp
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Central_IT
    wrote on last edited by
    #1

    I wonder if anyone could help me. I have the following function I took from VB6 and entered it into VB.Net 2005. When I try and compile it, I get lines under Scripting.FileSystemObject & Scripting.Folder. I know you have to use Imports but what one do I use. This function is in a module. If anyone has a different way to do a files in folder count let me know. Public Function FileCount(ByVal DirectoryName As String) As Long 'E.g. MsgBox FileCount("C:\windows")' Dim objFS As New Scripting.FileSystemObject Dim objFolder As Scripting.Folder If objFS.FolderExists(DirectoryName) Then Set objFolder = objFS.GetFolder(DirectoryName) FileCount = objFolder.Files.Count End If ' Set objFolder = Nothing Set objFS = Nothing End Function

    K O D M 4 Replies Last reply
    0
    • C Central_IT

      I wonder if anyone could help me. I have the following function I took from VB6 and entered it into VB.Net 2005. When I try and compile it, I get lines under Scripting.FileSystemObject & Scripting.Folder. I know you have to use Imports but what one do I use. This function is in a module. If anyone has a different way to do a files in folder count let me know. Public Function FileCount(ByVal DirectoryName As String) As Long 'E.g. MsgBox FileCount("C:\windows")' Dim objFS As New Scripting.FileSystemObject Dim objFolder As Scripting.Folder If objFS.FolderExists(DirectoryName) Then Set objFolder = objFS.GetFolder(DirectoryName) FileCount = objFolder.Files.Count End If ' Set objFolder = Nothing Set objFS = Nothing End Function

      O Offline
      O Offline
      originSH
      wrote on last edited by
      #2

      Check out the System.IO.Directory class, it'll do the counting stuff for you.

      1 Reply Last reply
      0
      • C Central_IT

        I wonder if anyone could help me. I have the following function I took from VB6 and entered it into VB.Net 2005. When I try and compile it, I get lines under Scripting.FileSystemObject & Scripting.Folder. I know you have to use Imports but what one do I use. This function is in a module. If anyone has a different way to do a files in folder count let me know. Public Function FileCount(ByVal DirectoryName As String) As Long 'E.g. MsgBox FileCount("C:\windows")' Dim objFS As New Scripting.FileSystemObject Dim objFolder As Scripting.Folder If objFS.FolderExists(DirectoryName) Then Set objFolder = objFS.GetFolder(DirectoryName) FileCount = objFolder.Files.Count End If ' Set objFolder = Nothing Set objFS = Nothing End Function

        K Offline
        K Offline
        Kerry Drake
        wrote on last edited by
        #3

        This MIGHT help:

        The FSO model is contained in the Scripting type library, which is located in the file Scrrun.dll. If you don't already have a reference to it, you can create one.

        To create a reference to the Scripting type library (Scrrun.dll)

        1. On the Project menu, click Add Reference, and then click the COM tab.
        2. Choose Microsoft Scripting Runtime from the Component Name list, and then click Select.

        You can now use the Object Browser to view the FSO model's objects, collections, properties, methods, events, and constants.

        My goal in life is to be the kind of person my dog thinks I am.

        1 Reply Last reply
        0
        • C Central_IT

          I wonder if anyone could help me. I have the following function I took from VB6 and entered it into VB.Net 2005. When I try and compile it, I get lines under Scripting.FileSystemObject & Scripting.Folder. I know you have to use Imports but what one do I use. This function is in a module. If anyone has a different way to do a files in folder count let me know. Public Function FileCount(ByVal DirectoryName As String) As Long 'E.g. MsgBox FileCount("C:\windows")' Dim objFS As New Scripting.FileSystemObject Dim objFolder As Scripting.Folder If objFS.FolderExists(DirectoryName) Then Set objFolder = objFS.GetFolder(DirectoryName) FileCount = objFolder.Files.Count End If ' Set objFolder = Nothing Set objFS = Nothing End Function

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          There is no import for the FileSystemObject. You would normally have to set a reference to the Windows Scripting Host. But, it's pointless to do this under the .NET Framework. Scrap this code and rewrite it using the classes in the System.Io namespace. The FileSystemObject is not needed in VB.NET. Also, the return type, Long in VB6, is a 32-bit signed interger. In VB.NET, this same Long datatype name is a 64-bit signed integer. Be careful when translating VB6 code to VB.NET.

          Imports System.Io
          .
          .
          .
          Public Shared Function GetFileCount(ByVal folderPath As String) As Integer
          If Not Directory.Exists(folderPath) Then
          Throw New DirectoryNotFoundException("The specified path was not found.")
          End If

          Dim filenames As String()
          filenames = Directory.GetFiles(folderPath)
          Return filenames.Length
          

          End Function

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          1 Reply Last reply
          0
          • C Central_IT

            I wonder if anyone could help me. I have the following function I took from VB6 and entered it into VB.Net 2005. When I try and compile it, I get lines under Scripting.FileSystemObject & Scripting.Folder. I know you have to use Imports but what one do I use. This function is in a module. If anyone has a different way to do a files in folder count let me know. Public Function FileCount(ByVal DirectoryName As String) As Long 'E.g. MsgBox FileCount("C:\windows")' Dim objFS As New Scripting.FileSystemObject Dim objFolder As Scripting.Folder If objFS.FolderExists(DirectoryName) Then Set objFolder = objFS.GetFolder(DirectoryName) FileCount = objFolder.Files.Count End If ' Set objFolder = Nothing Set objFS = Nothing End Function

            M Offline
            M Offline
            mr_lasseter
            wrote on last edited by
            #5

            Try using the System.IO functions rather than using the old VB6 functions. When you are coding in .Net you should always look for the new ways to do things rather than the old VB6 functions, you never know when they are going to cease to exists, so you might as well conform now. Imports System.IO Private Function GetFileCount(Byval path as String) as Integer Dim dirInfo As New IO.DirectoryInfo(path) Dim files() As FileInfo = dirInfo.GetFiles() return files.GetUpperBound(0) + 1 End Function

            Mike Lasseter

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups