[关闭]
@cyysu 2017-10-12T10:21:37.000000Z 字数 11945 阅读 803

Bat-实战基础篇(九)

  • 时间:2017年10月11日
  • 作者:Kali
  • 邮箱:cyysu.github.io@gmail.com/2869905223@qq.com/微信lwyx1413
  • 版本:4.0
  • 描述:批处理实战基础篇九,本次主要给出几个栗子。

Bat系列教程


MPV播放器

  1. @echo off
  2. :: 启动命令拓展
  3. setlocal enableextensions enabledelayedexpansion
  4. :: 为执行文件设置一个搜索路径
  5. path %SystemRoot%\System32;%SystemRoot%;%SystemRoot%\System32\Wbem
  6. :: Unattended install flag. When set, the script will not require user input.
  7. :: 设置是否自动运行
  8. set unattended=no
  9. if "%1"=="/u" set unattended=yes
  10. :: Make sure this is Windows Vista or later
  11. :: 检查是否为 vista 系统
  12. call :ensure_vista
  13. :: Make sure the script is running as admin
  14. call :ensure_admin
  15. :: Command line arguments to use when launching mpv from a file association
  16. set mpv_args=
  17. :: Get mpv.exe location
  18. :: %~dp0表示批处理所在路径
  19. set mpv_path=%~dp0mpv.exe
  20. if not exist "%mpv_path%" call :die "mpv.exe not found"
  21. :: Get mpv-document.ico location
  22. set icon_path=%~dp0mpv-document.ico
  23. if not exist "%icon_path%" call :die "mpv-document.ico not found"
  24. :: Register mpv.exe under the "App Paths" key, so it can be found by
  25. :: ShellExecute, the run command, the start menu, etc.
  26. set app_paths_key=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\mpv.exe
  27. :: 调用reg标签 并且传入后面的参数
  28. call :reg add "%app_paths_key%" /d "%mpv_path%" /f
  29. call :reg add "%app_paths_key%" /v "UseUrl" /t REG_DWORD /d 1 /f
  30. :: Register mpv.exe under the "Applications" key to add some default verbs for
  31. :: when mpv is used from the "Open with" menu
  32. set classes_root_key=HKLM\SOFTWARE\Classes
  33. set app_key=%classes_root_key%\Applications\mpv.exe
  34. :: -v 参数表示添加到指定子项下的项名称 -d 表示值 -f 表示不用询问信息
  35. call :reg add "%app_key%" /v "FriendlyAppName" /d "mpv" /f
  36. call :add_verbs "%app_key%"
  37. :: 运行reg命令 表示和注册表有关
  38. :reg
  39. :: Wrap the reg command to check for errors
  40. >nul reg %*
  41. if errorlevel 1 set error=yes
  42. if [%error%] == [yes] echo Error in command: reg %*
  43. if [%error%] == [yes] call :die
  44. goto :EOF
  45. :: Add mpv to the "Open with" list for all video and audio file types
  46. call :reg add "%classes_root_key%\SystemFileAssociations\video\OpenWithList\mpv.exe" /d "" /f
  47. call :reg add "%classes_root_key%\SystemFileAssociations\audio\OpenWithList\mpv.exe" /d "" /f
  48. :: Add DVD AutoPlay handler
  49. set autoplay_key=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers
  50. call :reg add "%classes_root_key%\io.mpv.dvd\shell\play" /d "&Play" /f
  51. call :reg add "%classes_root_key%\io.mpv.dvd\shell\play\command" /d "\"%mpv_path%\" %mpv_args% dvd:// --dvd-device=\"%%%%L" /f
  52. call :reg add "%autoplay_key%\Handlers\MpvPlayDVDMovieOnArrival" /v "Action" /d "Play DVD movie" /f
  53. call :reg add "%autoplay_key%\Handlers\MpvPlayDVDMovieOnArrival" /v "DefaultIcon" /d "%mpv_path%,0" /f
  54. call :reg add "%autoplay_key%\Handlers\MpvPlayDVDMovieOnArrival" /v "InvokeProgID" /d "io.mpv.dvd" /f
  55. call :reg add "%autoplay_key%\Handlers\MpvPlayDVDMovieOnArrival" /v "InvokeVerb" /d "play" /f
  56. call :reg add "%autoplay_key%\Handlers\MpvPlayDVDMovieOnArrival" /v "Provider" /d "mpv" /f
  57. call :reg add "%autoplay_key%\EventHandlers\PlayDVDMovieOnArrival" /v "MpvPlayDVDMovieOnArrival" /f
  58. :: Add Blu-ray AutoPlay handler
  59. call :reg add "%classes_root_key%\io.mpv.bluray\shell\play" /d "&Play" /f
  60. call :reg add "%classes_root_key%\io.mpv.bluray\shell\play\command" /d "\"%mpv_path%\" %mpv_args% bd:// --bluray-device=\"%%%%L" /f
  61. call :reg add "%autoplay_key%\Handlers\MpvPlayBluRayOnArrival" /v "Action" /d "Play Blu-ray movie" /f
  62. call :reg add "%autoplay_key%\Handlers\MpvPlayBluRayOnArrival" /v "DefaultIcon" /d "%mpv_path%,0" /f
  63. call :reg add "%autoplay_key%\Handlers\MpvPlayBluRayOnArrival" /v "InvokeProgID" /d "io.mpv.bluray" /f
  64. call :reg add "%autoplay_key%\Handlers\MpvPlayBluRayOnArrival" /v "InvokeVerb" /d "play" /f
  65. call :reg add "%autoplay_key%\Handlers\MpvPlayBluRayOnArrival" /v "Provider" /d "mpv" /f
  66. call :reg add "%autoplay_key%\EventHandlers\PlayBluRayOnArrival" /v "MpvPlayBluRayOnArrival" /f
  67. :: Add a capabilities key for mpv, which is registered later on for use in the
  68. :: "Default Programs" control panel
  69. set capabilities_key=HKLM\SOFTWARE\Clients\Media\mpv\Capabilities
  70. call :reg add "%capabilities_key%" /v "ApplicationName" /d "mpv" /f
  71. call :reg add "%capabilities_key%" /v "ApplicationDescription" /d "mpv media player" /f
  72. :: Add file types
  73. set supported_types_key=%app_key%\SupportedTypes
  74. set file_associations_key=%capabilities_key%\FileAssociations
  75. :: DVD/Blu-ray audio formats
  76. call :add_type "audio/ac3" "audio" "AC-3 Audio" ".ac3" ".a52"
  77. call :add_type "audio/eac3" "audio" "E-AC-3 Audio" ".eac3"
  78. call :add_type "audio/vnd.dolby.mlp" "audio" "MLP Audio" ".mlp"
  79. call :add_type "audio/vnd.dts" "audio" "DTS Audio" ".dts"
  80. call :add_type "audio/vnd.dts.hd" "audio" "DTS-HD Audio" ".dts-hd" ".dtshd"
  81. call :add_type "" "audio" "TrueHD Audio" ".true-hd" ".thd" ".truehd" ".thd+ac3"
  82. call :add_type "" "audio" "True Audio" ".tta"
  83. :: Uncompressed formats
  84. call :add_type "" "audio" "PCM Audio" ".pcm"
  85. call :add_type "audio/wav" "audio" "Wave Audio" ".wav"
  86. call :add_type "audio/aiff" "audio" "AIFF Audio" ".aiff" ".aif" ".aifc"
  87. call :add_type "audio/amr" "audio" "AMR Audio" ".amr"
  88. call :add_type "audio/amr-wb" "audio" "AMR-WB Audio" ".awb"
  89. call :add_type "audio/basic" "audio" "AU Audio" ".au" ".snd"
  90. call :add_type "" "audio" "Linear PCM Audio" ".lpcm"
  91. call :add_type "" "video" "Raw YUV Video" ".yuv"
  92. call :add_type "" "video" "YUV4MPEG2 Video" ".y4m"
  93. :: Free lossless formats
  94. call :add_type "audio/x-ape" "audio" "Monkey's Audio" ".ape"
  95. call :add_type "audio/x-wavpack" "audio" "WavPack Audio" ".wv"
  96. call :add_type "audio/x-shorten" "audio" "Shorten Audio" ".shn"
  97. :: MPEG formats
  98. call :add_type "video/vnd.dlna.mpeg-tts" "video" "MPEG-2 Transport Stream" ".m2ts" ".m2t" ".mts" ".mtv" ".ts" ".tsv" ".tsa" ".tts" ".trp"
  99. call :add_type "audio/vnd.dlna.adts" "audio" "ADTS Audio" ".adts" ".adt"
  100. call :add_type "audio/mpeg" "audio" "MPEG Audio" ".mpa" ".m1a" ".m2a" ".mp1" ".mp2"
  101. call :add_type "audio/mpeg" "audio" "MP3 Audio" ".mp3"
  102. call :add_type "video/mpeg" "video" "MPEG Video" ".mpeg" ".mpg" ".mpe" ".mpeg2" ".m1v" ".m2v" ".mp2v" ".mpv" ".mpv2" ".mod" ".tod"
  103. call :add_type "video/dvd" "video" "Video Object" ".vob" ".vro"
  104. call :add_type "" "video" "Enhanced VOB" ".evob" ".evo"
  105. call :add_type "video/mp4" "video" "MPEG-4 Video" ".mpeg4" ".m4v" ".mp4" ".mp4v" ".mpg4"
  106. call :add_type "audio/mp4" "audio" "MPEG-4 Audio" ".m4a"
  107. call :add_type "audio/aac" "audio" "Raw AAC Audio" ".aac"
  108. call :add_type "" "video" "Raw H.264/AVC Video" ".h264" ".avc" ".x264" ".264"
  109. call :add_type "" "video" "Raw H.265/HEVC Video" ".hevc" ".h265" ".x265" ".265"
  110. :: Xiph formats
  111. call :add_type "audio/flac" "audio" "FLAC Audio" ".flac"
  112. call :add_type "audio/ogg" "audio" "Ogg Audio" ".oga" ".ogg"
  113. call :add_type "audio/ogg" "audio" "Opus Audio" ".opus"
  114. call :add_type "audio/ogg" "audio" "Speex Audio" ".spx"
  115. call :add_type "video/ogg" "video" "Ogg Video" ".ogv" ".ogm"
  116. call :add_type "application/ogg" "video" "Ogg Video" ".ogx"
  117. :: Matroska formats
  118. call :add_type "video/x-matroska" "video" "Matroska Video" ".mkv"
  119. call :add_type "video/x-matroska" "video" "Matroska 3D Video" ".mk3d"
  120. call :add_type "audio/x-matroska" "audio" "Matroska Audio" ".mka"
  121. call :add_type "video/webm" "video" "WebM Video" ".webm"
  122. call :add_type "audio/webm" "audio" "WebM Audio" ".weba"
  123. :: Misc formats
  124. call :add_type "video/avi" "video" "Video Clip" ".avi" ".vfw"
  125. call :add_type "" "video" "DivX Video" ".divx"
  126. call :add_type "" "video" "3ivx Video" ".3iv"
  127. call :add_type "" "video" "XVID Video" ".xvid"
  128. call :add_type "" "video" "NUT Video" ".nut"
  129. call :add_type "video/flc" "video" "FLIC Video" ".flic" ".fli" ".flc"
  130. call :add_type "" "video" "Nullsoft Streaming Video" ".nsv"
  131. call :add_type "application/gxf" "video" "General Exchange Format" ".gxf"
  132. call :add_type "application/mxf" "video" "Material Exchange Format" ".mxf"
  133. :: Windows Media formats
  134. call :add_type "audio/x-ms-wma" "audio" "Windows Media Audio" ".wma"
  135. call :add_type "video/x-ms-wm" "video" "Windows Media Video" ".wm"
  136. call :add_type "video/x-ms-wmv" "video" "Windows Media Video" ".wmv"
  137. call :add_type "video/x-ms-asf" "video" "Windows Media Video" ".asf"
  138. call :add_type "" "video" "Microsoft Recorded TV Show" ".dvr-ms" ".dvr"
  139. call :add_type "" "video" "Windows Recorded TV Show" ".wtv"
  140. :: DV formats
  141. call :add_type "" "video" "DV Video" ".dv" ".hdv"
  142. :: Flash Video formats
  143. call :add_type "video/x-flv" "video" "Flash Video" ".flv"
  144. call :add_type "video/mp4" "video" "Flash Video" ".f4v"
  145. call :add_type "audio/mp4" "audio" "Flash Audio" ".f4a"
  146. :: QuickTime formats
  147. call :add_type "video/quicktime" "video" "QuickTime Video" ".qt" ".mov"
  148. call :add_type "video/quicktime" "video" "QuickTime HD Video" ".hdmov"
  149. :: Real Media formats
  150. call :add_type "application/vnd.rn-realmedia" "video" "Real Media Video" ".rm"
  151. call :add_type "application/vnd.rn-realmedia-vbr" "video" "Real Media Video" ".rmvb"
  152. call :add_type "audio/vnd.rn-realaudio" "audio" "Real Media Audio" ".ra" ".ram"
  153. :: 3GPP formats
  154. call :add_type "audio/3gpp" "audio" "3GPP Audio" ".3ga"
  155. call :add_type "audio/3gpp2" "audio" "3GPP Audio" ".3ga2"
  156. call :add_type "video/3gpp" "video" "3GPP Video" ".3gpp" ".3gp"
  157. call :add_type "video/3gpp2" "video" "3GPP Video" ".3gp2" ".3g2"
  158. :: Video game formats
  159. call :add_type "" "audio" "AY Audio" ".ay"
  160. call :add_type "" "audio" "GBS Audio" ".gbs"
  161. call :add_type "" "audio" "GYM Audio" ".gym"
  162. call :add_type "" "audio" "HES Audio" ".hes"
  163. call :add_type "" "audio" "KSS Audio" ".kss"
  164. call :add_type "" "audio" "NSF Audio" ".nsf"
  165. call :add_type "" "audio" "NSFE Audio" ".nsfe"
  166. call :add_type "" "audio" "SAP Audio" ".sap"
  167. call :add_type "" "audio" "SPC Audio" ".spc"
  168. call :add_type "" "audio" "VGM Audio" ".vgm"
  169. call :add_type "" "audio" "VGZ Audio" ".vgz"
  170. :: Playlist formats
  171. call :add_type "audio/x-mpegurl" "audio" "M3U Playlist" ".m3u" ".m3u8"
  172. call :add_type "audio/x-scpls" "audio" "PLS Playlist" ".pls"
  173. call :add_type "" "audio" "CUE Sheet" ".cue"
  174. :: Register "Default Programs" entry
  175. call :reg add "HKLM\SOFTWARE\RegisteredApplications" /v "mpv" /d "SOFTWARE\Clients\Media\mpv\Capabilities" /f
  176. echo.
  177. echo Installed successfully^^! You can now configure mpv's file associations in the
  178. echo Default Programs control panel.
  179. echo.
  180. if [%unattended%] == [yes] exit 0
  181. <nul set /p =Press any key to open the Default Programs control panel . . .
  182. pause >nul
  183. control /name Microsoft.DefaultPrograms
  184. exit 0
  185. :: 退出系统
  186. :die
  187. if not [%1] == [] echo %~1
  188. if [%unattended%] == [yes] exit 1
  189. pause
  190. exit 1
  191. :: 确认运行脚本的用户身份
  192. :ensure_admin
  193. :: 'openfiles' is just a commmand that is present on all supported Windows
  194. :: versions, requires admin privileges and has no side effects, see:
  195. :: https://stackoverflow.com/questions/4051883/batch-script-how-to-check-for-admin-rights
  196. :: 确定是否是管理员身份
  197. openfiles >nul 2>&1
  198. if errorlevel 1 (
  199. echo This batch script requires administrator privileges. Right-click on
  200. echo mpv-install.bat and select "Run as administrator".
  201. call :die
  202. )
  203. goto :EOF
  204. :: 确认系统类型
  205. :ensure_vista
  206. ver | find "XP" >nul
  207. if not errorlevel 1 (
  208. echo This batch script only works on Windows Vista and later. To create file
  209. echo associations on Windows XP, right click on a video file and use "Open with...".
  210. call :die
  211. )
  212. goto :EOF
  213. :: 注册表操作标签
  214. :reg
  215. :: Wrap the reg command to check for errors
  216. >nul reg %*
  217. if errorlevel 1 set error=yes
  218. if [%error%] == [yes] echo Error in command: reg %*
  219. if [%error%] == [yes] call :die
  220. goto :EOF
  221. :: 读取需要设定的数值
  222. :reg_set_opt
  223. :: Set a value in the registry if it doesn't already exist
  224. set key=%~1
  225. set value=%~2
  226. set data=%~3
  227. reg query "%key%" /v "%value%" >nul 2>&1
  228. if errorlevel 1 call :reg add "%key%" /v "%value%" /d "%data%"
  229. goto :EOF
  230. :: 从这里之后就没有研究意义了,就不做介绍了
  231. :add_verbs
  232. set key=%~1
  233. :: Set the default verb to "play"
  234. call :reg add "%key%\shell" /d "play" /f
  235. :: Hide the "open" verb from the context menu, since it's the same as "play"
  236. call :reg add "%key%\shell\open" /v "LegacyDisable" /f
  237. :: Set open command
  238. call :reg add "%key%\shell\open\command" /d "\"%mpv_path%\" %mpv_args% -- \"%%%%L" /f
  239. :: Add "play" verb
  240. call :reg add "%key%\shell\play" /d "&Play" /f
  241. call :reg add "%key%\shell\play\command" /d "\"%mpv_path%\" %mpv_args% -- \"%%%%L" /f
  242. goto :EOF
  243. :add_progid
  244. set prog_id=%~1
  245. set friendly_name=%~2
  246. :: Add ProgId, edit flags are FTA_OpenIsSafe | FTA_AlwaysUseDirectInvoke
  247. set prog_id_key=%classes_root_key%\%prog_id%
  248. call :reg add "%prog_id_key%" /d "%friendly_name%" /f
  249. call :reg add "%prog_id_key%" /v "EditFlags" /t REG_DWORD /d 4259840 /f
  250. call :reg add "%prog_id_key%" /v "FriendlyTypeName" /d "%friendly_name%" /f
  251. call :reg add "%prog_id_key%\DefaultIcon" /d "%icon_path%" /f
  252. call :add_verbs "%prog_id_key%"
  253. goto :EOF
  254. :update_extension
  255. set extension=%~1
  256. set prog_id=%~2
  257. set mime_type=%~3
  258. set perceived_type=%~4
  259. :: Add information about the file extension, if not already present
  260. set extension_key=%classes_root_key%\%extension%
  261. if not [%mime_type%] == [] call :reg_set_opt "%extension_key%" "Content Type" "%mime_type%"
  262. if not [%perceived_type%] == [] call :reg_set_opt "%extension_key%" "PerceivedType" "%perceived_type%"
  263. call :reg add "%extension_key%\OpenWithProgIds" /v "%prog_id%" /f
  264. :: Add type to SupportedTypes
  265. call :reg add "%supported_types_key%" /v "%extension%" /f
  266. :: Add type to the Default Programs control panel
  267. call :reg add "%file_associations_key%" /v "%extension%" /d "%prog_id%" /f
  268. goto :EOF
  269. :add_type
  270. set mime_type=%~1
  271. set perceived_type=%~2
  272. set friendly_name=%~3
  273. set extension=%~4
  274. echo Adding "%extension%" file type
  275. :: Add ProgId
  276. set prog_id=io.mpv%extension%
  277. call :add_progid "%prog_id%" "%friendly_name%"
  278. :: Add extensions
  279. :extension_loop
  280. call :update_extension "%extension%" "%prog_id%" "%mime_type%" "%perceived_type%"
  281. :: Trailing parameters are additional extensions
  282. shift /4
  283. set extension=%~4
  284. if not [%extension%] == [] goto extension_loop
  285. goto :EOF

打赏

                    支付宝                                                         微信

微信与支付宝支付

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注