Re: copy text files only from all subfolders within a folder to a

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



first of all thank you so much for such great one, it really worked ;)

i managed the destination folder to which the files will be copied to so as
not to overwrite the rrot one

but i would like to ask something about the below script (the one i tried to
develop) it didn't work, abd every time i try to debug it , it returns
different error

HUG if you don't mind to help:

--------------------------------------------
My Script:
----------------------------------------------

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("D:\Test")
Set colFiles = objFSO.GetFolder(subFolder).Files



AllFolders objFolder

Sub AllFolders (Folder)

For Each SubFolder in Folder.Subfolders

CopyTextFiles subfolder

AllFolders Subfolder

Next
End Sub


Sub CopyTextFiles (subFolder)

For Each objFile in colFiles
arrSplitName = Split(objFile.Name, ".")
strExtension = arrSplitName(UBound(arrSplitName) - 1)
If strExtension = "txt" Then
objFSO.CopyFile objFile.Path, "D:\Text\"
Wscript.Echo objFSO.Name
End If
Next

End Sub

Wscript.Echo "Done..."
--------------------------------------------------------------------------------------------------------------





"James Whitlow" wrote:

<Yasser Elzefzaf> wrote in message
news:2008525163110yasser.fouad@xxxxxxxxxxxx
Dear All,

i'm new in vb scripting and a task was assigned to me so as to creat a vb
script that performs the following:

if i have a folder whose path is: "D:\Test"
and it containes more than one sub folder as: "D:\Test\Sub1",
"D:\Test\Sub2", etc..

the sub folders containes different types of files

what i want to do is to copy only all text files in all the sub folders
under "D:\Test", to a new folder "D:\Text" without copying any of the sub
folders.

Try the code below, but be aware of it's limitations. It is copying (as
specified) instead of moving the files, so every time the code is executed,
all files are re-copied. It also blindly overwrites files in the root
directory, regardless of 'Date Modified'. Files in any sub-folder will
always overwrite the root folder. Same thing for identically named files in
the sub-folders. The last file encountered will always win. If you wish to
change this behavior & would like assistance, reply to this thread.

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sRoot = "R:\DTest\"
sExt = "txt"

If Not Right(sRoot, 1) = "\" Then sRoot = sRoot & "\"

Set oFSO = CreateObject("Scripting.FileSystemObject")
For Each sSub in EnumFolder(sRoot)
For Each oFile in oFSO.GetFolder(sSub).Files
If LCase(oFSO.GetExtensionName(oFile)) = sExt Then
oFSO.CopyFile oFile, sRoot, True
End If
Next
Next

Function EnumFolder(ByVal vFolder)
Dim oFSO, oFolder, sFldr, oFldr
Set oFSO = CreateObject("Scripting.FileSystemObject")
If Not IsArray(vFolder) Then
If Not oFSO.FolderExists(vFolder) Then Exit Function
sFldr = vFolder
ReDim vFolder(-1)
Else sFldr = vFolder(UBound(vFolder))
End If
Set oFolder = oFSO.GetFolder(sFldr)
For Each oFldr in oFolder.Subfolders
ReDim Preserve vFolder(UBound(vFolder) + 1)
vFolder(UBound(vFolder)) = oFldr.Path
EnumFolder vFolder
Next
EnumFolder = vFolder
End Function
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



.



Relevant Pages

  • RE: Cycle through all folders under Mailbox
    ... The macro seems to crash on the first folder it goes to, ... Public Sub ProcessAllFolders() ... Dim fld As MAPIFolder ... Dim subfld As MAPIFolder ...
    (microsoft.public.outlook.program_vba)
  • Re: Populate a listbox with folder names
    ... why not use the "Browse for folder" dialog ... > Private Sub Form_Open ... Dim FSO As Scripting.FileSystemObject ... Dim SubFolder As Scripting.Folder ...
    (microsoft.public.access.formscoding)
  • *** TOUGH ONE *** Posting Form Results - using multiple selection boxes (ASP)
    ... I was changing the wrong files in the wrong folder. ... directory and the "_fpclass" folder that should be visible ... in the current web - folder view of FrontPage 2003. ... Sub FP_SetLocaleForPage ...
    (microsoft.public.frontpage.programming)
  • RE: Collect Info from Wkbks in a Folder with Criteria to 1 sheet.
    ... MsgBox ("Cannot open folder - Exiting Macro") ... Exit Sub ... Can you set a filter mechanism so that if ANY files in TEST FOLDER is ...
    (microsoft.public.excel.programming)
  • RE: Web Query Issue
    ... goes to next sub folder, i.e. the rest of htm files in current sub folder ... all htm files from Temporary Internet Files. ... you delete temporary internet files and view files. ...
    (microsoft.public.excel.misc)