Solved

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

Posted on 2008-06-10
28
995 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:techieguy_100
  • 15
  • 8
  • 5
28 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 21754754
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
 
LVL 1

Author Comment

by:techieguy_100
ID: 21755400
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 21755453
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 65

Expert Comment

by:RobSampson
ID: 21755502
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
 
LVL 1

Author Comment

by:techieguy_100
ID: 21755559
I think I solved one problem but now a new error. :)
echocpyerror.bmp
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 21755644
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
 
LVL 1

Author Comment

by:techieguy_100
ID: 21755652
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
 
LVL 1

Author Comment

by:techieguy_100
ID: 21755722
Hi Rob,

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

Glen
echocpyerror.bmp
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 21755759
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
 
LVL 1

Author Comment

by:techieguy_100
ID: 21755846
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 21755923
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 21755931
Oh, and for a decent editor, besides Notepad, try VBSEdit from www.vbsedit.com

Regards,

Rob.
0
 
LVL 1

Author Comment

by:techieguy_100
ID: 21755971
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 21756037
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
 
LVL 1

Author Comment

by:techieguy_100
ID: 21756061
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 21756091
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
 
LVL 1

Author Comment

by:techieguy_100
ID: 21756170
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
 
LVL 1

Author Comment

by:techieguy_100
ID: 21756182
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
 
LVL 1

Author Comment

by:techieguy_100
ID: 21756192
One correction;

for each source folder I have one separate destination folder

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

Glen
0
 
LVL 1

Author Comment

by:techieguy_100
ID: 21756196
Sorry,

destination folders are c:\echo stuff\echo & c:\echo stuff\echosoundfiles
0
 
LVL 1

Author Comment

by:techieguy_100
ID: 21756249
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
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 21756252
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
 
LVL 1

Author Closing Comment

by:techieguy_100
ID: 31465913
You guys are awsome!  It is working, I never would have got this on my own.  one round of donuts, :)
Glen
0
 
LVL 1

Author Comment

by:techieguy_100
ID: 21756318
Now I know why others praise this site so much!  
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21756320
Great. Thanks for the grade.

Regards,

Rob.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 21758702
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
 
LVL 1

Author Comment

by:techieguy_100
ID: 21760815
Hey, Sir bounty!

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

Glen
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21764958
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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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