Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default Copy Excel files from sub-folders but restricted to one level

Hi All,

Using 'FileSystemObject' i would like to copy all the Excel files from
the sub-folders to another directory but restricting those sub folders
to one level. Additionally l would like to exclude some specifically
named sub-folders at level one.

Example:

C:\MyName\MyFiles\ThisMonth
C:\MyName\MyFiles\ThisMonth\Sub-Folder1
C:\MyName\MyFiles\ThisMonth\Sub-Folder2\more folders
C:\MyName\MyFiles\ThisMonth\Sub-Folder3\more folders
C:\MyName\MyFiles\ThisMonth\Sub-FolderIgnore1
C:\MyName\MyFiles\ThisMonth\Sub-FolderIgnore2

Using the directory structure above as an example l would like to copy
the Excel files that exist in Sub-Folder1, Sub-Folder2 & Sub-Folder3
only.

Any example code gratefully appreciated.

Regards

Michael
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,549
Default Copy Excel files from sub-folders but restricted to one level

Something structured like the following should work...
'--
Sub Pretend()
Dim strPath As String
Dim oFSO As Object
Dim oFolder As Object
Dim vPaths As Variant
Dim N As Long

vPaths = Array(folder1_Path, folder2_Path, folder3_Path)
Set oFSO = CreateObject("Scripting.FileSystemObject")

For N = 0 To UBound(vPaths)
strPath = vPaths(N)
Set oFolder = oFSO.GetFolder(strPath)
'lots of code here
Next 'N
End Sub
--
Jim Cone
Portland, Oregon USA



"michael.beckinsale"

wrote in message
Hi All,
Using 'FileSystemObject' i would like to copy all the Excel files from
the sub-folders to another directory but restricting those sub folders
to one level. Additionally l would like to exclude some specifically
named sub-folders at level one.

Example:

C:\MyName\MyFiles\ThisMonth
C:\MyName\MyFiles\ThisMonth\Sub-Folder1
C:\MyName\MyFiles\ThisMonth\Sub-Folder2\more folders
C:\MyName\MyFiles\ThisMonth\Sub-Folder3\more folders
C:\MyName\MyFiles\ThisMonth\Sub-FolderIgnore1
C:\MyName\MyFiles\ThisMonth\Sub-FolderIgnore2

Using the directory structure above as an example l would like to copy
the Excel files that exist in Sub-Folder1, Sub-Folder2 & Sub-Folder3
only.
Any example code gratefully appreciated.
Regards
Michael
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default Copy Excel files from sub-folders but restricted to one level


Hi Jim,

Thanks for the reply. I am either not understanding your reply or did
not explain myelf very well.

Your example seems to imply that l know the paths of the files l want
to copy and that l build an array and loop thru the array to copy
them.

Basically l have lets say the 'TargetFolder', below this are many sub-
folders 1 level down, which may have been added too or deleted. I want
to copy the Excel files from each of these sub-folders but not the sub-
folders below that level but want to exclude 2 specific folders that l
do know the name / path of. Hope this is a bit clearer.

I have used the following code to copy files where the directory
structure is simpler and hoped that l could amend to accommodate the
above.

Sub Copy_GROUP()

Dim strFromPath As String
Dim strToPath As String
Dim Fso As Object

Sheets("Control").Activate
strFromPath = ThisWorkbook.Path & "TEST_GROUP_*.xls"

strToPath = Range("tCopyDefra") <<<file path exists in this named
range

Set Fso = CreateObject("Scripting.FileSystemObject")

'Copy all of the .xls files - , False prevents overwriting of
files
Fso.CopyFile strFromPath, strToPath
Set Fso = Nothing
End Sub

Any help gratefully received

Regards

Michael
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 88
Default Copy Excel files from sub-folders but restricted to one level

Are you wanting to recreate the sub-folder structure underneath the new
folder location, or do you just want to copy ALL files found in the
sub-folders to the (single) new folder?

