<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<TITLE>Photos Player</TITLE>
<meta name="content-type" content="text/html; charset=utf-8">
<HTA:APPLICATION ID="oHTA"
APPLICATIONNAME="myApp"
BORDER="thick"
BORDERSTYLE="normal"
CAPTION="yes"
ContextMenu="yes"
ICON=""
MAXIMIZEBUTTON="yes"
MINIMIZEBUTTON="yes"
scroll="no"
selection="no"
SHOWINTASKBAR="yes"
SINGLEINSTANCE="yes"
SYSMENU="yes"
VERSION="1.0"
WINDOWSTATE="normal"
>
<script language="javascript">
function stringReplaceAll(AFindText,ARepText){
var raRegExp = new RegExp(AFindText.replace(/([\(\)\[\]\{\}\^\$\+\-\*\?\.\"\'\|\/\\])/g,"\\$1"),"ig");
return this.replace(raRegExp,ARepText);
}
String.prototype.replaceAll = stringReplaceAll;
function getFolderPath(FileID)
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
//取得指定路径
var filespec = document.getElementById(FileID).value;
err_msg.innerHTML = "";
//判断指定路径是否为空
if(filespec=="") {err_msg.innerHTML = "请选择文件路径!";return false;}
//判断指定路径文件是否存在
if (!fso.FileExists(filespec))
{
//判断指定路径是否是文件夹
if (!fso.FolderExists(filespec))
{
filespec = filespec.substring(0,filespec.lastIndexOf("\\"));
if(fso.FolderExists(filespec)) return filespec;
err_msg.innerHTML = "指定路径文件不存在!";
//指定路径既不是文件也不是文件夹,返回false
return false;
}
//指定路径是文件夹,返回文件夹的路径
return filespec;
}
//指定路径文件存在,返回指定路径文件的父文件夹的路径
return fso.GetFile(filespec).ParentFolder;
}
function getFiles(folderspec)
{
var fso, f, fc;
fso = new ActiveXObject("Scripting.FileSystemObject");
if(!fso.FolderExists(folderspec)) return false;
f = fso.GetFolder(folderspec);
fc = new Enumerator(f.files);
return fc;
}
function getFolders(folderspec)
{
var fso, f, fc;
fso = new ActiveXObject("Scripting.FileSystemObject");
if(!fso.FolderExists(folderspec)) return false;
f = fso.GetFolder(folderspec);
fc = new Enumerator(f.SubFolders);
return fc;
}
function example(FileID,type)
{
var folderspec=getFolderPath(FileID);
if(folderspec==false) {
alert("Err");
return;
}
/*alert(document.getElementById("replaceType").value);*/
var type = document.getElementById("replaceType").value;
getItem(folderspec,type);
}
function getItem(folderspec,type)
{
if(document.getElementById("selectfile").checked)
changeName(getFiles(folderspec),type);
if(document.getElementById("selectfolder").checked)
changeName(getFolders(folderspec),type);
if(document.getElementById("selectsubfolder").checked)
{
var e=getFolders(folderspec);
for (;!e.atEnd(); e.moveNext())
{
if(document.getElementById("selectfile").checked)
changeName(getFiles(e.item().Path),type);
if(!!getFolders(e.item().Path))
getItem(e.item().Path,type);
}
}
}
function changeName(fc,type)
{
var resultList_str = "";
var fso, f, fc;
fso = new ActiveXObject("Scripting.FileSystemObject");
if(type==1) //替换指定字符串
{
var oldStr = document.getElementById("old_str").value,
newStr = document.getElementById("new_str").value;
if(oldStr == newStr) return;
for (; !fc.atEnd(); fc.moveNext())
{
var newName = fc.item().name.replaceAll(oldStr,newStr);
if(!fso.FileExists(fc.item().ParentFolder + "/" + newName))
fc.item().name = newName;
//fc.item().name = fc.item().name.replace(re, ""); //相关资料参见For Each改成for语句怎么写? Web 开发 - ASP - CSDN社区 community_csdn_net.htm
resultList_str += fc.item().Path + "<br>";
}
}
else if(type==2)
{
var oldStr = document.getElementById("old_str").value;
if(oldStr == "") return;
var i=0;
for (;!fc.atEnd(); fc.moveNext())
{
i++;
}
for (fc.moveFirst();!fc.atEnd(); fc.moveNext())
{
var newName = oldStr + fc.item().name;
if(!fso.FileExists(fc.item().ParentFolder + "/" + newName))
fc.item().name = newName;
//fc.item().name = fc.item().name.replace(re, ""); //相关资料参见For Each改成for语句怎么写? Web 开发 - ASP - CSDN社区 community_csdn_net.htm
resultList_str += fc.item().Path + "<br>";
i--;
if(i<1) break;
}
}
document.getElementById("resultList").innerHTML = resultList_str;
alert("OK");
}
</script>
</head>
<body>
<div style="text-align:center; font-weight:bold; text-indent:-1100px">HTA文件名修改工具</div>
<table style="width: 80%; font-size:10pt;" align="center">
<tr>
<td colspan="4"></td>
</tr>
<tr>
<td>请选择路径:
</td>
<td colspan="3"><input name="File1" id="File1" type="file"><label id="err_msg"></label> (<span lang="zh-cn">注:不能直接选择文件夹对象)</span></td>
</tr>
<tr>
<td><span lang="zh-cn">文件类型:</span></td>
<td colspan="3">
<input name="selectfile" id="selectfile" type="checkbox" checked="checked" >文件
<input name="selectsubfolder" id="selectsubfolder" type="checkbox">包含子文件夹
<input name="selectfolder" id="selectfolder" type="checkbox">重命名文件夹
</td>
</tr>
<tr>
<td><span lang="zh-cn">替换类型:</span></td>
<td><select name="replaceType" id="replaceType">
<option value="1">普通替换</option>
<option value="2" selected="selected">增加前缀</option>
<option value="3">增加后缀</option>
</select></td>
<td> </td>
<td>
</td>
</tr>
<tr>
<td>替换指定字符串<span lang="en-us">:</span></td>
<td><input name="Text1" id="old_str" type="text" size="20" value="[1]"></td>
<td><span lang="zh-cn">替换成</span>--></td>
<td>
<input name="Text2" id="new_str" type="text" size="20" value=""></td>
</tr>
<tr>
<td style="height: 30px">
</td>
<td style="height: 30px">
<input name="Button2" type="button" value="转换" οnclick="example('File1',1);"></td>
<td style="height: 30px"></td>
<td style="height: 30px"></td>
</tr>
<tr>
<td> </td>
<td colspan="3">
<div id="resultList" style="width:450px; height:200px;overflow:auto;border:1px #ccc solid; word-break:break-all;white-space : normal;">2222</div>
</td>
</tr>
</table>
</body>
</html>