Replace Scripting. with System.IO. in following function

Posted on 2005-05-05
Medium Priority
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
  • 2
LVL 19

Accepted Solution

arif_eqbal earned 1000 total points
ID: 13943059
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

Author Comment

ID: 13947429
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

ID: 13957132
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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

840 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