本文针对使用RAR自解压程序制作安装包:(只需将下面的代码保存成.js结尾的js脚本就可以使用了)
//<script language="JavaScript">
//创建ws对象 可以取得当前脚本所在路径,执行命令、程序等
var ws = new ActiveXObject("wscript.shell");
//创建fso对象 可以遍历目录、读写文件、复制移动文件等
var fso = new ActiveXObject("Scripting.FileSystemObject");
//创建seo对象 可以执行shellexecute函数
var seo = new ActiveXObject("Shell.Application");
//开始遍历脚本所在目录下的目录,每个目录代表一个安装包目录,这里遍历子目录可以批量生成安装包
var arrFolders = searchFolders(ws.currentdirectory);
for(x in arrFolders)
{
try{
//解析目录,判断目录是不是安装包目录
var strSetupName = parseFolder(arrFolders[x]);
if (strSetupName.length > 0) {
//如果该目录是安装包目录,就生成该目录的安装包,并将生成好的安装包strSetupName拷贝到ws.currentdirectory目录下
buildSetup(arrFolders[x], strSetupName, ws.currentdirectory);
}
} catch(err) {
;
}
}
arrFolders=null;
seo = null;
fso = null;
ws = null;
function alert(message){
try{
ws.Popup(message);
} catch(err) {
;
}
}
//遍历目录
function searchFolders(path){
var arr = new Array();
try{
var fsoFolder = fso.GetFolder(path);
var fsoFolderEnumerator = new Enumerator(fsoFolder.subfolders);
for (; !fsoFolderEnumerator.atEnd(); fsoFolderEnumerator.moveNext())
{
arr.push(fsoFolderEnumerator.item());
}
fsoFolderEnumerator = null;
fsoFolder = null;
} catch(err) {
;
}
return arr;
}
//解析目录是不是安装包目录,并返回将要生成的安装包的名称,如果path不是有效的安装包目录,返回空字符串
//这里可以自己根据自己的安装程序特定自己特殊处理
function parseFolder(path){
var strFile, txtFile, strText;
strFile = path + "\\QlSetup.ini";
//alert(strFile);
try{
txtFile = fso.OpenTextFile(strFile, 1);
strText = txtFile.ReadAll();
txtFile.Close();
} catch(err) {
;
}
//alert(strText);
var chFirst, chNext, ch;
chFirst = strText.indexOf("QSID=");
if (chFirst >= 0) {
chFirst += 5;
//过滤掉空格之类
for(ch = strText.charAt(chFirst); ch<'0' || ch>'9'; chFirst++, ch = strText.charAt(chFirst));
if (chFirst < strText.length) {
//找到QSID结尾
for(chNext = chFirst + 1, ch = strText.charAt(chNext); ch>='0' && ch<='9'; chNext++,ch = strText.charAt(chNext));
var strSetup;
strSetup = strText.substring(chFirst,chNext);
return strSetup;
}
}
return "";
}
//生成path安装包目录的安装包,安装包名字是setup,然后将安装包放到目标目录destination下,如果不指定destination,就直接放在path下
function buildSetup(path, setup, destination){
var strIcon, strSetup, strDestination;
var strcmd;
strIcon = path + "\\qlsetup.ico";
//alert(strIcon);
strSetup = path + "\\" + setup + ".exe";
//alert(strSetup);
if (destination.length>0) {
strDestination = destination + "\\" + setup + ".exe";
//alert(strDestination);
}
try{
fso.DeleteFile(strSetup, true);
} catch(err) {
;
}
//strcmd = 'a -mc -r -sfx -z..\\setup_c.txt -iicon"' + strIcon + '" ' + strSetup + '"';
strcmd = 'a -mc -r -sfx -iicon.\\qlsetup.ico -z..\\setup_c.txt ' + setup + '.exe';
//alert('rar ' + strcmd + ' ' + path);
try{
//seo.ShellExecute("winrar", strcmd, path, "", 1);
var runcmd = 'cmd /K CD "' + path + '"&&' + 'winrar ' + strcmd + '&&EXIT';
//alert(runcmd);
ws.Run(runcmd, 1, true);
if (strDestination.length>0) {
fso.MoveFile(strSetup, strDestination);
}
} catch(err) {
;
}
}
//</script>