Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for edit.bat

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

  1. @ECHO OFF
  2. :: Check Windows version
  3. SET Error=This batch file requires Windows XP or later
  4. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  5. SET Error=
  6.  
  7. SETLOCAL
  8.  
  9. :: Check command line arguments
  10. IF     "%~1"=="" GOTO Syntax
  11. IF NOT "%~2"=="" GOTO Syntax
  12. SET Error=File not found
  13. IF NOT EXIST "%~1" GOTO Syntax
  14.  
  15. :: Check Windows version
  16. SET Error=This batch file requires Windows XP or later
  17. FOR /F "tokens=*" %%A IN ('VER') DO FOR %%B IN (%%~A) DO FOR /F "delims=]" %%C IN ("%%~B") DO SET Ver=%%C
  18. FOR /F "delims=." %%A IN ("%Ver%") DO (
  19. 	IF %%A EQU 5 IF %%B EQU 0 (
  20. 		GOTO Syntax
  21. 	)
  22. 	IF %%A LSS 5 (
  23. 		GOTO Syntax
  24. 	)
  25. )
  26.  
  27. :: Find file type for specified extension
  28. SET Error=No file type is registered for %~x1
  29. SET FileType=
  30. FOR /F "tokens=2*" %%A IN ('REG Query "HKCR\%~x1" /ve 2^>NUL') DO SET FileType=%%B
  31. IF NOT DEFINED FileType GOTO Syntax
  32. :: Remove leading "REG_SZ" if name of default value contains spaces
  33. ECHO.%FileType% | FINDSTR /R /C:"REG.*_SZ" >NUL && FOR /F "tokens=1*" %%A IN ("%FileType%") DO SET FileType=%%B
  34. ECHO.%FileType% | FINDSTR /R /C:"REG.*_SZ" >NUL && FOR /F "tokens=1*" %%A IN ("%FileType%") DO SET FileType=%%B
  35.  
  36. :: Find edit action for that file type
  37. SET Error=No edit action is registered for file type %FileType%
  38. SET Edit=
  39. FOR /F "tokens=*" %%A IN ('REG Query "HKCR\%FileType%\shell" 2^>NUL ^| FIND /I "HKEY_CLASSES_ROOT\%FileType%\shell\edit"') DO SET Edit=%%A
  40. IF NOT DEFINED Edit GOTO Syntax
  41.  
  42. :: Find command for that edit action
  43. SET Error=No command is registered for %Edit% action on %FileType%
  44. SET Command=
  45. FOR /F "tokens=2*" %%A IN ('REG Query "%Edit%\command" /ve 2^>NUL ^| FINDSTR /R /C:"REG.*_SZ"') DO SET Command=%%B
  46. IF NOT DEFINED Command GOTO Syntax
  47.  
  48. :: Remove leading "REG_SZ" if name of default value contains spaces
  49. :: Note: GOTO tries to evade using FOR loops with parenthesis in the command,
  50. :: e.g. "C:\Program Files (x86)\...", which might crash the FOR loop...
  51. ECHO.%Command% | FINDSTR /R /C:"REG.*_SZ" >NUL || GOTO SkipStrip
  52. FOR /F "tokens=1*" %%A IN ("%Command%") DO SET Command=%%B
  53. ECHO.%Command% | FINDSTR /R /C:"REG.*_SZ" >NUL || GOTO SkipStrip
  54. FOR /F "tokens=1*" %%A IN ("%Command%") DO SET Command=%%B
  55. :SkipStrip
  56.  
  57. :: Display and run that command
  58. CALL ECHO %Command%
  59. CALL START "Edit %~nx1" %Command%
  60.  
  61. ENDLOCAL
  62. GOTO:EOF
  63.  
  64.  
  65. :Syntax
  66. IF NOT "%Error%"=="" ECHO.
  67. IF NOT "%Error%"=="" ECHO Error: %Error%
  68. SET Error=
  69.  
  70. ECHO.
  71. ECHO Edit.bat,  Version 1.11 for Windows XP and later
  72. ECHO Open the specified file in the editor registered for its file type
  73. ECHO.
  74. ECHO Usage:  EDIT  filename
  75. ECHO.
  76. ECHO Written by Rob van der Woude
  77. ECHO http://www.robvanderwoude.com
  78.  
  79. IF "%OS%"=="Windows_NT" ENDLOCAL
  80.  

page last modified: 2024-04-16; loaded in 0.0058 seconds