Replace Scripting. with System.IO. in following function

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
LVL 4
shawnlehnerAsked:
Who is Participating?
 
arif_eqbalConnect With a Mentor Commented:
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)
        MsgBox(Ct)
    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)
        Next
    End Function
0
 
shawnlehnerAuthor Commented:
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

        BuildFolderStructure((DestinationFolder))


        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
0
 
arif_eqbalCommented:
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
    objFile.CopyTo(YourPath)
Next

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
Next
Here SubDir is again an object of Type DirectoryInfo.

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



0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.