Solved

advanced file copy vbs script required

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Trying to fix NUMBER ARRANGE Button to load faster and more efficiently. 20 32
Problem to page 4 70
Message not shown 5 29
Problem to picture file 20 30
This article will show, step by step, how to integrate R code into a R Sweave document
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

919 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

12 Experts available now in Live!

Get 1:1 Help Now