Eric

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default Copy Excel files from sub-folders but restricted to one level

Hi Eric,

At the moment just copy to a new single folder.

Recreating the sub-folder structure would be nice but at the moment l
am getting short of time.

Hope my post / explanation makes sense.

Hope you can help

Regards

Michael


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,549
Default Copy Excel files from sub-folders but restricted to one level

Michael,
Another approach...
'--
-snip-
strFromPath = ThisWorkbook.Path & "TEST_GROUP_*.xls"
Set oFolder = oFSO.GetFolder(strFromPath)
'snip
Fso.CopyFile strFromPath, strToPath

Call CopyOtherFiles(oFolder)

Set Fso = Nothing
End Sub
'--
Function CopyOtherFiles(ByRef oParentFolder As Object)
Dim oSubFolder As Object
Dim N As Long
For Each oSubFolder In oParentFolder.SubFolders
If oSubFolder.Name < "Sludge" And _
oSubFolder.Name < "Residue" Then
'copy files
N = N + 1
End If
If N 3 Then Exit Function
Next 'oSubFolder
End Function
'--
I am not sure in what order the folders will be returned.
Its conceivable you might have to return them all and do
some sort of filter on the folder names.
Note: the Windows Script 5.6 help file is invaluable.
If you don't have it, get it...
http://www.microsoft.com/downloads/d...DisplayLang=en
--
Jim Cone
Portland, Oregon USA




"michael.beckinsale"

wrote in message
Hi Jim,
Thanks for the reply. I am either not understanding your reply or did
not explain myelf very well.
Your example seems to imply that l know the paths of the files l want
to copy and that l build an array and loop thru the array to copy
them.
Basically l have lets say the 'TargetFolder', below this are many sub-
folders 1 level down, which may have been added too or deleted. I want
to copy the Excel files from each of these sub-folders but not the sub-
folders below that level but want to exclude 2 specific folders that l
do know the name / path of. Hope this is a bit clearer.
I have used the following code to copy files where the directory
structure is simpler and hoped that l could amend to accommodate the
above.

Sub Copy_GROUP()

Dim strFromPath As String
Dim strToPath As String
Dim Fso As Object

Sheets("Control").Activate
strFromPath = ThisWorkbook.Path & "TEST_GROUP_*.xls"

strToPath = Range("tCopyDefra") <<<file path exists in this named
range

Set Fso = CreateObject("Scripting.FileSystemObject")

'Copy all of the .xls files - , False prevents overwriting of
files
Fso.CopyFile strFromPath, strToPath
Set Fso = Nothing
End Sub

Any help gratefully received
Regards
Michael
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default Copy Excel files from sub-folders but restricted to one level

Hi Jim,

That code looks to me as if its on the right lines for what l am
trying to achieve but l suspect l will have to do quite a bit of
'playing around'

The link you gave for the Script help file should as you say prove
invaluable. I have looked for help re VBScript / Scripting in general
and there doesn't seem to be a lot of info about it. It would be great
if it could be referenced via the Object Browser.

I may not get around to using your ideas / code for a couple of days
but will repost to keep you updated.

Again many thanks for your help

Regards

Michael

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 open same name files in different folders in Excel? DrDisk7 Excel Discussion (Misc queries) 1 April 1st 08 02:35 PM
Can anyone help me Create Excel list of files in windows folders solrac1956 Excel Worksheet Functions 1 November 28th 05 11:07 PM
Moving files between folders using VBA in Excel Paul Excel Programming 2 August 5th 05 02:25 PM
Application.FileSearch on only one level of sub folders Crustysquirrel Excel Programming 3 August 3rd 05 09:18 AM
Open Excel files/folders animalfriend7 Excel Discussion (Misc queries) 1 June 23rd 05 11:57 PM


All times are GMT +1. The time now is 09:03 AM.

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

About Us

"It's about Microsoft Excel"