Re: copy text files only from all subfolders within a folder to a
- From: "James Whitlow" <jwhitlow.60372693@xxxxxxxxxxxxx>
- Date: Mon, 26 May 2008 16:09:00 -0500
"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
Copying and running your script as posted gives me an error on line 3:
"Error: Invalid procedure call or argument"
The problem appears to be that you are trying to set 'colfiles' to the
files in the directory defined by 'subfolder', but you have not yet set the
value of 'subfolder'. I think you meant to use 'objFolder' here.
Once I change line 3 to [Set colFiles =
objFSO.GetFolder(objFolder).Files] , the script runs, but does not actually
do anything because of an error in the line that gets the file extension:
Change it **From**: strExtension = arrSplitName(UBound(arrSplitName) -
1)
**To**: strExtension = arrSplitName(UBound(arrSplitName) )
With these changes, you will get the files in the root of "D:\Test", but
not the subfolders since you are not recalculating 'colFiles' for each
subfolder. If you add the line:
Set colFiles = objFSO.GetFolder(subFolder).Files
...just inside "Sub CopyTextFiles", it should give you the results you
are looking for.
Lastly, the code is not copying the text files from the root (I assume
you want this). Just add the line:
CopyTextFiles objFolder
...just about the [AllFolders objFolder] line.
With these changes, your code should do exactly what you want, while
mine will not (see below). If you don't want the text file in the root and
only the text files inside the subfolders, leave this out.
My originally posted code had a mistake in it that caused it not to
carry forward any subfolders below the first level in the array. I have
corrected it and posted it below.
Your code is enumerating the subdirectories and copying the files as it
goes. Mine is creating a complete array of the subdirectories first and then
walking the array. For what you are doing, your code is going to be more
compact and easier to read. I would personally suggest using your code. I
have, however, posted a corrected version of mine below.
Also, please note, that it looking at your code here, I realized that I
misread your original post and was not doing exactly what you wanted. In
your original message you stated you wanted all text file from 'D:\Test' to
go to 'D:\Text'. I did not notice the 's' changing to 'x' and falsly
concluded that you wanted all of the file from the subfolders of the
specified folder copied to the root of that folder. Sorry about that, my
mistake.
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sSourceDir = "D:\Test\"
sDestDir = "D:\Text\"
sExt = "txt"
If Not Right(sSourceDir, 1) = "\" Then sSourceDir = sSourceDir & "\"
If Not Right(sDestDir, 1) = "\" Then sDestDir = sDestDir & "\"
Set oFSO = CreateObject("Scripting.FileSystemObject")
For Each sSub in EnumFolder(sSourceDir)
For Each oFile in oFSO.GetFolder(sSub).Files
If LCase(oFSO.GetExtensionName(oFile)) = sExt Then
oFSO.CopyFile oFile, sDestDir, True
End If
Next
Next
Function EnumFolder(ByRef 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(0)
vFolder(0) = oFSO.GetFolder(sFldr).Path
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
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.
- References:
- copy text files only from all subfolders within a folder to a another folder
- From: Yasser Elzefzaf
- Re: copy text files only from all subfolders within a folder to a another folder
- From: James Whitlow
- Re: copy text files only from all subfolders within a folder to a
- From: Yasser Elzefzaf
- copy text files only from all subfolders within a folder to a another folder
- Prev by Date: Re: copy text files only from all subfolders within a folder to a
- Next by Date: Re: copy text files only from all subfolders within a folder to a
- Previous by thread: Re: copy text files only from all subfolders within a folder to a
- Next by thread: Re: copy text files only from all subfolders within a folder to a
- Index(es):
Relevant Pages
|
|