Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for defopen.bat

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

  1. @ECHO OFF
  2. :: No parameters required
  3. IF NOT [%1]==[] GOTO Syntax
  4.  
  5. :: Choose the correct command processor for the current operating system
  6. SET _cmd=
  7. :: Variable to add shortcut to menu entry (NT only,
  8. :: since COMMAND.COM cannot echo an ampersand)
  9. SET _=
  10. ECHO.%COMSPEC% | FIND /I "command.com" >NUL
  11. IF NOT ERRORLEVEL 1 SET _cmd=command.com /e:4096
  12. ECHO.%COMSPEC% | FIND /I "cmd.exe" >NUL
  13. IF NOT ERRORLEVEL 1 SET _cmd=cmd.exe
  14. IF [%_cmd%]==[cmd.exe] SET _=^&
  15.  
  16. :: Create a temporary .REG file
  17. > %TEMP%.\DEFOPEN.REG ECHO REGEDIT4
  18. >>%TEMP%.\DEFOPEN.REG ECHO.
  19.  
  20. :: Open With Notepad doesn't work in XP
  21. SET Skip=0
  22. VER | FIND /I "XP" >NUL
  23. IF NOT ERRORLEVEL 1 SET Skip=1
  24. VER | FIND /I "Server 2003" >NUL
  25. IF NOT ERRORLEVEL 1 SET Skip=1
  26. IF "%Skip%"=="1" ECHO Skipping "Open with Notepad" entry
  27. IF "%Skip%"=="1" GOTO Print
  28. ECHO Adding "Open with Notepad" entry
  29. >>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\*\shell\open]
  30. >>%TEMP%.\DEFOPEN.REG ECHO @="%_%Open with Notepad"
  31. >>%TEMP%.\DEFOPEN.REG ECHO.
  32. >>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\*\shell\open\command]
  33. >>%TEMP%.\DEFOPEN.REG ECHO @="notepad.exe \"%%L\""
  34. >>%TEMP%.\DEFOPEN.REG ECHO.
  35.  
  36. :Print
  37. ECHO Adding "Print with Notepad" entry
  38. >>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\*\shell\print]
  39. >>%TEMP%.\DEFOPEN.REG ECHO @="%_%Print with Notepad"
  40. >>%TEMP%.\DEFOPEN.REG ECHO.
  41. >>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\*\shell\print\command]
  42. >>%TEMP%.\DEFOPEN.REG ECHO @="notepad.exe /p \"%%L\""
  43. >>%TEMP%.\DEFOPEN.REG ECHO.
  44.  
  45. :: If neither COMMAND.COM nor CMD.EXE then skip this step
  46. IF [%_cmd%]==[] ECHO Skipping "Command Prompt Here" entry
  47. IF [%_cmd%]==[] GOTO Merge
  48.  
  49. ECHO Adding "Command Prompt Here" entry
  50. SET Pushd=cd
  51. IF "%Skip%"=="1" SET Pushd=pushd
  52. VER | FIND /I "Windows 2000" >NUL
  53. IF NOT ERRORLEVEL 1 SET Pushd=pushd
  54.  
  55. :: Add Command Prompt Here for files
  56. >>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\*\shell\prompt]
  57. >>%TEMP%.\DEFOPEN.REG ECHO @="Command Prompt Here"
  58. >>%TEMP%.\DEFOPEN.REG ECHO.
  59. >>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\*\shell\prompt\command]
  60. >>%TEMP%.\DEFOPEN.REG ECHO @="%_cmd% /k %Pushd% \"%%L\\..\""
  61. >>%TEMP%.\DEFOPEN.REG ECHO.
  62. :: Add Command Prompt Here for directories
  63. >>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\Directory\shell\prompt]
  64. >>%TEMP%.\DEFOPEN.REG ECHO @="Command Prompt Here"
  65. >>%TEMP%.\DEFOPEN.REG ECHO.
  66. >>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\Directory\shell\prompt\command]
  67. >>%TEMP%.\DEFOPEN.REG ECHO @="%_cmd% /k %Pushd% \"%%L\""
  68. >>%TEMP%.\DEFOPEN.REG ECHO.
  69. :: Add Command Prompt Here for drives
  70. >>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\Drive\shell\prompt]
  71. >>%TEMP%.\DEFOPEN.REG ECHO @="Command Prompt Here"
  72. >>%TEMP%.\DEFOPEN.REG ECHO.
  73. >>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\Drive\shell\prompt\command]
  74. >>%TEMP%.\DEFOPEN.REG ECHO @="%_cmd% /k %Pushd% \"%%L\""
  75. >>%TEMP%.\DEFOPEN.REG ECHO.
  76.  
  77. :: Merge the temporary .REG file
  78. :Merge
  79. START /WAIT REGEDIT /S %TEMP%.\DEFOPEN.REG
  80.  
  81. :: Delete the temporary .REG file
  82. DEL %TEMP%.\DEFOPEN.REG
  83.  
  84. :: Ready
  85. GOTO End
  86.  
  87. :Syntax
  88. ECHO.
  89. ECHO DefOpen.bat,  Version 3.00 for Windows 95 .. Windows Server 2003
  90. ECHO Adds a default file association: double-clicking a file without a file
  91. ECHO association will open the file in Notepad.
  92. ECHO Also adds three new entries to Explorer's context menu: "Open with Notepad",
  93. ECHO "Print with Notepad" and "Command Prompt Here".
  94. ECHO.
  95. ECHO Usage:  DEFOPEN
  96. ECHO.
  97. ECHO Notes:  In Windows 2000 and later, "Command Prompt Here" will also work for
  98. ECHO         UNC paths by using Reinhard Irnberger's PUSHD trick; the price to pay
  99. ECHO         for this functionality is that you need to close the window with the
  100. ECHO         POPD command, otherwise a network mapping will persist.
  101. ECHO         "Open With Notepad" will not be added in XP or later, as it won't work.
  102. ECHO         Notepad registry tip courtesy of Regedit.com (http://www.regedit.com)
  103. ECHO.
  104. ECHO Written by Rob van der Woude
  105. ECHO http://www.robvanderwoude.com
  106.  
  107. :: Clean up variables and quit
  108. :End
  109. SET _cmd=
  110. SET _=
  111.  

page last modified: 2024-02-26; loaded in 0.0293 seconds