We help IT Professionals succeed at work.

VBScript "Do While"

lpbenergy
lpbenergy asked
on
992 Views
Last Modified: 2013-11-05
Ok, so for starters I am VBScripting newbie.  That said, I am trying to accomplish the following;

I am trying to automate some Robocopy jobs and would like to put together a script the will spawn multiple jobs based on the contents of a text file.  The text file will contain a list of names, these names correspond to directory path. For example, the text will contain data looking as such...

Fodler1
Folder2
Folder3
etc...

The script will read the text file, line by line, spawning jobs until it reaches their are no more entries in the text file.

Source and Destination parameters will be passed via variables.  

Here is what I have conceptually in psuedoscript



Option Explicit

Dim objShell, objFSO
Dim strLog strSource, strDestination

strLog = This is the name and path of the logfile.  Each robocopy job that is spawned from this script will have a unique log file.  The log file name should be the 'Folder1.log, Folder2.Log, etc..  corresponding to the name given in the text file.

Set objShell = CreateObject("WScript.Shell")

DO objShell.Run" Robocopy " strSource strDestination strOptions strLog
LOOP, Keep doing this for each entry in the provided text file.

My biggest issue is that I don't know how to get the script to open the text file, read a line and do something with it then go to the next line.  I also need it to pass the logfile name based on the entry in the text file.  

Thanks for your help!

Comment
Watch Question

Author

Commented:
Oops, left out some information...so just for clarity.

strSource = \\ServerA\Path\"Folder 1"
strDestination = \\ServerB\Path\"Folder 1"
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Actually yes, they do need to execute in parallel.  Normally I would agree that this would not be a good idea.  In my case I have found that is is necessary and actually does not significantly impact performance.
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
If you really want to start multiple robocopy you have to change line 11 to
objShell.Run "start /min Robocopy "& strSource & " " & strDestination & " " & strOptions & " /log:" & strLog

Author

Commented:
Thanks for your help.  Here is the final script.  Works like a champ.


Option Explicit
Dim objShell, objFSO
Dim strOptions, strLog, strSource, strDestination, strFolder
Dim txtFile

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set txtFile = objFSO.OpenTextFile("Robocopy_File.txt", 1)

do until txtFile.AtEndOfStream
  strFolder      = txtFile.ReadLine
  strSource      = "\\Server1\Share1" & "\" & strFolder
  strDestination = "\\Server2\Share2" & "\" & strFolder
  strOptions       = " /XF *.tif /S /maxage:14 /R:5 /w:2 /NFL /NDL /TEE "
  strLog         = strFolder & ".log"
objShell.Run "Robocopy "& strSource & " " & strDestination & " " & strOptions & " /log:" & strLog
loop
txtFile.Close
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.