none
Script to copy folders to multiple PCs simultaneously

    Question

  • Hi,
    I often have to copy folders to multiple PCs in labs and have created a script to read the name of the PC from a txt file, read the name of the folder from a txt file and copy out to the machines as bellow. Problem is I need it to copy out to the PCs in the txt file simultaneously. I’m copying VM’s that can be up to 25Gb in size so it’s not viable to copy out to just one machine at a time or it’ll be running all day! Is this possible??
    Script I’ve got so far is:
    InputmachineFile = "C:\VMDEPLOYMENT SCRIPTS\machinelist.Txt"
    Inputvmfile = "C:\VMDEPLOYMENT SCRIPTS\vmlist.txt"
    strFolderroot = "\\staffclus\issimages$\VMWare\VM Labs 2011-12\C-Drive"
    machinecount=0
    vmcount=0 
    Const OverwriteExisting = True
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objmachineFile = objFSO.OpenTextFile(InputmachineFile)
    Set objvmFile = objFSO.OpenTextFile(InputvmFile)
    Do While Not (objvmFile.AtEndOfStream)
    strvm = objvmFile.ReadLine
    strfolderName = strFolderroot & "\" & strvm
    Do While Not (objmachineFile.AtEndOfStream)
    strComputer = objmachineFile.ReadLine
    objFSO.Copyfolder strfolderName, "\\" & strComputer & "\C$\VMware\"
    strComputer = ""
    machinecount=machinecount+1
    Loop
    objmachineFile.close
    Set objmachineFile = objFSO.OpenTextFile(InputmachineFile)
    vmcount=vmcount+1
    strvm=""
    foldername=""
    Loop
    MsgBox "Done " & machinecount & " " & vmcount
    I’m a complete novice at this so any help would be great  
    Rob
    Thursday, November 03, 2011 8:44 PM

Answers

  • When I do that I get a blank cmd window and it does nothing. The batch is still running but not doing  a thing.

    Rob

    Let's be a little inquisitive. Why don't you change the order of lines so that you see what happens before it happens? Since you now know how to use debugging tools, you should play with them until you know exactly what's going on. We cannot do this for you!

    :Sub
    echo Parameter=%1
    echo Command=robocopy "%Source%" "\\%1\%arget%" /R:3 /W:5
    pause
    robocopy "%Source%" "\\%1\%arget%" /R:3 /W:5
    pause

    Saturday, November 05, 2011 1:54 PM
  • Hi Rob,

    As Pegasus says, you need to do some debugging on your own. The "rem" command is a comment, so if you put a "rem" command at the start of a command, that command won't run! In addition, I think you need to say "%Target%" instead of "%arget%" (it looks like the "T" is missing from the variable name).

    Bill

    Saturday, November 05, 2011 4:34 PM
    Moderator

