?
Solved

advanced file copy vbs script required

Posted on 2011-02-15
4
Medium Priority
?
558 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

750 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