Re: copy text files only from all subfolders within a folder to a
- From: Yasser Elzefzaf <YasserElzefzaf@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 26 May 2008 10:00:01 -0700
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
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Follow-Ups:
- Re: copy text files only from all subfolders within a folder to a
- From: Pegasus \(MVP\)
- Re: copy text files only from all subfolders within a folder to a
- From: James Whitlow
- Re: copy text files only from all subfolders within a folder to a
- 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
- copy text files only from all subfolders within a folder to a another folder
- Prev by Date: Re: Migrating users to another domain but keeping their SIDs
- 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 another folder
- Next by thread: Re: copy text files only from all subfolders within a folder to a
- Index(es):
Relevant Pages
|