vbscript >> copy text files only from all subfolders within a folder to a another folder

by Yasser » Mon, 26 May 2008 04:31:10 GMT

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.

could any one help?

thanks in advance

vbscript >> copy text files only from all subfolders within a folder to a another folder

by Pegasus (MVP) » Mon, 26 May 2008 05:04:04 GMT



This is a standard maintenance task that can most easily be
performed with a two-line batch file:
@echo off
cd /d "d:\Test"
for /d %%a in (*.*) do xcopy /y "%%a\*.txt" "D:\Text\%%a\"

It's fairly easy to do in with VB Script too but it requires about
six times as much code.

vbscript >> copy text files only from all subfolders within a folder to a another folder

by James Whitlow » Tue, 27 May 2008 00:02:28 GMT


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
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

vbscript >> copy text files only from all subfolders within a folder to a another folder

by Pegasus (MVP) » Tue, 27 May 2008 00:27:47 GMT


It seems I was a little conservative with my estimate. I assume
that your VB Script solution is functionally equivalent to my
batch file solution. The size of my batch file is 88 bytes, the size
of your script solution 882 bytes, ten times as much, probably
requiring then times the development, debugging and maintenance
time. There appears to be a bug in it already - the OP wanted
c:\Text as a target folder. Your script copies all files into c:\Test,
which is not quite the same thing.

Still, it's a VB Script solution which is what the OP wanted!

vbscript >> copy text files only from all subfolders within a folder to a another folder

by James Whitlow » Tue, 27 May 2008 00:50:38 GMT


I think it is, but it would be much easier to fix the date problem in
your batch code (by simply adding the '/D' switch to XCOPY) than it would be
in my VBScript code.


I absolutely agree! A VBScript solution is not always the best.solution
when a simple batch will do.


Thanks for pointing this out. I normally do a quick code test in my ram
disk before posting. Sometimes I forget to change the path back. So, to the
OP, please change the folder in the first line [sRoot = "R:\DTest\"] to
"D:\Test" if you decide to use the VBScript code.

Similar Threads

1. copy text files only from all subfolders within a folderto a another folder

2. copy text files only from all subfolders within a folder to a

thanks for the reply, but i know how to do it using a batch file, what is 
required is to accomplish this task using a vb script

any Idea

"Pegasus (MVP)" wrote:

> 
> <Yasser Elzefzaf> wrote in message 
> news: XXXX@XXXXX.COM ...
> > 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.
> >
> > could any one help?
> >
> > thanks in advance
> 
> This is a standard maintenance task that can most easily be
> performed with a two-line batch file:
> @echo off
> cd /d "d:\Test"
> for /d %%a in (*.*) do xcopy /y "%%a\*.txt" "D:\Text\%%a\"
> 
> It's fairly easy to do in with VB Script too but it requires about
> six times as much code. 
> 
> 
> 

3. copy folder's files to a new nonexisting folder's subfolder - Word VBA

4. copy a single file to multiple folders and subfolders

Hello,

I have this script here:

Dim fso 
Const OverwriteExisting = True
Set FSO = CreateObject("Scripting.FileSystemObject")
Doc = "c:\test.txt"
ShowSubfolders FSO.GetFolder("C:\temp")
Sub ShowSubFolders(Folder)
 For Each Subfolder in Folder.SubFolders
 fso.copyfile Doc, OverwriteExisting
 ShowSubFolders Subfolder
 Next
End Sub

Basically, I want to copy text.txt to every folder underneath c:\temp.  
There are multiple levels of folders beneath c:\temp.  I think I'm missing 
something with the copy line, because it's only copying the file to the first 
level of folders, and nothing else beneath that.

Thanks in advance...
Troy

5. copy 1 file to all folders and subfolders

6. Creating a new folder named backup in each folder within a folder

I've used VB Script once to create an Autoload feature for a CDRom.

I have folder containing directories of surveyed sites. Each directory is 
named as per the site name. Each directory contains further folders and 
reports. I want to create a single folder within each site folder named 
"Backup" to allow our surveyors to save their work in progress.

Can a VB Script achieve this?

Regards
Dylan
Dawson
Scotland 


7. Copy all files from a folder to another folder, recursively - VB.Net

8. Create a new folder and then copy files into that same folder

Hello,

I am having dificulties figuring this one out. I need to create a new
folder that is labeled with the date in YearMonthDay format and then
copy files that were delivered that day into the newly created folder.
I have been able to successfully create the folder but cannot figure
out how to copy the files into the new folder. I am trying to do all
this in the same script to simplify things. Am I going about this
wrong? Below is the script. The first part works fine but then dumps
the files into the folder called NewFolder instead of one labeled by
yesterdays date. Any help is highly appreciated. Thanks!

'// START
'// CreateDailyFolder-MoveFiles.vbs

Option Explicit

Dim objFSO, yesterday, yy, mt, dd, NewFolder

Set objFSO = CreateObject("Scripting.FileSystemObject")

	yesterday = (Date-1)
	yy = Right(Year(yesterday), 2)
	mon = Right("00" & Month(yesterday), 2)
	dd = Right("00" & Day(yesterday), 2)

	NewFolder = "D:\test\folderb\" & yy & mon & dd
'Create the folder
objFSO.CreateFolder NewFolder

objFSO.CopyFolder "D:\test\foldera", "D:\test\folderb\NewFolder", True

'//END