Search code examples
vbafile-copyingfile-move

DropIt Project - Moving folders based on number of files in folder


DropIt Project Software:

I would like to know if it is possible to copy (or move) folders based on the amount of files they contain? E.g. I have: Folder 1 (2 Files), Folder 2 (5 Files), Folder 3 (1 File). Files 1 and 2 need to be moved to another location, because they contain more than one file. File 3 needs to remain where it is, because it only contains 1 file.

Is it possible to define a rule or regular expression to help with this?

If it's possible to do this task with VBA, that would also be fine.

Best regards, Hendre


Solution

  • The idea:

    Sub Test()
        Call MoveFolder("C:\Test1", "C:\Test2", 1)
    End Sub
    
    Sub MoveFolder(strSource$, strTarget$, iCount%)
        Dim fso As FileSystemObject
        Dim fld As Folder
        Set fso = New FileSystemObject
        Set fld = fso.GetFolder(strSource)
        If fld.Files.Count > iCount Then
            fld.Copy strTarget
            fld.Delete Force:=True
        End If
    End Sub