Solved

advanced file copy vbs script required

Posted on 2011-02-15
4
517 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
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 …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now