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

Tech-Archive recommends: Fix windows errors by optimizing your registry




"Yasser Elzefzaf" <YasserElzefzaf@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:7A4CF8AA-DD07-4ADA-92D4-216C127619C4@xxxxxxxxxxxxxxxx
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..."
--------------------------------------------------------------------------------------------------------------

I could not quite follow the logic in your script. Parts of it
appeared to recursive. Note also the following points:
- Avoid embedding important constants deep inside in your code.
Put them right up the top where they are highly visible. This
makes maintenance much, much easier.
- Keep your main routine in one contiguous block. Spreading
it before, in between and after your subroutines gets
confusing.
Try this script. It is fully tested.

01. Source = "D:\Temp\"
02. Target = "D:\Text\"
03. ext = ".txt"
04.
05. Set objFSO = CreateObject("Scripting.FileSystemObject")
06. If Not objFSO.FolderExists(Target) then objFSO.CreateFolder(Target)
07.
08. For Each sFolder In objFSO.GetFolder(Source).SubFolders
09. CopyTextFiles Source & sFolder.Name
10. Next
11. WScript.Echo "Done..."
12.
13. Sub CopyTextFiles (subFolder)
14. For Each objFile In objFSO.GetFolder(subFolder).Files
15. If LCase(Right(objFile.Name, 4)) = ext _
16. Then objFSO.CopyFile objFile.Path, Target
17. Next
18. End Sub


.



Relevant Pages

  • Re: Path not found error using FSO GetFolder
    ... Sub ShowSubFolders ... For Each Subfolder in Folder.SubFolders 'Path Not Found error on this ... This is just a test script for part of another project where I need to list ... files in folders, however, I need to ignore folders that can't be accessed ...
    (microsoft.public.scripting.vbscript)
  • Re: Path not found error using FSO GetFolder
    ... Sub ShowSubFolders ... For Each Subfolder in Folder.SubFolders 'Path Not Found error on ... This is just a test script for part of another project where I need to ... list files in folders, however, I need to ignore folders that can't be ...
    (microsoft.public.scripting.vbscript)
  • Re: Path not found error using FSO GetFolder
    ... I'm using a script to get a list of folders for a UNC ... Set objFolder = objFSO.GetFolder ... Sub ShowSubFolders ... For Each Subfolder in Folder.SubFolders ...
    (microsoft.public.scripting.vbscript)
  • Re: Folder enumeration
    ... creating a variable to count the folder ... Sub ShowSubFolders ... WScript.Echo Depth & vbTab & subfolder ... I am trying to write a script to enumerate folders that will allow me ...
    (microsoft.public.scripting.vbscript)
  • Re: Folder enumeration
    ... creating a variable to count the folder ... Sub ShowSubFolders ... WScript.Echo Depth & vbTab & subfolder ... I am trying to write a script to enumerate folders that will allow me ...
    (microsoft.public.scripting.vbscript)