Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.6 KiB

  1. @echo off
  2. REM -----------------------------------------------------------------------
  3. REM SXCOPY.CMD
  4. REM
  5. REM Purpose: Execute a safe and informative xcopy.
  6. REM - verify the existence of the source
  7. REM - write the operation performed in a logfile
  8. REM - write the execution times in a logfile
  9. REM - if xcopy fails, loop MAX times until success
  10. REM
  11. REM Parameters:
  12. REM - switches of xcopy
  13. REM - source of xcopy
  14. REM - destination of xcopy
  15. REM -----------------------------------------------------------------------
  16. if NOT "%_echo%" == "" echo on
  17. if NOT "%verbose%" == "" echo on
  18. setlocal ENABLEEXTENSIONS
  19. if "%3"=="" goto Usage
  20. REM Caller can specify target to be a filename explicitly to avoid being
  21. REM prompted with the target is a file or destination
  22. set filetarget=
  23. if /i "%4"=="file" set filetarget=1
  24. REM Verify source
  25. if exist %2 goto Start_Loop
  26. echo ERROR: xcopy source %2 does not exist.
  27. goto End
  28. :Start_Loop
  29. REM Try xcopy MAX times (10 minutes) or until success.
  30. set /A MAX=100
  31. set /A count=1
  32. :Loop
  33. REM ------------------------------------------------
  34. REM Attempts to execute xcopy:
  35. REM ------------------------------------------------
  36. if "%count%" == "%MAX%" goto XCopy_Fail
  37. if defined filetarget (
  38. echo f | xcopy %1 %2 %3
  39. ) else (
  40. xcopy %1 %2 %3
  41. )
  42. if errorlevel 1 (
  43. if defined filetarget (
  44. echo ERROR: echo f ^| xcopy %1 %2 %3 failed. Try again...
  45. ) else (
  46. echo ERROR: xcopy %1 %2 %3 failed. Try again...
  47. )
  48. set /A count=%count%+1
  49. sleep 6
  50. goto Loop
  51. )
  52. :End_Loop
  53. set mes=
  54. if not "%count%"=="1" set mes=succedded after %count% attempts
  55. if defined filetarget (
  56. echotime DONE /bH:MbObD --- echo f ^| xcopy /%1 %2 %3 %mes%
  57. ) else (
  58. echotime DONE /bH:MbObD --- xcopy /%1 %2 %3 %mes%
  59. )
  60. goto End
  61. :XCopy_Fail
  62. if defined filetarget (
  63. echo ERROR: echo f ^| xcopy %1 %2 %3 failed after %MAX% attempts.
  64. echo echo f ^| xcopy %1 %2 %3 failed after %MAX% attempts.
  65. ) else (
  66. echo ERROR: xcopy %1 %2 %3 failed after %MAX% attempts.
  67. echo xcopy %1 %2 %3 failed after %MAX% attempts.
  68. )
  69. goto End
  70. REM Display Usage:
  71. :Usage
  72. echo.
  73. echo Safe xcopy wrapper that logs and retries on error
  74. echo.
  75. echo Usage: %0 ^<xcopy switches^> ^<xcopy source^> ^<xcopy destination^> [file]
  76. echo ex: %0 /rf foo %%tmp%%\
  77. echo ex: %0 /v foo %%tmp%%\foo.orig file
  78. echo.
  79. echo file Key word to denote that ^<xcopy destination^> is a filename
  80. echo as opposed to a directory name.
  81. echo Prepends "echo f |" to the xcopy command
  82. echo.
  83. goto End
  84. :End
  85. endlocal