In VB/VBA How do i copy a whole Directory from one location to another???????????????
Printable View
In VB/VBA How do i copy a whole Directory from one location to another???????????????
look on the help for "filecopy"
it might be what ur looking for
I want to copy the whole directory and its contents to the same tree structure in a dirfferent location.
I have noticed that i could use the FSO object to do so but VBA doesn't ref this object.
e.g
Set FSO = CreateObject("Scripting.FileSystemObject")
Can i import a dll that allow to to reference the library containing the FSO object and its methods??
Use the SHFileOperation api function to do this.
Put this is a for under a button call Command1.Code:'********Put this in a module********
Public Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Public Const FO_MOVE = &H1
Public Const FO_COPY = &H2
Public Const FO_DELETE = &H3
Public Const FO_RENAME = &H4
Public Const FOF_SILENT = &H4
Public Const FOF_RENAMEONCOLLISION = &H8
Public Const FOF_NOCONFIRMATION = &H10
Public Const FOF_SIMPLEPROGRESS = & H100
Public Const FOF_ALLOWUNDO = &H40
Public Declare Function SHFileOperation _
Lib "shell32.dll" Alias "SHFileOperationA" _
(lpFileOp As SHFILEOPSTRUCT) As Long
Code:Private Sub Command1_Click()
Dim SHF As SHFILEOPSTRUCT
Dim lret As Long
SHF.wFunc = FO_COPY
SHF.hWnd = Me.hWnd
SHF.pFrom = "c:\windows\desktop\tiff\*.*"
SHF.pTo = "c:\windows\desktop\test\"
SHF.fFlags = FOF_SILENT
lret = SHFileOperation(SHF)
'Returns 0 if successful
If lret <> 0 Then
MsgBox "Error"
End If
End Sub
Or you can use FileSystemObject.
Code:Dim FSO as Object
Private SubForm_Load()
Set FSO = CreateObject("Scripting.FileSystemObject")
'Syntax is object.MoveFolder source, destination
FSO.MoveFolder "c:\myFolder\","c:\backup\myfolder\"
End Sub
Thanks, that works fine