Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for runbackupwithoutsleep.bat

(view source code of runbackupwithoutsleep.bat as plain text)

  1. @ECHO OFF
  2. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  3. IF "%~1"=="" GOTO Syntax
  4.  
  5. SETLOCAL
  6. CLS
  7.  
  8. :: Check if High Performance plan can be found
  9. POWERCFG /List 2>&1 | FIND /I "High performance" >NUL
  10. IF ERRORLEVEL 1 (
  11. 	POWERCFG /List 2>&1 | FIND /I "8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c" >NUL
  12. 	IF ERRORLEVEL 1 GOTO Syntax
  13. )
  14.  
  15. :: Save a list of power schemes once, before changing anything
  16. IF NOT EXIST "%~dpn0.inital.txt" (
  17. 	ECHO WARNING: Do not delete this file, it is used by %~nx0 to reactivate the original power scheme> "%~dpn0.inital.txt"
  18. 	ECHO.>> "%~dpn0.inital.txt"
  19. 	POWERCFG /List >> "%~dpn0.inital.txt" 2>&1
  20. )
  21.  
  22. :: Find the initial power scheme
  23. FOR /F "tokens=1* delims=:" %%A IN ('TYPE "%~dpn0.inital.txt" ^| FINDSTR /R /E /C:"\*"') DO (
  24. 	FOR /F "tokens=1,2 delims=()" %%C IN ("%%~B") DO (
  25. 		SET PowerCfg.Initial.GUID=%%C
  26. 		SET PowerCfg.Initial.Name=%%D
  27. 	)
  28. )
  29. FOR %%A IN (%PowerCfg.Initial.GUID%) DO SET PowerCfg.Initial.GUID=%%A
  30.  
  31. IF /I "%~1"=="/I" (
  32. 	POWERCFG /SetActive %PowerCfg.Initial.GUID%
  33. 	POWERCFG /List
  34. 	GOTO:EOF
  35. )
  36.  
  37. :: Find the high performace power scheme
  38. FOR /F "tokens=1* delims=:" %%A IN ('TYPE "%~dpn0.inital.txt" ^| FIND /I "High performance"') DO (
  39. 	FOR /F "tokens=1,2 delims=()" %%C IN ("%%~B") DO (
  40. 		SET PowerCfg.HighPerformance.GUID=%%C
  41. 		SET PowerCfg.HighPerformance.Name=%%D
  42. 	)
  43. )
  44. IF "%PowerCfg.HighPerformance.GUID%"=="" (
  45. 	FOR /F "tokens=1* delims=:" %%A IN ('TYPE "%~dpn0.inital.txt" ^| FIND /I "8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c"') DO (
  46. 		FOR /F "tokens=1,2 delims=()" %%C IN ("%%~B") DO (
  47. 			SET PowerCfg.HighPerformance.GUID=%%C
  48. 			SET PowerCfg.HighPerformance.Name=%%D
  49. 		)
  50. 	)
  51. )
  52. FOR %%A IN (%PowerCfg.HighPerformance.GUID%) DO SET PowerCfg.HighPerformance.GUID=%%A
  53.  
  54. :: Find the currently active scheme
  55. FOR /F "tokens=1* delims=:" %%A IN ('POWERCFG /GetActiveScheme') DO (
  56. 	FOR /F "tokens=1,2 delims=()" %%C IN ("%%~B") DO (
  57. 		SET PowerCfg.Current.GUID=%%C
  58. 		SET PowerCfg.Current.Name=%%D
  59. 	)
  60. )
  61. FOR %%A IN (%PowerCfg.Current.GUID%) DO SET PowerCfg.Current.GUID=%%A
  62.  
  63. :: Display the initially and currently active schemes
  64. ECHO.
  65. ECHO Power plan at startup:
  66. ECHO.
  67. FOR /F "skip=1 tokens=*" %%A IN ('TYPE "%~dpn0.inital.txt" ^| FIND "*"') DO ECHO Initial %%A
  68. FOR /F        "tokens=*" %%A IN ('POWERCFG /GetActiveScheme')            DO ECHO Current %%A
  69.  
  70. :: If the current scheme is not the initial one, some other process probably changed, so we'll leave it unchanged
  71. SET DoChange=1
  72. IF /I NOT "%PowerCfg.Current.GUID%"=="%PowerCfg.Initial.GUID%" (
  73. 	SET DoChange=0
  74. 	ECHO As the currently active power scheme is not the initial one, this batch file
  75. 	ECHO will not reactivate the initial power scheme when finished.
  76. 	ECHO Use RUNBACKUPWITHOUTSLEEP /I to manually reactivate the initial power scheme.
  77. )
  78.  
  79. :: Make high performance scheme the active one
  80. IF %DoChange% EQU 1 (
  81. 	ECHO.
  82. 	ECHO Making "%PowerCfg.HighPerformance.Name%" the active power scheme:
  83. 	ECHO.
  84. 	POWERCFG /SetActive %PowerCfg.HighPerformance.GUID%
  85. 	POWERCFG /List | MORE /E +3
  86. )
  87.  
  88. :: Start the backup task specified on the command line
  89. START /B /WAIT "Backup" %*
  90.  
  91. :: Reactivate the initial power scheme
  92. IF %DoChange% EQU 1 (
  93. 	ECHO.
  94. 	ECHO Reactivating "%PowerCfg.Initial.Name%" power scheme:
  95. 	ECHO.
  96. 	POWERCFG /SetActive %PowerCfg.Initial.GUID%
  97. ) ELSE (
  98. 	ECHO The power scheme at this batch file's startup was NOT the initial one.
  99. 	ECHO This batch file will not reactivate the initial power scheme.
  100. 	ECHO Use RUNBACKUPWITHOUTSLEEP /I to manually reactivate the initial power scheme.
  101. 	ECHO.
  102. )
  103. :: Display the initially and currently active schemes
  104. POWERCFG /List | MORE /E +3
  105.  
  106. ENDLOCAL
  107. GOTO:EOF
  108.  
  109.  
  110. :Syntax
  111. ECHO.
  112. ECHO RunBackupWithoutSleep.bat,  Version 1.20 for Windows 7
  113. ECHO Run a backup, defrag or any other long task without the computer going to sleep
  114. ECHO.
  115. ECHO Usage:  RunBackupWithoutSleep  backup_command  [ backup_command_arguments ]
  116. ECHO    or:  RunBackupWithoutSleep  /I
  117. ECHO.
  118. ECHO Notes:  The first time this batch file runs, it creates a list of available
  119. ECHO         Power schemes. This list will be used to restore the initial scheme.
  120. ECHO         If the currently active power scheme is not the initial one, the
  121. ECHO         batch file will not reactivate the initial power scheme, to prevent
  122. ECHO         interference with other running instances of this batch file.
  123. ECHO         In that case, the batch file will display a warning message.
  124. ECHO         The initial power scheme may be manually reactivated by running
  125. ECHO         RUNBACKUPWITHOUTSLEEP /I
  126. ECHO.
  127. ECHO Written by Rob van der Woude
  128. ECHO http://www.robvanderwoude.com
  129.  
  130. IF "%OS%"=="Windows_NT" EXIT /B 1
  131.  

page last uploaded: 2017-07-06, 12:37