a vbs script to copy multiple folders(2) and contents to multiple computers

I have two folders containing approx 1000 files each that I need to periodically copy over to multiple computers on my network.  Preferably using VBS as I have some small experience in vbs.
LVL 1
techieguy_100Asked:
Who is Participating?
 
RobSampsonConnect With a Mentor Commented:
OK, I've modified this a bit now.....it should work....

Regards,

Rob.
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
SourceDrive = "D:\"
echosrc="software\echosoundfiles\canada\echo"
echosoundfilessrc="software\echosoundfiles\canada\echosoundfiles"
echodest = "C:\Echo Stuff\"
echosoundfilesdest = "C:\Echo Stuff\"
 
BackupFolder SourceDrive & echosrc, Replace(echodest, ":", "$")
BackupFolder SourceDrive & echosoundfilessrc, Replace(echosoundfilesdest, ":", "$")
 
Set objFld1=Nothing
Set objFld2=Nothing
Set objFSO=Nothing
 
MsgBox "Done"
 
Sub BackupFolder(strSourceFld, strTargetFld)
  arrComputers=Split(objFSO.OpenTextFile("D:\TargetList.txt").ReadAll,vbNewLine)
  For Each PC in arrComputers
    If Ping(PC) = True Then
       CreateRemoteFolderStructure("\\" & PC & "\" & strTargetFld)
       objFSO.CopyFolder strSourceFld, "\\" & PC & "\" & strTargetFld, True
    End If
  Next
End Sub
 
Function Ping(strComputer)
      Dim objShell, boolCode
      Set objShell = CreateObject("WScript.Shell")
      boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
      If boolCode = 0 Then
            Ping = True
      Else
            Ping = False
      End If
End Function
 
Sub CreateRemoteFolderStructure(strFolderPath)
	If Right(strFolderPath, 1) = "\" Then strFolderPath = Left(strFolderPath, Len(strFolderPath) - 1)
	intStart = InStr(Mid(strFolderPath, 3), "\") + 3
	intStart = InStr(intStart, strFolderPath, "\")
	strFolder = Left(strFolderPath, intStart - 1)
	For Each strBit In Split(Mid(strFolderPath, intStart + 1), "\")
		strFolder = strFolder & "\" & strBit
		'MsgBox "Checking for " & strFolder
		If Not objFSO.FolderExists(strFolder) Then objFSO.CreateFolder(strFolder)
	Next
End Sub

Open in new window

0
 
sirbountyCommented:
Something like this could work.
Setup two objects for the source folders, and a list of target PCs, each with a carriage return at the end, and fire away...
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Folder1="C:\FolderA\"
Folder2="C:\FolderB\"
 
Dim objFld1 : Set objFld1=objFSO.GetFolder(Folder1)
Dim objFld2 : Set objFld1=objFSO.GetFolder(Folder2)
 
BackupFolder(objFld1)
BackupFolder(objFld2)
 
Set objFld1=Nothing
Set objFld2=Nothing
Set objFSO=Nothing
 
Sub BackupFolder(fld)
  arrComputers=Split(objFSO.OpenTextFile("C:\TargetPC-List.txt").ReadAll,vbNewLine)
  For Each PC in arrComputers
    For Each file in fld  
      objFSO.CopyFile file, "\\" & PC & "\C$\TargetFolder\"
    Next
  Next
End Sub

Open in new window

0
 
techieguy_100Author Commented:
Thanks for the fast response, I tried it and got the attached error.  This script looks like it copies the two folders into one on the destination drive. If that is correct, I'd like to have the two folders separate on the destination drive and overwrite the folders if they are already there.

Thanks again,
Glen
echocpyerror.bmp
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
sirbountyCommented:
In the code above, you don't seem to be using the source folder variables...
Give this one a shot and let me know.
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
SourceDrive="D:\"
Folder1="echo\
Folder2="echosoundfiles\"
 
Dim objFld1 : Set objFld1=objFSO.GetFolder(Folder1)
Dim objFld2 : Set objFld1=objFSO.GetFolder(Folder2)
 
BackupFolder(objFld1)
BackupFolder(objFld2)
 
Set objFld1=Nothing
Set objFld2=Nothing
Set objFSO=Nothing
 
Sub BackupFolder(fld)
  arrComputers=Split(objFSO.OpenTextFile("C:\TargetPC-List.txt").ReadAll,vbNewLine)
  For Each PC in arrComputers
    For Each file in fld  
      TargetPath="\\" & PC & "\C$\echo stuff" & Replace(fld,fld.Drive,"") & "\"
      objFSO.CopyFile file, TargetPath
    Next
  Next
End Sub

Open in new window

0
 
RobSampsonCommented:
Hi, you'll also probably want to ping the target machine first, and try to create the target folder if it doesn't exist....

Regards,

Rob.
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Folder1="C:\FolderA\"
Folder2="C:\FolderB\"
 
Dim objFld1 : Set objFld1=objFSO.GetFolder(Folder1)
Dim objFld2 : Set objFld1=objFSO.GetFolder(Folder2)
 
BackupFolder objFld1
BackupFolder objFld2
 
Set objFld1=Nothing
Set objFld2=Nothing
Set objFSO=Nothing
 
Sub BackupFolder(fld)
  arrComputers=Split(objFSO.OpenTextFile("C:\TargetPC-List.txt").ReadAll,vbNewLine)
  For Each PC in arrComputers
    If Ping(PC) = True Then
       If objFSO.FolderExists("\\" & PC & "\C$\TargetFolder\") = False Then objFSO.CreateFolder("\\" & PC & "\C$\TargetFolder\")
       For Each file in fld.Files
         objFSO.CopyFile file.path, "\\" & PC & "\C$\TargetFolder\"
       Next
    End If
  Next
End Sub
 
Function Ping(strComputer)
      Dim objShell, boolCode
      Set objShell = CreateObject("WScript.Shell")
      boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
      If boolCode = 0 Then
            Ping = True
      Else
            Ping = False
      End If
End Function

Open in new window

0
 
techieguy_100Author Commented:
I think I solved one problem but now a new error. :)
echocpyerror.bmp
0
 
sirbountyCommented:
You're still pointing to the folder names, not the variables...use this and it should work:


Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
SourceDrive="D:\"
echo="software\echosoundfiles\canada\echo\"
echosoundfiles="software\echosoundfiles\canada\echosoundfiles\"

Open in new window

0
 
techieguy_100Author Commented:
Hi Rob,
I got the same error as before line 5 character 15 invalid proceedure call...
I noticed that in the brackets in that line you have "folder1" which I have been changing to the source folder name.  Does that have something to do with the error?
0
 
techieguy_100Author Commented:
Hi Rob,

We are making progress, I have another error farther down in the script, please see attached.

Glen
echocpyerror.bmp
0
 
sirbountyCommented:
You have no reference to the folder now...

If you're using these two lines:
echo="software\echosoundfiles\canada\echo\"
echosoundfiles="software\echosoundfiles\canada\echosoundfiles\"

Then you need to use:

Set objFld1=objFSO.GetFolder(echo)
Set objFld2=objFSO.GetFolder(echosoundfiles)

just prior to these lines:
BackupFolder objFld1
BackupFolder objFld2
0
 
techieguy_100Author Commented:
Sir Bounty,
When I added those two lines, i received the following error;
Line: 6
Char:1
Error: Path not Found
Code: 800a004c

Glen
0
 
RobSampsonCommented:
Hi, you now get that error because you separated SourceDrive, so those lines should be:

Set objFld1=objFSO.GetFolder(SourceDrive & echo)
Set objFld2=objFSO.GetFolder(SourceDrive & echosoundfiles)

Anyway, I've ammended the code, and the below should be fixed.

Regards,

Rob.
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
SourceDrive = "D:\"
echo="software\echosoundfiles\canada\echo"
echosoundfiles="software\echosoundfiles\canada\echosoundfiles"
 
Dim objFld1 : Set objFld1=objFSO.GetFolder(SourceDrive & echo)
Dim objFld2 : Set objFld1=objFSO.GetFolder(SourceDrive & echosoundfiles)
 
BackupFolder objFld1
BackupFolder objFld2
 
Set objFld1=Nothing
Set objFld2=Nothing
Set objFSO=Nothing
 
Sub BackupFolder(fld)
  arrComputers=Split(objFSO.OpenTextFile("D:\TargetList.txt").ReadAll,vbNewLine)
  For Each PC in arrComputers
    If Ping(PC) = True Then
       If objFSO.FolderExists("\\" & PC & "\C$\Echo Stuff\") = False Then objFSO.CreateFolder("\\" & PC & "\C$\Echo Stuff\")
       For Each file in fld.Files
         objFSO.CopyFile file.path, "\\" & PC & "\C$\Echo Stuff\"
       Next
    End If
  Next
End Sub
 
Function Ping(strComputer)
      Dim objShell, boolCode
      Set objShell = CreateObject("WScript.Shell")
      boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
      If boolCode = 0 Then
            Ping = True
      Else
            Ping = False
      End If
End Function

Open in new window

0
 
RobSampsonCommented:
Oh, and for a decent editor, besides Notepad, try VBSEdit from www.vbsedit.com

Regards,

Rob.
0
 
techieguy_100Author Commented:
This is now looking promising, it would appear that the files are copying, but I didn't get the folders, so all ~2000 files are now in one folder.(or will be once it has finished copying).

Thanks, I'll try that editor.
0
 
RobSampsonCommented:
OK, so what is the end folder structure you're looking for?

So you have these two source folders:
echo="software\echosoundfiles\canada\echo"
echosoundfiles="software\echosoundfiles\canada\echosoundfiles"

and this is the target fodler on the remote machine:
C:\Echo Stuff\

The way the code is currently written, it assumes there are no subfolders in your source echo or echosoundfiles folders.

Now, on the target machine, do you just want to have
C:\Echo Stuff\Echo
and
C:\Echo Stuff\EchoSoundFiles

with all of the files of both folders?  We should just be able to the CopyFolder method for that:
http://msdn.microsoft.com/en-us/library/xbfwysex(VS.85).aspx

This should work....

Rob.
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
SourceDrive = "D:\"
echo="software\echosoundfiles\canada\echo"
echosoundfiles="software\echosoundfiles\canada\echosoundfiles"
 
Dim objFld1 : Set objFld1=objFSO.GetFolder(SourceDrive & echo)
Dim objFld2 : Set objFld1=objFSO.GetFolder(SourceDrive & echosoundfiles)
 
BackupFolder objFld1
BackupFolder objFld2
 
Set objFld1=Nothing
Set objFld2=Nothing
Set objFSO=Nothing
 
Sub BackupFolder(fld)
  arrComputers=Split(objFSO.OpenTextFile("D:\TargetList.txt").ReadAll,vbNewLine)
  For Each PC in arrComputers
    If Ping(PC) = True Then
       If objFSO.FolderExists("\\" & PC & "\C$\Echo Stuff\") = False Then objFSO.CreateFolder("\\" & PC & "\C$\Echo Stuff\")
       objFSO.CopyFolder fld.Path, "\\" & PC & "\C$\Echo Stuff\", True
    End If
  Next
End Sub
 
Function Ping(strComputer)
      Dim objShell, boolCode
      Set objShell = CreateObject("WScript.Shell")
      boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
      If boolCode = 0 Then
            Ping = True
      Else
            Ping = False
      End If
End Function

Open in new window

0
 
techieguy_100Author Commented:
Rob,

Just to be sure we are shooting at the same goal, the source directories are ...\canada\echo & ...\canada\echosound files
the target directories are c:\echo stuff\echo & c:\echo\echosoundfiles

Glen
0
 
RobSampsonCommented:
OK, so for each source folder, you've got two separte target folders?

So you want:
"software\echosoundfiles\canada\echo"    --->  C:\echo stuff\echo
and
"software\echosoundfiles\canada\echosoundfiles"   --->  C:\echo\echosoundfiles

is that right?

Rob.
0
 
techieguy_100Author Commented:
Yes, that is correct.

The script appears to run great, but it did have an error, unfortunately I accidentally cleared the error before I got a chance to read it, so I am running the script again.
0
 
techieguy_100Author Commented:
OK, here's the new error.

Line:21
Char:8
error: Object requiered: 'fld'
code: 800a01a8

it did create one subfolder before the error.

Glen
0
 
techieguy_100Author Commented:
One correction;

for each source folder I have one separate destination folder

...echosoundfiles--->c:\echosoundfiles
...echo--->c:\echo

Glen
0
 
techieguy_100Author Commented:
Sorry,

destination folders are c:\echo stuff\echo & c:\echo stuff\echosoundfiles
0
 
techieguy_100Author Commented:
OK, It looks like I am going to have to call it quits for the night.  My boss has a serious allergy to my getting any overtime. :)

Hopefully, we can continue with this tomorrow.  Thank you so much for your help so far.

Glen
0
 
techieguy_100Author Commented:
You guys are awsome!  It is working, I never would have got this on my own.  one round of donuts, :)
Glen
0
 
techieguy_100Author Commented:
Now I know why others praise this site so much!  
0
 
RobSampsonCommented:
Great. Thanks for the grade.

Regards,

Rob.
0
 
sirbountyCommented:
techiguy_100, I'm not sure that you're aware that you can split points here, but I believe I had a hand at helping you arrive at a solution....kind of surprised not to have received anything here...
0
 
techieguy_100Author Commented:
Hey, Sir bounty!

Sorry, I'm kind of new at this, how do I give you credit too?

Glen
0
 
RobSampsonCommented:
Glen,

As you are accepting a solution, you should be able to select more than one comment from one or more experts, if they contributed to your answer.

Now, however, because it's already accepted, I think you would need to post a request in the Community Support zone to have the question re-PAQ'd.

Regards,

Rob.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.