Friday, March 17, 2017

Copy files in respective folder type, if folder not exit it will be created.

'Macro is designed to copy files in receptive folder type. If folder is not created it will be created..


'-----------------------------------------------------------------------------------------------------

Sub Moving_Similar_FileType()

Range("A1").Select
Call fso_file_type(InputBox("Enter the folder Path: "))

MsgBox "done!!"

End Sub
'----------------------------------------------------------------------------------------------------------------
Sub fso_file_type(path As String)

Dim fso As New FileSystemObject         'Intializing file system object
Dim fl As File
Dim fld, subfolder As Folder


Set fld = fso.GetFolder(path)


For Each fl In fld.Files                ' Loop to copy all files in their respective folder

        If fso.FolderExists(Environ("userprofile") & "\desktop\dummy\" & fl.Type) Then
            fl.Copy Environ("Userprofile") & "\desktop\dummy\" & fl.Type & "\"
        Else
            fso.CreateFolder (Environ("userprofile") & "\desktop\dummy\" & fl.Type)
            fl.Copy Environ("Userprofile") & "\desktop\dummy\" & fl.Type & "\"
        End If

Next


For Each subfolder In fld.SubFolders        'Recursive to list all files folder within folder
    Call fso_file_type(subfolder.path)
Next


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...