Windows的文件类型关联

在用脚本语言开发时尤其是在windows环境下发现想自动在命令行环境下运行脚本必须要带着相应的解释器的路径才行,不然就会提示无法找到对应的命令,于是乎在《学习Ruby》这本书中对于文件类型关联的方法有所介绍,其方法主要是采用了assoc和ftype两个命令来建立文件关联的。实际上其他脚本语言例如perl、tcl、python也是如此。

以Ruby语言为例,具体做法如下:

在命令行下输入如下命令:C:/Documents and Settings/Stanley>assoc .rb=rbFile

接着测试文件关联是否成立:C:/Documents and Settings/Stanley>assoc .rb
                                                                                                                     .rb=rbFile

接着测试文件类型是否存在:C:/Documents and Settings/Stanley>ftype rbfile
                                                                                                                      rbfile="c:/ruby/bin/ruby.exe" "%1" %*

如果不存在可以手动建立:C:/Documents and Settings/Stanley>ftype rbfile=  "c:/ruby/bin/ruby.exe" "%1" %*

最后把.rb加入环境变量PATHEXT,先检查看看:

C:/Documents and Settings/Stanley>set pathext
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.RB;.RBW

如果没有看到.rb就动手将它加进去:C:/Documents and Settings/Stanley>set pathext=.rb;%pathext%

然后再次测试:

C:/Documents and Settings/Stanley>set pathext
PATHEXT=.rb;.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.RB;.RBW

查看关联
assoc .wei
设置关联
assoc .wei=weigefile

查看具体关联
ftype weigefile
设置具体关联
ftype weigefile="C:路径.exe" "%1" %*

注册表UserChoice优先级最高

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionExplorerFileExts.tif]

[HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionExplorerFileExts.tifOpenWithList]
"a"="DllHost.exe"
"MRUList"="cbjiahgfe"
"c"="WardTestTIF.exe"
"d"="STDUViewerApp.exe"
"f"="WINWORD.EXE"
"g"="MSPVIEW.EXE"
"h"="mspaint.exe"
"j"="PDFEdit.exe"

[HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionExplorerFileExts.tifOpenWithProgids]
"TIFImage.Document"=hex(0):
"MSDI"=hex(0):
"ward"=hex(0):

[HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionExplorerFileExts.tifUserChoice]
"Progid"="Applications\WardTestTIF.exe"

原文地址:https://www.cnblogs.com/lee2011/p/6340687.html