Sunday, April 2, 2017

Recursive File System Object

''Creating Recursive Procedures
'
'Procedures have a limited amount of space for variables. Each time a procedure calls itself, more of that space is used. A procedure that calls itself is a recursive procedure. A recursive procedure that continuously calls itself eventually causes an error. For

Option Explicit
'Recursive process to copy all vlc files in a given folder
Sub CopyVLCFiles()

RecursiveVLCFiles "C:\Users\Vinay Kumar\Pictures"
RecursiveVLCFiles "C:\Users\Vinay Kumar\Videos"
MsgBox "done!"

End Sub

Sub RecursiveVLCFiles(startfldr As String)

Dim fso As New Scripting.FileSystemObject
Dim fil As Scripting.File
Dim tempfolder As Scripting.Folder
Dim subfldr As Scripting.Folder


Set tempfolder = fso.GetFolder(startfldr)

For Each fil In tempfolder.Files

    If Left(fso.GetExtensionName(fil.Path), 3) = "MP4" Then
        fil.Copy Environ("userprofile") & "\desktop\finalvidopico\MP4\" & fil.Name
    End If
   
Next fil

For Each subfldr In tempfolder.SubFolders

    Call RecursiveVLCFiles(subfldr.Path)

Next subfldr
End Sub


'Recursive process to list all file type/extension
Sub GetExtensionName()

Recursiveextname "C:\Users\Vinay Kumar\Pictures"
Recursiveextname "C:\Users\Vinay Kumar\Videos"
MsgBox "done!"


End Sub

Sub Recursiveextname(startfldr As String)

Dim fso As New Scripting.FileSystemObject
Dim fil As Scripting.File
Dim tempfolder As Scripting.Folder
Dim subfldr As Scripting.Folder
Dim extname As String

Sheet12.Activate

Set tempfolder = fso.GetFolder(startfldr)

    For Each fil In tempfolder.Files
        extname = fso.GetExtensionName(fil.Path)
        ActiveCell.value = extname
        ActiveCell.Offset(0, 1).value = fil.Path
        ActiveCell.Offset(1, 0).Select
    Next fil

    For Each subfldr In tempfolder.SubFolders
        Call Recursiveextname(subfldr.Path)
    Next subfldr


End Sub

No comments:

Post a Comment

*INTERVIEW QUESTIONS

* Ques 01. What is the difference between ByVal and ByRef and which is default ? Ans-  ByRef : If you pass an argument by reference when...