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. IF %Ver% LEQ 5.1 GOTO Syntax
  19.  
  20. :: Find file type for specified extension
  21. SET Error=No file type is registered for %~x1
  22. SET FileType=
  23. FOR /F "tokens=2*" %%A IN ('REG Query "HKCR\%~x1" /ve 2^>NUL') DO SET FileType=%%B
  24. IF NOT DEFINED FileType GOTO Syntax
  25. :: Remove leading "REG_SZ" if name of default value contains spaces
  26. ECHO.%FileType% | FINDSTR /R /C:"REG.*_SZ" >NUL && FOR /F "tokens=1*" %%A IN ("%FileType%") DO SET FileType=%%B
  27. ECHO.%FileType% | FINDSTR /R /C:"REG.*_SZ" >NUL && FOR /F "tokens=1*" %%A IN ("%FileType%") DO SET FileType=%%B
  28.  
  29. :: Find edit action for that file type
  30. SET Error=No edit action is registered for file type %FileType%
  31. SET Edit=
  32. FOR /F "tokens=*" %%A IN ('REG Query "HKCR\%FileType%\shell" 2^>NUL ^| FIND /I "HKEY_CLASSES_ROOT\%FileType%\shell\edit"') DO SET Edit=%%A
  33. IF NOT DEFINED Edit GOTO Syntax
  34.  
  35. :: Find command for that edit action
  36. SET Error=No command is registered for %Edit% action on %FileType%
  37. SET Command=
  38. FOR /F "tokens=2*" %%A IN ('REG Query "%Edit%\command" /ve 2^>NUL ^| FINDSTR /R /C:"REG.*_SZ"') DO SET Command=%%B
  39. IF NOT DEFINED Command GOTO Syntax
  40.  
  41. :: Remove leading "REG_SZ" if name of default value contains spaces
  42. :: Note: GOTO tries to evade using FOR loops with parenthesis in the command,
  43. :: e.g. "C:\Program Files (x86)\...", which might crash the FOR loop...
  44. ECHO.%Command% | FINDSTR /R /C:"REG.*_SZ" >NUL || GOTO SkipStrip
  45. FOR /F "tokens=1*" %%A IN ("%Command%") DO SET Command=%%B
  46. ECHO.%Command% | FINDSTR /R /C:"REG.*_SZ" >NUL || GOTO SkipStrip
  47. FOR /F "tokens=1*" %%A IN ("%Command%") DO SET Command=%%B
  48. :SkipStrip
  49.  
  50. :: Display and run that command
  51. CALL ECHO %Command%
  52. CALL START "Edit %~nx1" %Command%
  53.  
  54. ENDLOCAL
  55. GOTO:EOF
  56.  
  57.  
  58. :Syntax
  59. IF NOT "%Error%"=="" ECHO.
  60. IF NOT "%Error%"=="" ECHO Error: %Error%
  61. SET Error=
  62.  
  63. ECHO.
  64. ECHO Edit.bat,  Version 1.10 for Windows XP and later
  65. ECHO Open the specified file in the editor registered for its file type
  66. ECHO.
  67. ECHO Usage:  EDIT  filename
  68. ECHO.
  69. ECHO Written by Rob van der Woude
  70. ECHO http://www.robvanderwoude.com
  71.  
  72. IF "%OS%"=="Windows_NT" ENDLOCAL
  73.  

page last uploaded: 2017-04-06, 13:33