Replace Scripting. with System.IO. in following function

Posted on 2005-05-05
Last Modified: 2010-05-18
I need a function that does the same thing as the one below except with the System.IO. controls instead

    Function CopyfolderCount(ByRef directoryToCount As String) As Integer
        Dim ofs As New Scripting.FileSystemObject
        Dim oFolder As Scripting.Folder
        Dim oFile As Scripting.File
        Dim nFolder As Scripting.Folder
        Dim count As Integer

        oFolder = ofs.GetFolder(directoryToCount)
        For Each nFolder In oFolder.SubFolders
            count = count + 1 + CopyfolderCount(directoryToCount & nFolder.Name & "\")
        Next nFolder
        For Each oFile In oFolder.Files
            count = count + 1
        Next oFile
        CopyfolderCount = count
    End Function
Question by:shawnlehner
    LVL 19

    Accepted Solution

    Well I gather that you are attempting to get a count of All Folders/Sub-Folders/Sub-Sub-Folders .........

    Here's a code using System.IO
    While trying to convert your code I found that DirectoryInfo would make things still simpler, so I have used that. Its not eaxact translation of your code but I think it should solve the purpose....

       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Dir As DirectoryInfo = New DirectoryInfo("d:\")
            Dim Ct As Integer
            Ct = GetDirCount(Dir)
        End Sub

        Private Function GetDirCount(ByVal Dir As DirectoryInfo) As Integer
            For Each dirinfo As DirectoryInfo In Dir.GetDirectories("*")
                Return Dir.GetDirectories.Length + GetDirCount(dirinfo)
        End Function
    LVL 4

    Author Comment

    awsome... but i was looking for an example of the otherway so I could use it to convert the following function as well

    Public Function CopyAllFiles(ByVal SourceFolder As String, ByVal DestinationFolder As Object) As Boolean

            Dim ofs As New Scripting.FileSystemObject
            Dim oFolder As Scripting.Folder
            Dim nFolder As Scripting.Folder
            Dim oFile As Scripting.File


            Dim FilesNumber As Integer
            Dim CurrentFile As Integer
            If ofs.FolderExists(SourceFolder) Then
                oFolder = ofs.GetFolder(SourceFolder)
                For Each oFile In oFolder.Files
                    FilesNumber = FilesNumber + 1
                Next oFile
                For Each oFile In oFolder.Files
                    FileCopy(oFile.Path, DestinationFolder & "\" & oFile.Name)
                Next oFile
                For Each nFolder In oFolder.SubFolders
                    CopyAllFiles(SourceFolder & nFolder.Name & "\", DestinationFolder + nFolder.Name + "\")
                Next nFolder
            End If
        End Function
    LVL 19

    Expert Comment

    Hi shawnlehner
    I'll help you here to get this code converted, rather than giving you the converted code.
    I'll tell you the equivalents of the commands you have used and you try to change the code.

    Changes in declarations:
    The Scripting.FileSystemObject is not at all required as we will be using System.IO
    The Scripting.Folder will now become DirectoryInfo (call its object Dir)
    And Scripting.File will now be FileInfo (Call its Object objFile )

    To check whether a Directory exists try Directory.Exists("Path")
    Directory is a class and Exists is its Static method so no need to create any object for this.

    Next you are trying to get the No of files in the folder, you need not run a loop for that just use Dir.GetFiles.Length where Dir is a DirectoryInfo object you'd create in the declaration part.

    next you are copying all files in the folder to somewhere else
    use this loop

    For each objFile in Dir.GetFiles

    Here objFile is an object of FileInfo type and Dir is Directory Info object created in declaration part.

    Next is the Recursive call for SubFolders of the Folder
    Use this loop

    For each SubDir in Dir.GetDirectories
       'Call the function
    Here SubDir is again an object of Type DirectoryInfo.

    Hope you can work this out yourself, in case of any problem post back here.


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    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

    18 Experts available now in Live!

    Get 1:1 Help Now