All replies

  • You're doubly punishing yourself using the above script, because
    a) You're re-inventing the wheel and
    b) Your method is slow.

    Copying files from A to B is a standard admin task for which there is a standard admin tool, robocopy.exe. Here is how it works in principle, using a batch file:

    @echo off
    set Source=C:\VMDEPLOYMENT SCRIPTS
    set Target=\\staffclus\issimages$\VMWare\VM Labs 2011-12\C-Drive
    set List=C:\VMDEPLOYMENT SCRIPTS\machinelist.Txt
    for /F %%a in ("%List%") do robocopy "%Source%" "\\%%a\%arget%" "vmlist.txt"

    You will have to adjust the above script to suit your environment and set a number of switches for robocopy.exe, e.g. /r:1 /w:1.

    If you want to run the copy processes simultaneously then you can do it in much the same way. You will need to watch out for stack overflow!


    @echo off
    set Source=C:\VMDEPLOYMENT SCRIPTS
    set Target=\\staffclus\issimages$\VMWare\VM Labs 2011-12\C-Drive
    set List=C:\VMDEPLOYMENT SCRIPTS\machinelist.Txt
    for /F %%a in ("%List%") do call :Sub %%a
    goto :eof

    :Sub
    start /b "Bulk Copy" robocopy "%Source%" "\\%1\%arget%" "vmlist.txt"

    Thursday, November 03, 2011 9:13 PM
  • HI!

    Thanks for your reply. What I basically have are some VMs in a folder on a server and deploying them out to C:\VMware on various target PCs in labs. So you think this would work much better? Also, would the script call one PC at a time from the machinelist.txt deploying one VM at a time? Or deploy out to all PCs in the list simultaneously? Not really sure if that's even possible

    @echonoff

    set Source=C:\VMDEPLOYMENT SCRIPTS

    set Source="\\staffclus\issimages$\VMWare\VM Labs 2011-12\C-Drive"

    set Target="c:\VMware"

    set List="C:\VMDEPLOYMENT SCRIPTS\machinelist.Txt"

    for /F %%a in ("%List%") do robocopy "%Source%" "\\%%a\%arget%" "vmlist.txt"

     

    Thursday, November 03, 2011 9:39 PM
  • Also, would the script call one PC at a time from the machinelist.txt deploying one VM at a time? Or deploy out to all PCs in the list simultaneously? Not really sure if that's even possible
    Have a closer look at my response.
    Thursday, November 03, 2011 9:47 PM
  • Sorry, think I was half asleep when I was looking at that last night.

    I've been testing this today and changed the script to:

    @echo off
    set Source="\\staffclus\issimages$\VMWare\VM Labs 2011-12\Test" /E
    set Target="C:\VMware"
    set List="C:\VMDEPLOYMENT SCRIPTS\machinelist.Txt"
    for /F %%a in ("%List%") do call :Sub %%a
    goto :eof

    :Sub
    start /b "Bulk Copy" robocopy "%Source%" "\\%1\%arget%" /R:3 /W:5

    Where Source is where my VMs are located on the server

    Target is the target folder on the lab PCs

    List is the txt file containing the list of PCs to copy VMs to.

    When I run this I just get a continous scroll saying "C:\VMDEPLOYMENT SCRIPTS >" in a cmd window that doesn't seem to actualy do anything.

    Thanks for yoru help,

    Rob

     

    Friday, November 04, 2011 11:53 AM
  • In such cases you add some debugging commands so that you can see with your own eyes what's going on, e.g. like so:

    @echo off
    set Source="\\staffclus\issimages$\VMWare\VM Labs 2011-12\Test" /E
    set Target="C:\VMware"
    set List="C:\VMDEPLOYMENT SCRIPTS\machinelist.Txt"
    for /F %%a in ("%List%") do call :Sub %%a
    goto :eof

    :Sub
    rem start /b "Bulk Copy" robocopy "%Source%" "\\%1\%arget%" /R:3 /W:5
    echo Parameter=%1
    echo Command=start /b "Bulk Copy" robocopy "%Source%" "\\%1\%arget%" /R:3 /W:5
    pause

    You must also make sure that robocopy.exe exists somewhere in a %path% folder.

    Friday, November 04, 2011 2:02 PM
  • I have robocopy.exe in the System32 folder - running Win7Ent.

    Running the script with the debugging commands gives me:

    Looks like it's trying to copy to the folder C:\VMDEPLOYMENT although the folder is called VMDEPLOYMENT SCRIPTS. Nothing is actualy copied to anywhere.

    Rob

    Friday, November 04, 2011 4:06 PM
  • Hi,

    In general, I recommend avoiding quotes in environment variables because environment variables can contain spaces without needing quotes. (The exception is when your environment variable is a string that will be used as, or in, a command line.)

    Thus, I would recommend something like the following:

    @echo off
    set Source=\\staffclus\issimages$\VMWare\VM Labs 2011-12\Test
    set Target=C$\VMware
    set List=C:\VMDEPLOYMENT SCRIPTS\machinelist.Txt
    for /F %%a in ('type "%List%"') do call :Sub %%a
    goto :eof
    
    :Sub
    rem start /b "Bulk Copy" robocopy "%Source%" "\\%1\%Target%" /R:3 /W:5 /E
    echo Parameter=%1
    echo Command=start /b "Bulk Copy" robocopy "%Source%" "\\%1\%Target%" /R:3 /W:5 /E
    pause
    

    (Not tested)

    Note that since you are robocopying to a UNC path (\\servername\sharename\directory), you need to set C$\VMware as the target (assuming you haven't disabled admin shares). You can't copy to a path \\servername\C:\VMware as that is not valid UNC syntax.

    HTH,

    Bill

    Friday, November 04, 2011 4:15 PM
    Moderator
  • Bill,

    you probably wanted to write

    for /F "delims=" %%a in ('type "%List%"') do call :Sub %%a
    instead of
    for /F %%a in ('type "%List%"') do call :Sub %%a

    Friday, November 04, 2011 4:36 PM
  • Hi,

    Yes, my code assumes there are not delimiters in the text file (I was assuming there are none, since computer names typically don't contain delimiters).

    Bill

    Friday, November 04, 2011 5:10 PM
    Moderator
  • Thanks guys. I've made the changes and it looks like it should be working. And I get this output:

    However, nothing is actualy copied accross. It's calling the correct lab PC names and pointing to the correct source and target folder and looks like it should have copied the test files over. Should I be bringing in a txt file with the names of the folders within the Test folder like I had to with the VB Script?

    Rob

    Friday, November 04, 2011 10:12 PM
  • Rob,

    I hope you have removed the REM in the following line ...


    rem start /b "Bulk Copy" robocopy "%Source%" "\\%1\%Target%" /R:3 /W:5 /E

     

    Gastone Canali

    http://www.armadillo.it/

     

    Friday, November 04, 2011 10:54 PM
  • However, nothing is actualy copied accross. It's calling the correct lab PC names and pointing to the correct source and target folder and looks like it should have copied the test files over. Should I be bringing in a txt file with the names of the folders within the Test folder like I had to with the VB Script?

    Rob

    If your batch file looks like this:

    :Sub
    rem start /b "Bulk Copy" robocopy "%Source%" "\\%1\%arget%" /R:3 /W:5
    echo Parameter=%1
    echo Command=start /b "Bulk Copy" robocopy "%Source%" "\\%1\%arget%" /R:3 /W:5
    pause

    then perhaps making it look like this will help:

    :Sub
    robocopy "%Source%" "\\%1\%arget%" /R:3 /W:5
    echo Parameter=%1
    echo Command=start /b "Bulk Copy" robocopy "%Source%" "\\%1\%arget%" /R:3 /W:5
    pause

    Friday, November 04, 2011 10:54 PM
  • Rob,

    I hope you have removed the REM in the following line ...


    rem start /b "Bulk Copy" robocopy "%Source%" "\\%1\%Target%" /R:3 /W:5 /E

     

    Gastone Canali

    http://www.armadillo.it/

     

    When I remove the rem it goes into a continuous loop over and over hitting the first PC in the list, but not actually copying anything across.

    Rob

    Saturday, November 05, 2011 12:18 PM
  • When I do that I get a blank cmd window and it does nothing. The batch is still running but not doing  a thing.

     

    Rob

    Saturday, November 05, 2011 12:19 PM
  • When I do that I get a blank cmd window and it does nothing. The batch is still running but not doing  a thing.

    Rob

    Let's be a little inquisitive. Why don't you change the order of lines so that you see what happens before it happens? Since you now know how to use debugging tools, you should play with them until you know exactly what's going on. We cannot do this for you!

    :Sub
    echo Parameter=%1
    echo Command=robocopy "%Source%" "\\%1\%arget%" /R:3 /W:5
    pause
    robocopy "%Source%" "\\%1\%arget%" /R:3 /W:5
    pause

    Saturday, November 05, 2011 1:54 PM
  • Hi Rob,

    As Pegasus says, you need to do some debugging on your own. The "rem" command is a comment, so if you put a "rem" command at the start of a command, that command won't run! In addition, I think you need to say "%Target%" instead of "%arget%" (it looks like the "T" is missing from the variable name).

    Bill

    Saturday, November 05, 2011 4:34 PM
    Moderator
  • Thanks for all your help guys. It's very close, but I'll have a play around with it .

    Cheers,

    Rob

     

    Saturday, November 05, 2011 5:10 PM