daily pastebin goal
19%
SHARE
TWEET

Robocopy script

a guest Sep 18th, 2012 18 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. : This script does a basic robocopy, but also it does the following:  
  2. : - test connectivity to the machine (ping). Send WOL Magic Packet if it doesn't respond.  
  3. : - after WOL, wait until the machine appears on the network  
  4. : - regardless of whether or not we got the machine up using WOL, we still verify a level of ping response consistency before continuing  
  5. : - verify the network path is visible before continuing  
  6. : - prompt at the start whether you want to shutdown the machine when finished  
  7. : - no further prompts during the process  
  8.  
  9. @ECHO OFF  
  10.  
  11. : SET THE FOLLOWING VARIABLES  
  12.  
  13. : Set robocopy destination into two variables. They are used individually to test CIFS and PING connectivity then combined to insert into robocopy command  
  14. : We'll strip quotes from the outsides of these, so feel free to use quotes around each varilable - or not.  
  15. Set remotemachine=192.168.1.137  
  16. Set copysource=D:\Mijn afbeeldingen\  
  17. Set copytoshare=BACKUP\%COMPUTERNAME%\Mijn afbeeldingen
  18.  
  19. : Time to wait after sending wol packet, before bothering to try to do anything else (approx startup time of remote machine)  
  20. Set timetowol=30  
  21.  
  22. : If, after sending wol and waiting, there's still no response, we'll wait 1 second and try again.  
  23. : This is the total number of tries. TBH, may as well set this really high and Ctrl-C if you get bored.  
  24. Set pingfaillimit=25  
  25.  
  26. : What do you consider is a good number of ping receipts to get back before deeming your connection to remote machine is stable? 1 = impatient. 10000 = paranoid. 10 = normal.  
  27. Set stabilitysatisfaction=6  
  28.  
  29. : Once stability, by the definition of how many pings specified, is attained, we check the copy-to network path is available  
  30. : Frankly, if it isn't, it probably won't become available. And you'll have to figure out the problem separately.  
  31. : But this gives us the option to keep trying x number of times before continuing.  
  32. : Note: this number doesn't correspond to an amount of time. Windows is unpredictable when trying to check fileshares.  
  33. Set filesharefaillimit=15  
  34.  
  35. : NO MORE VARIABLES TO SET NOW  
  36.  
  37. Set consecutivepingcheckcount=1  
  38. Set consecutivepingfailcount=1  
  39. Set filesharetestcount=1  
  40.  
  41. :pingandcheck  
  42.  
  43. ping /n 2 %remotemachine% | find "TTL=" >nul  
  44. if %errorlevel% == 0 goto reply  
  45.  
  46. @echo No Reply on that IP! Tried %consecutivepingfailcount% of %pingfaillimit% times  
  47.  
  48. Set consecutivepingcheckcount=1  
  49. Set /A consecutivepingfailcount+=1  
  50.  
  51. IF %consecutivepingfailcount% == %pingfaillimit% (  
  52. @echo We didn't get very far did we?  
  53. @echo I waited, but nothing!  
  54. @echo Increase the pingfaillimit variable?  
  55. GOTO fin  
  56. )  
  57. goto pingandcheck  
  58.  
  59. :reply  
  60. @echo IP Replied! Checking connection stability... %consecutivepingcheckcount% of %stabilitysatisfaction%  
  61. Set /A consecutivepingcheckcount+=1  
  62. IF %consecutivepingcheckcount% == %stabilitysatisfaction% (  
  63. @echo Connection appears stable!  
  64. GOTO checkfileshare  
  65. )  
  66. GOTO pingandcheck  
  67.  
  68. :checkfileshare  
  69. @echo Now checking fileshare  
  70. IF EXIST \\%remotemachine%\%copytoshare% (  
  71. @echo Fileshare is visible. Good to go. Starting copying.  
  72. GOTO docopy  
  73. )  
  74. @echo Couldn't find fileshare - tried %filesharetestcount% of %filesharefaillimit% times.  
  75. Set /A filesharetestcount+=1  
  76. ping 127.0.0.1 -n 2 >null  
  77. IF %filesharetestcount% == %filesharefaillimit% (  
  78. @echo Failed to find the fileshare. Oh no!  
  79. @echo Maybe verify the fileshare is accessible yourself?  
  80. GOTO fin  
  81. )  
  82. goto checkfileshare  
  83.  
  84. :docopy  
  85. :first three lines strip quotes if found then combine the machine name and share to give path  
  86. for /f "useback tokens=*" %%a in ('%remotemachine%') do set remotemachine=%%~a  
  87. for /f "useback tokens=*" %%a in ('%copytoshare%') do set copytoshare=%%~a  
  88. Set destination=\\%remotemachine%\%copytoshare%  
  89. ECHO on  
  90. robocopy "%copysource%" "%destination%" /MIR /ETA /XJ /R:10 /W:10  
  91.  
  92. :fin
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top