Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel VBA - Copy Folder problem

I wish to copy a folder and its sub-folders and all its files to anothe
location from within an Excel VBA macro. Is this possible?

Pau

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Excel VBA - Copy Folder problem

Hi Paul,

All things are possible!

Dim FSO As Object

Sub Folders()
Dim i As Long
Dim sFolder As String
Dim sSource As String
Dim sTarget As String

sSource = "C:\MyTest"
sTarget = "C:\NewDir"

Set FSO = CreateObject("Scripting.FileSystemObject")

On Error Resume Next
If FSO.GetFolder(sTarget) Is Nothing Then
MkDir sTarget
End If
On Error GoTo 0

CopyFiles sSource, sTarget

End Sub

'-----------------------------------------------------------------------
Sub CopyFiles(ByVal Source As String, ByVal Target As String)
'-----------------------------------------------------------------------
Dim oFldr As Object
Dim oFolder As Object
Dim oFile As Object
Dim oFiles As Object
Dim sTarget As String

Set oFolder = FSO.GetFolder(Source)
If InStr(4, oFolder.Path, "\") = 0 Then
sTarget = Target
Else
sTarget = Target & Mid(Source, InStr(4, oFolder.Path, "\"), 255)
End If
On Error Resume Next
If FSO.GetFolder(sTarget) Is Nothing Then
MkDir sTarget
End If
On Error GoTo 0
Set oFolder = FSO.GetFolder(Source)
Set oFiles = oFolder.Files
For Each oFile In oFiles
oFile.Copy (sTarget & "\" & oFile.Name)
Next oFile

For Each oFldr In oFolder.Subfolders
CopyFiles oFldr.Path, Target
Next

End Sub

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"PaulC " wrote in message
...
I wish to copy a folder and its sub-folders and all its files to another
location from within an Excel VBA macro. Is this possible?

Paul


---
Message posted from http://www.ExcelForum.com/



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,718
Default Excel VBA - Copy Folder problem

You can duplicate a complete folder like this:

Sub CopyFolder()
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
fso.CopyFolder "c:\old", "c:\new"
End Sub


--
Jim Rech
Excel MVP
"PaulC " wrote in message
...
|I wish to copy a folder and its sub-folders and all its files to another
| location from within an Excel VBA macro. Is this possible?
|
| Paul
|
|
| ---
| Message posted from http://www.ExcelForum.com/
|


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel VBA - Copy Folder problem

Many thanks Bob,

Your copy folder macro works perfectly, with the advantage that it wil
create a new folder if its not already there. I have made use of thi
so that I can create a new one for each day with the date at the end o
the folder name.

Thanks again,

Pau

--
Message posted from http://www.ExcelForum.com

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel VBA - Copy Folder problem

Greetings...and TIA for your help.
Is it possible to exclude a specific file (eg "test.xls") from this
code?I'm not able to get the NAME of the file that is being
processing...any idea?
please help me!!


---
Message posted from http://www.ExcelForum.com/



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Excel VBA - Copy Folder problem

The thread has aged off my cache of messages, but in general:

if lcase(somevariableforthefilename) = "test.xls" then
'do nothing
else
'do all the stuff you want to do
end if



"michael mclane <" wrote:

Greetings...and TIA for your help.
Is it possible to exclude a specific file (eg "test.xls") from this
code?I'm not able to get the NAME of the file that is being
processing...any idea?
please help me!!

---
Message posted from http://www.ExcelForum.com/


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Copy Excel Workbook - Hyperlinks - Folder of Photos - to CD nokomis Excel Worksheet Functions 1 February 9th 09 04:31 AM
Copy folder with excel linked workbooks lightbulb Excel Discussion (Misc queries) 1 June 20th 08 06:01 PM
copy explorer folder list to excel KK New Users to Excel 9 May 14th 07 05:02 AM
Macro to copy range from Excel files in folder nc Excel Discussion (Misc queries) 1 June 15th 05 11:11 AM
Create Folder / Copy Folder / Replace Murray Outtrim[_2_] Excel Programming 0 February 24th 04 06:40 PM


All times are GMT +1. The time now is 11:42 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"