Thursday, 11 August 2011

Copy All Files From Folder and Subfolders

Hi,

Sometimes we require to copy or move the files which are resided deep in different folders such as photos from mobiles. When ever we take backup we usually don't move the data we just copy and there are also lots of duplication of data. So the following VBS code will just do your job of getting all the data in one folder instead of moving inside every folder to search specific data.
Following are the steps to execute the code.
Step 1: Copy the data in text File and save it as .vbs file.
Step 2: Copy your source and destination in the place where background in code is red.
Step 3: Now save the code and just double click the file.

May this help you too.

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Call ListFolderContents("D:\Src\")
Set fso = Nothing

Sub ListFolderContents(path)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim strSourceFolder
Dim strDestFolder
strDestFolder="D:\ALL Dist\"
Set folder = fso.GetFolder(path)
'wscript.echo folder.path


For Each objFile in folder.files
sourceFileName = fso.GetFileName(objFile)
'wscript.echo folder.path+"\"+sourceFileName
sourceFileName = folder.path+"\"+sourceFileName
If fso.FileExists(sourceFileName) Then
If not fso.FileExists(strDestFolder) Then
fso.CopyFile sourceFileName, strDestFolder, TRUE
End If
End If
Next
'fso.CopyFile sourceFile, destFileName

For each item in folder.SubFolders
ListFolderContents(item.Path)

Next
set folder = Nothing
End Sub

No comments:

Post a Comment