Solved

advanced file copy vbs script required

Posted on 2011-02-15
4
528 Views
Last Modified: 2012-05-11
I need a vbs sript to copy all the files in folder A to folder B

Error control:-
*  If the destination folder (B) does not exist, do not attempt copy or show an error.
*  If the destination folder does exist, but the target files are already there, do not copy the files or show an error.

Thanks!
0
Comment
Question by:browndobe
  • 2
4 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 34895621
Hi,this should do what you are asking.

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
strSource = "C:\Temp\Source"
strDestination = "C:\Temp\Destination"

If objFSO.FolderExists(strSource) = True Then
	If objFSO.FolderExists(strDestination) = True Then
		If Right(strDestination, 1) <> "\" Then strDestination = strDestination & "\"
		If objFSO.FileExists(strDestination & objFile.Name) = False Then objFSO.CopyFile objFile.Path, strDestination, False
	End If
End If

Open in new window

0
 

Author Comment

by:browndobe
ID: 34896330
greetings RobSampson,
                      I get an error:-
Error-01.jpg
0
 
LVL 5

Accepted Solution

by:
Tompa99 earned 50 total points
ID: 34897145
Hi,

It should look something like this to loop true the files.

Regards Tompa


Dim objFso
Dim strSource
Dim strDestination
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
strSource = "C:\Temp\Source"
strDestination = "C:\Temp\Destination"

if objFso.FolderExists(strSource) = true then
	
	if objFso.FolderExists(strDestination) = true then
		set objFolder = objFso.GetFolder(strSource)
		Set objFiles = objFolder.Files
		
		for each objFile in objFiles
		
			If Right(strDestination, 1) <> "\" Then strDestination = strDestination & "\"
			If objFso.FileExists(strDestination & objFile.Name) = False Then objFSO.CopyFile objFile.Path, strDestination, False
		
		next
	end if

end if

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34900797
Oh, I forgot the Files loop...oh well...

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
strSource = "C:\Temp\Source"
strDestination = "C:\Temp\Destination"

If objFSO.FolderExists(strSource) = True Then
	If objFSO.FolderExists(strDestination) = True Then
		If Right(strDestination, 1) <> "\" Then strDestination = strDestination & "\"
		For Each objFile In objFSO.GetFolder(strSource).Files
			If objFSO.FileExists(strDestination & objFile.Name) = False Then objFSO.CopyFile objFile.Path, strDestination, False
		Next
	End If
End If

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 96
Pass through dll 2 81
VBscript/Batch file: copy files/folders based on a csv file 15 82
Looking for a program that deletes all files of same size 18 62
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question