shawnlehner
asked on
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(directoryToC ount)
For Each nFolder In oFolder.SubFolders
count = count + 1 + CopyfolderCount(directoryT oCount & nFolder.Name & "\")
Next nFolder
For Each oFile In oFolder.Files
count = count + 1
Next oFile
CopyfolderCount = count
End Function
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(directoryToC
For Each nFolder In oFolder.SubFolders
count = count + 1 + CopyfolderCount(directoryT
Next nFolder
For Each oFile In oFolder.Files
count = count + 1
Next oFile
CopyfolderCount = count
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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
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.
ASKER
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((Dest
Dim FilesNumber As Integer
Dim CurrentFile As Integer
If ofs.FolderExists(SourceFol
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