I have the following code that successfully copies a folder from one location to another. Before it does the copy I would like to know if any of the files within the folder and its sub folders are open. The issue is I don't know what the names of the files will be and also they can be in any format i.e. Excel, CSV, Text PDF etc.
Question Summary: Can I check the contents of the source folder to check if any of the files contained within are open?
Sub Move_Rename_Folder()
'This example move the folder from FromPath to ToPath.
Sheets("Variables").Select
Dim fso As Object
Dim FromPath As String
Dim ToPath As String
FromPath = Range("b22")
ToPath = Range("b24")
'Note: It is not possible to use a folder that exists in ToPath
If Right(FromPath, 1) = "\" Then
FromPath = Left(FromPath, Len(FromPath) - 1)
End If
If Right(ToPath, 1) = "\" Then
ToPath = Left(ToPath, Len(ToPath) - 1)
End If
Set fso = CreateObject("scripting.filesystemobject")
If fso.folderexists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
If fso.folderexists(ToPath) = True Then
MsgBox ToPath & " exists already, not possible to move to a existing folder"
Exit Sub
End If
fso.CopyFolder Source:=FromPath, Destination:=ToPath
MsgBox "The folder is moved from " & FromPath & " to " & ToPath
End Sub
Thanks,
Des