扩展名为hta的文件即:网页应用程序(Html Application)
这类程序中网页拥有本地安全访问级别,可以创建ActiveX对象、访问内存、文件、注册表、网络等等
本文介绍使用hta编写工具可能碰到的技术,包括:访问文本文件、数据库、xml文件、网络等等
【优势】
不需要安装开发和运行环境
记事本即可编写
浏览器即可运行
WEB开发人员容易上手VBScript、JavaScript
例子1 "hello.hta" 显示对话框
将内容存为"hello.hta"文件即可运行,后面的列子运行方式相同,不再说明
运行上面的程序,将显示出一个对话框,和普通网页完全一样
【访问文件】
例子2 "fileexists.hta" 判断文件目录是否存在
在hat中复杂操作都离不开ActiveX对象,ActiveXObject()是创建该对象的方法。
参数"Scripting.FileSystemObject"则是这个对象注册的名字
在注册表里可以找到:
这样程序就可以知道实际从哪个dll里载入ActiveX对象
Scripting.FileSystemObject明显是一个操作文件系统的对象
详细资料参考: http://msdn.microsoft.com/en-us/library/z9ty6h50(VS.85).aspx
【访问不同编码的文本文件】
现在程序越来越国际化,所以Unicdoe、UTF8编码非常流行
这样在处理文本文件的时候就会碰到不同编码的情况
例子3 "readtextfile.hta" 读取文本文件
Scripting.FileSystemObject可以处理Unicode编码但不能处理Utf8编码。
用ADODB.Stream则可以处理更多的编码;
该对象还可以按字节读取,可是我郁闷没有找到javascript中如何处理variant类型的方法,知道的请告之。
Variant = Stream .Read ( NumBytes )
【创建快捷方式】
例子4 "createshortcut.hta" 创建快捷方式
WScript.Shell是个好东东,shell.SpecialFolders("Desktop");这个方法就是获取桌面的路径
详细可以参考阿笨狗的网页: http://www.pifoo.com/post/WscriptShell.html
【访问XML】
XML目前是相当流行一种数据交互文件
例子5 "addxmlnode.hta" 添加节点
xmldocument.loadXML()是直接载入xml文本
xmldocument.load(fileName)从文件中载入
xmldocument.save(fileName)从保存到文件
例子6 "xpathdemo.hta" xpath演示
寻找xml节点最方便的就是使用xpath表达式
参考: http://www.w3.org/TR/xpath
【访问网络】
这个是ajax常用的“Microsoft.XMLHTTP”对象,当然本地安全级不用担心跨域问题。
这里就不多讲了,参考:
【脚本共享】优快云版主批量删帖工具。
优快云积分查询--清洁工2008版
【访问数据库】
使用ADODB.Connection链接数据。
例子7 "Access.hta" 创建Access数据库和执行插入和查询
【调用其他程序】
例子8 "runnotepad.hta" 打开记事本
例子9 "processes.hta" 遍历当前进程
例子10 "doscmd.hta" 执行dos命令
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="ProgId" content="PowerPoint.Slide"> <meta name="Generator" content="Microsoft PowerPoint 11"> <!--[if !mso]> <style> v/:* {behavior:url(#default#VML);} o/:* {behavior:url(#default#VML);} p/:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} v/:textbox {display:none;} </style> <![endif]-->幻灯片 2<meta name="Description" content="2008-11-1"> <!--[if !ppt]--><style> .O {font-size:149%;} </style> <style media="print"> <!--.sld {left:0px !important; width:6.0in !important; height:4.5in !important; font-size:103% !important;} --> </style> <!--[endif]--><shapelayout v:ext="edit"></shapelayout><idmap v:ext="edit" data="1"></idmap><colorscheme colors="#ffffff,#000000,#808080,#000000,#bbe0e3,#333399,#009999,#99cc00"><div v:shape="_x0000_s1026" style=""> <div class="O"> <span style="font-family: 华文新魏; font-size: 133%;"><span style="position: absolute; left: -4.02%; font-family: Wingdings;">u</span></span><span style="font-family: 华文新魏; font-size: 24pt;"></span> </div> <div class="O" style="text-align: center;"></div> </div> </colorscheme>
这类程序中网页拥有本地安全访问级别,可以创建ActiveX对象、访问内存、文件、注册表、网络等等
本文介绍使用hta编写工具可能碰到的技术,包括:访问文本文件、数据库、xml文件、网络等等
【优势】
不需要安装开发和运行环境
记事本即可编写
浏览器即可运行
WEB开发人员容易上手VBScript、JavaScript
例子1 "hello.hta" 显示对话框
- <html>
- <scripttype="text/javascript">
- alert("helloworld");
- </script>
- </html>
运行上面的程序,将显示出一个对话框,和普通网页完全一样
【访问文件】
例子2 "fileexists.hta" 判断文件目录是否存在
- <html>
- <body>
- <div>
- <inputid="TextFilename"type="text"value="c:/autoexec.bat"/>
- <inputtype="button"value="判断文件是否存在"onclick="ButtonFileExists();"/>
- </div>
- <div>
- <inputid="TextFolder"type="text"value="c:/windows"/>
- <inputtype="button"value="判断目录是否存在"onclick="ButtonFolderExists();"/>
- </div>
- <scripttype="text/javascript">
- var$=document.getElementById;
- functionButtonFileExists(){
- varfso=newActiveXObject("Scripting.FileSystemObject");
- alert(fso.FileExists($("TextFilename").value)?"文件存在":"文件不存在");
- fso=null;
- }
- functionButtonFolderExists(){
- varfso=newActiveXObject("Scripting.FileSystemObject");
- alert(fso.FolderExists($("TextFolder").value)?"目录存在":"目录不存在");
- fso=null;
- }
- </script>
- <body>
- </html>
参数"Scripting.FileSystemObject"则是这个对象注册的名字
在注册表里可以找到:
- HKEY_CLASSES_ROOT/Scripting.FileSystemObject/CLSID
- @={0D43FE01-F093-11CF-8940-00A0C9054228}
- ----
- HKEY_CLASSES_ROOT/CLSID/{0D43FE01-F093-11CF-8940-00A0C9054228}
- @=FileSystemObject
- ----
- HKEY_CLASSES_ROOT/CLSID/{0D43FE01-F093-11CF-8940-00A0C9054228}/InprocServer32
- @=C:/WINDOWS/system32/scrrun.dll
- ----
- HKEY_CLASSES_ROOT/CLSID/{0D43FE01-F093-11CF-8940-00A0C9054228}/ProgID
- @=Scripting.FileSystemObject
Scripting.FileSystemObject明显是一个操作文件系统的对象
详细资料参考: http://msdn.microsoft.com/en-us/library/z9ty6h50(VS.85).aspx
【访问不同编码的文本文件】
现在程序越来越国际化,所以Unicdoe、UTF8编码非常流行
这样在处理文本文件的时候就会碰到不同编码的情况
例子3 "readtextfile.hta" 读取文本文件
- <html>
- <body>
- <div>
- 文件名:<inputid="TextFilename"type="text"value="tempUnicode.txt"/>
- <inputtype="button"value="按gb2312读取"onclick="ButtonReadTextClick('gb2312');"/>
- <inputtype="button"value="按Unicode读取"onclick="ButtonReadTextClick('Unicode');"/>
- <inputtype="button"value="按UTF-8读取"onclick="ButtonReadTextClick('UTF-8');"/>
- </div>
- <textareaid="TextareaText"style="height:400px;width:100%;"></textarea>
- <scripttype="text/javascript">
- varBinaryStreamType=1;
- varTextStreamType=2;
- var$=document.getElementById;
- functionButtonReadTextClick(charset)
- {
- varadostream=newActiveXObject("ADODB.Stream");
- adostream.Type=TextStreamType;
- if(typeof(charset)=="undefined") charset=charset;
- adostream.Charset=charset;
- adostream.Open();
- adostream.LoadFromFile($("TextFilename").value);
- varcontents=adostream.ReadText();
- adostream.Close();
- adostream=null;
- $("TextareaText").value=contents;
- }
- </script>
- <body>
- </html>
用ADODB.Stream则可以处理更多的编码;
该对象还可以按字节读取,可是我郁闷没有找到javascript中如何处理variant类型的方法,知道的请告之。
Variant = Stream .Read ( NumBytes )
【创建快捷方式】
例子4 "createshortcut.hta" 创建快捷方式
- <html>
- <title>快捷方式--清洁工2008版</title>
- <body>
- <div>
- <inputtype="button"value="开始"onclick="ButtonStartClick();"id="ButtonStart"/>
- </div>
- <scriptlanguage="javascript"type="text/javascript">
- var$=document.getElementById;
- functionButtonStartClick() {
- varshell=newActiveXObject("WScript.Shell");
- varstrDesktop=shell.SpecialFolders("Desktop");
- varlink=shell.CreateShortcut(strDesktop+"//一个记事本.lnk");
- link.TargetPath="notepad.exe";
- link.WorkingDirectory="";
- link.Save();
- varlink=shell.CreateShortcut(strDesktop+"//床上等你.url");
- link.TargetPath="http://www.youkuaiyun.com";
- link.Save();
- link=null;
- shell=null;
- }
- </script>
- </body>
- </html>
详细可以参考阿笨狗的网页: http://www.pifoo.com/post/WscriptShell.html
【访问XML】
XML目前是相当流行一种数据交互文件
例子5 "addxmlnode.hta" 添加节点
- <html>
- <body>
- <div>
- <inputtype="button"value="测试"onclick="ButtonClick();"/>
- </div>
- <textareaid="TextareaText"style="height:400px;width:100%;"></textarea>
- <scripttype="text/javascript">
- var$=document.getElementById;
- functionButtonClick(){
- varxmldocument=newActiveXObject("MSXML2.DOMDocument");
- xmldocument.loadXML("<?xmlversion=/"1.0/"?><root></root>");
- varroot=xmldocument.documentElement;
- varnewnode=root.appendChild(xmldocument.createElement("newnode"));
- newnode.setAttribute("att1","1");
- newnode.setAttribute("att1","2");
- newnode.text="zswang路过";
- $("TextareaText").value=xmldocument.xml;
- xmldocument=null;
- }
- </script>
- <body>
- </html>
xmldocument.load(fileName)从文件中载入
xmldocument.save(fileName)从保存到文件
例子6 "xpathdemo.hta" xpath演示
- <html>
- <body>
- <textareaid="TextareaXML"style="width:100%;height:45%">
- <?xmlversion="1.0"?>
- <test>
- <xa="1">
- <xa="2"b="B">
- <x>
- <y>y31</y>
- <y>y32</y>
- <ya="3">y33</y>
- </x>
- </x>
- </x>
- <ya="4">y11</y>
- </test>
- </textarea>
- <textareaid="TextareaText"style="width:100%;height:45%"></textarea>
- <buttononclick="ButtonClick(this);"title="寻找Tag为y的节点">//y</button>
- <buttononclick="ButtonClick(this);"title="寻找存在属性a的节点">//*[@a]</button>
- <buttononclick="ButtonClick(this);"title="寻找文本为y31的节点">//y[.="y31"]</button>
- <buttononclick="ButtonClick(this);"title="寻找Tag为y并且序号3的节点">//y[position()=3]</button>
- <scriptlanguage="javascript">
- var$=document.getElementById;
- varxmldocument=newActiveXObject("MSXML2.DOMDocument");
- functionButtonClick(sender){
- xmldocument.loadXML($("TextareaXML").value);
- xmldocument.setProperty("SelectionLanguage","XPath");
- varnodes=xmldocument.selectNodes(sender.innerText);
- for(vari=0;i<nodes.length;i++)
- nodes[i].setAttribute("find","****");
- $("TextareaText").value=xmldocument.xml;
- }
- </script>
- </body>
- </html>
参考: http://www.w3.org/TR/xpath
【访问网络】
这个是ajax常用的“Microsoft.XMLHTTP”对象,当然本地安全级不用担心跨域问题。
这里就不多讲了,参考:
【脚本共享】优快云版主批量删帖工具。
优快云积分查询--清洁工2008版
【访问数据库】
使用ADODB.Connection链接数据。
例子7 "Access.hta" 创建Access数据库和执行插入和查询
- <html>
- <title>测试</title>
- <div>
- <inputtype="button"value="测试"onclick="ButtonStartClick();"id="ButtonStart"/>
- </div>
- <divstyle="width:100%;height:80%">
- <textareaid="TextareaLog"style="width:100%;height:100%"></textarea>
- </div>
- <scriptlanguage="javascript"type="text/javascript">
- var$=document.getElementById;
- varaccessfile="temp.mdb";
- varadSchemaTables=20;
- functionButtonStartClick(){
- varcreateDatabase=false;
- ///Begin确保数据库文件存在
- varfso=newActiveXObject("Scripting.FileSystemObject");
- if(!fso.FileExists(accessfile)){
- varadoxcatalog=newActiveXObject("ADOX.Catalog");
- try{
- adoxcatalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+accessfile);
- createDatabase=true;
- }catch(e){
- return;
- }
- adoxcatalog=null;
- }
- ///End确保数据库文件存在
- ///Begin确保表存在
- varexistsTable=false;
- varconnection=newActiveXObject("ADODB.Connection");
- try{
- connection.Open("Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+accessfile);
- if(!createDatabase){
- varrecordset=connection.OpenSchema(adSchemaTables);
- recordset.MoveFirst();
- while(!recordset.EOF){
- if(recordset("TABLE_TYPE")=="TABLE"){
- existsTable=true;
- break;
- }
- recordset.MoveNext();
- }
- recordset.Close();
- recordset=null;
- }
- if(!existsTable){
- connection.Execute("CREATETABLEtemp(记录号CounterPrimarykey,名称Text(10))");
- connection.Execute("INSERTINTOtemp(名称)values(/"zswang/")");
- connection.Execute("INSERTINTOtemp(名称)values(/"hcat1999/")");
- connection.Execute("INSERTINTOtemp(名称)values(/"ming4098/")");
- }
- }catch(e){
- return;
- }
- ///End确保表存在
- varrecordset=newActiveXObject("ADODB.Recordset");
- try{
- recordset.Open("SELECT*FROMtemp",connection);
- recordset.MoveFirst();
- while(!recordset.EOF){
- $("TextareaLog").value+=recordset("名称")+"/r/n";
- recordset.MoveNext();
- }
- recordset.Close();
- recordset=null;
- }catch(e){
- return;
- }
- connection.Close();
- }
- </script>
- </html>
例子8 "runnotepad.hta" 打开记事本
- <html>
- <body>
- <buttononclick="ButtonClick();">打开记事本</button>
- <scripttype="text/javascript">
- varSW_HIDE=0;
- varSW_NORMAL=1;
- varSW_MINIMIZED=2;
- varSW_MAXIMIZED=3;
- varSW_NOACTIVATE=4;
- varSW_SHOW=5;
- varSW_MINIMIZE=6;
- varSW_MINNOACTIVE=7;
- varSW_SHOWNA=8;
- varSW_RESTORE=9;
- varSW_DEFAULT=10;
- functionButtonClick(){
- varshell=newActiveXObject("WScript.Shell");
- shell.Run("notepad.exe",SW_MAXIMIZED,true);//执行并等待
- shell=null;
- }
- </script>
- </body>
- </html>
- <html>
- <body>
- <script>
- try{
- varlocator=newActiveXObject("WbemScripting.SWbemLocator");
- varservice=locator.ConnectServer(".","/root/CIMV2");
- varprocesses=service.ExecQuery("Select*fromWin32_Process");
- varprocessEnum=newEnumerator(processes);
- varmsg="";
- for(;!processEnum.atEnd();processEnum.moveNext())
- {
- varprocess=processEnum.item();
- msg+=process.Name+"<br/>";
- }
- document.write(msg);
- }
- catch(e){
- }
- </script>
- </body>
- </html>
- <html>
- <body>
- <buttononclick="ButtonClick();">设置NTFS文件访问权限</button>
- <scripttype="text/javascript">
- varSW_HIDE=0;
- varSW_NORMAL=1;
- varSW_MINIMIZED=2;
- varSW_MAXIMIZED=3;
- varSW_NOACTIVATE=4;
- varSW_SHOW=5;
- varSW_MINIMIZE=6;
- varSW_MINNOACTIVE=7;
- varSW_SHOWNA=8;
- varSW_RESTORE=9;
- varSW_DEFAULT=10;
- functionButtonClick(){
- varshell=newActiveXObject("WScript.Shell");
- shell.Run("cmd.exe/ccopyc://AUTOEXEC.BATd://AUTOEXEC.TXT",SW_HIDE,true);//执行并等待
- shell.Run("cmd.exe/ccaclsd://AUTOEXEC.TXT/e/p/"localservice/":w",SW_HIDE,true);//执行并等待
- shell=null;
- }
- </script>
- </body>
- </html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="ProgId" content="PowerPoint.Slide"> <meta name="Generator" content="Microsoft PowerPoint 11"> <!--[if !mso]> <style> v/:* {behavior:url(#default#VML);} o/:* {behavior:url(#default#VML);} p/:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} v/:textbox {display:none;} </style> <![endif]-->幻灯片 2<meta name="Description" content="2008-11-1"> <!--[if !ppt]--><style> .O {font-size:149%;} </style> <style media="print"> <!--.sld {left:0px !important; width:6.0in !important; height:4.5in !important; font-size:103% !important;} --> </style> <!--[endif]--><shapelayout v:ext="edit"></shapelayout><idmap v:ext="edit" data="1"></idmap><colorscheme colors="#ffffff,#000000,#808080,#000000,#bbe0e3,#333399,#009999,#99cc00"><div v:shape="_x0000_s1026" style=""> <div class="O"> <span style="font-family: 华文新魏; font-size: 133%;"><span style="position: absolute; left: -4.02%; font-family: Wingdings;">u</span></span><span style="font-family: 华文新魏; font-size: 24pt;"></span> </div> <div class="O" style="text-align: center;"></div> </div> </colorscheme>