Powered by GeSHi

Source code for appcompat.bat

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

  1. @ECHO OFF
  2. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  3.  
  4. SETLOCAL ENABLEDELAYEDEXPANSION
  5.  
  6. IF "%~2"=="" GOTO Syntax
  7.  
  8. :: Check Windows version (abort if not Vista or later)
  9. FOR /F "tokens=*" %%A IN ('VER') DO (
  10. 	FOR %%B IN (%%A) DO (
  11. 		FOR /F "delims=[]." %%C IN ("%%~B") DO (
  12. 			SET Winver=%%C
  13. 		)
  14. 	)
  15. )
  16. IF %WinVer% LSS 6 GOTO Syntax
  17.  
  18. :: Initialize variables with valid options
  19. SET Compatibility.OS=WIN95;WIN98;WIN4SP5;WIN2000;WINXPSP2;WINXPSP3;VISTARTM;VISTASP1;VISTASP2;WIN7RTM;WINSRV03SP1;WINSRV08SP1
  20. SET Compatibility.Options=256COLOR;640X480;DISABLETHEMES;DISABLEDWM;HIGHDPIAWARE;RUNASADMIN
  21. SET Compatibility.Set.Options=
  22. SET Compatibility.Set.Count=0
  23.  
  24. :: Check validity of command line arguments
  25. SET LoopCount=0
  26. FOR %%A IN (%*) DO (
  27. 	SET /A LoopCount += 1
  28. 	IF !LoopCount! EQU 1 (
  29. 		REM First argument is program path
  30. 		IF EXIST "%%~A" (
  31. 			SET ProgPath=%%~fA
  32. 		) ELSE (
  33. 			GOTO Syntax
  34. 		)
  35. 	) ELSE (
  36. 		REM Second and following arguments are the options
  37. 		SET Found=!Compatibility.Set.Count!
  38. 		FOR %%B IN (%Compatibility.OS%;%Compatibility.Options%) DO (
  39. 			IF /I "%%~A"=="%%~B" (
  40. 				SET /A Compatibility.Set.Count += 1
  41. 			)
  42. 		)
  43. 		REM If the argument is not found in the
  44. 		REM list of OSs or Options, it is invalid
  45. 		IF !Found! EQU !Compatibility.Set.Count! GOTO Syntax
  46. 	)
  47. )
  48. :: At least 1 OS/Option must be specified
  49. IF %Compatibility.Set.Count% EQU 0 GOTO Syntax
  50.  
  51. :: Check if (only 1) OS is specified
  52. SET LoopCount=0
  53. SET Compatibility.Set.Count=0
  54. SET Compatibility.Set.Options=
  55. FOR %%A IN (%*) DO (
  56. 	SET /A LoopCount += 1
  57. 	IF !LoopCount! NEQ 1 (
  58. 		FOR %%B IN (%Compatibility.OS%) DO (
  59. 			IF /I "%%~A"=="%%~B" (
  60. 				SET /A Compatibility.Set.Count += 1
  61. 				SET Compatibility.Set.Options=%%~B
  62. 			)
  63. 		)
  64. 	)
  65. )
  66. :: Abort if multiple OSs were specified
  67. IF %Compatibility.Set.Count% GTR 1 GOTO Syntax
  68.  
  69. :: Format specified options (whitespace, no quotes)
  70. SET Compatibility.Set.Count=0
  71. FOR %%A IN (%*) DO (
  72. 	FOR %%B IN (%Compatibility.Options%) DO (
  73. 		IF /I "%%~A"=="%%~B" (
  74. 			SET /A Compatibility.Set.Count += 1
  75. 			SET Compatibility.Set.Options=!Compatibility.Set.Options! %%~B
  76. 		)
  77. 	)
  78. )
  79. :: Remove leading whitespace
  80. IF "%Compatibility.Set.Options:~0,1%"==" " SET Compatibility.Set.Options=%Compatibility.Set.Options:~1%
  81.  
  82. :: If all went well, we can now write the options to the registry
  83. IF Compatibility.Set.Count GTR 0 (
  84. 	REG.EXE Add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /V "%ProgPath%" /T REG_SZ /D "!Compatibility.Set.Options!" /F
  85. )
  86.  
  87. ENDLOCAL
  88. GOTO:EOF
  89.  
  90.  
  91. :Syntax
  92. IF "%OS%"=="Windows_NT" ENDLOCAL
  93.  
  94. :: Set codepage to properly display vertical line characters for DOS
  95. IF NOT "%OS%"=="Windows_NT" CHCP 437
  96.  
  97. ECHO.
  98. ECHO AppCompat.bat,  Version 1.01 for Windows 7
  99. ECHO Add an Application Compatibility Layer for the specified program
  100. ECHO.
  101. IF NOT "%OS%"=="Windows_NT" GOTO NoEscape
  102. ECHO Usage:  APPCOMPAT  program  [ os  ^|  screen  ^|  priv ]
  103. GOTO Next
  104. :NoEscape
  105. ::  is ASCII character 179 (vertical line), used to emulate pipe symbol in DOS
  106. ECHO Usage:  APPCOMPAT  program  [ os    screen    priv ]
  107. :Next
  108. ECHO.
  109. ECHO Where:  program  is the fully qualified or relative path to the program
  110. ECHO         os       is the optional operating system mode: WIN95, WIN98, WIN4SP5,
  111. ECHO                  WIN2000, WINXPSP2, WINXPSP3, VISTARTM, VISTASP1, VISTASP2,
  112. ECHO                  WIN7RTM, WINSRV03SP1 or WINSRV08SP1
  113. ECHO         screen   is the optional restriction(s) for screen settings: 256COLOR,
  114. ECHO                  640X480, DISABLETHEMES, DISABLEDWM and/or HIGHDPIAWARE
  115. ECHO         priv     is the optional privilege level: RUNASADMIN
  116. ECHO.
  117. ECHO Notes:  At least one option must be specified; more allowed, but 1 OS maximum.
  118. ECHO         Option values are for Windows 7; this batch file may (or may not)
  119. ECHO         work in Vista (not tested), but with a limited set of values.
  120. ECHO         The options for Windows 7 are described in more details on:
  121. ECHO         http://www.verboon.info/index.php/2011/03/
  122. ECHO         running-an-application-as-administrator-or-in-compatibility-mode/
  123. ECHO.
  124. ECHO Written by Rob van der Woude
  125. ECHO http://www.robvanderwoude.com
  126.  
  127. :: Errorlevel 1 without the need for EXIT /B
  128. IF "%OS%"=="Windows_NT" COLOR 00
  129.