您的位置:首页 > Web前端 > JavaScript

自编自用的歌曲整理程序 js+html+hta

2013-04-11 21:32 225 查看
功用:
把临时文件夹中的音乐文件按指定的格式自动移动存放到指定的文件夹内:
如我要求是把未处理的文件从某个临时文件夹中移动到music文件夹中,且按文件的上级目录是歌手名/歌曲名做文件名规则存放;
-----------
截屏:





-------------

代码:
复制另存为
xxxx.hta
双击运行即可
------------
<HTML><HEAD><TITLE>歌曲整理程序</TITLE><?XML:NAMESPACE PREFIX = HTA /><HTA:APPLICATION id=rebuild VERSION="1.0" SYSMENU="yes" SINGLEINSTANCE="yes" SHOWINTASKBAR="yes" MINIMIZEBUTTON="yes" MAXIMIZEBUTTON="yes" ICON="C:\Windows\system32\Perfcentercpl.ico" CONTEXTMENU="yes" CAPTION="yes" BORDERSTYLE="normal" BORDER="thick" APPLICATIONNAME="rebuild"></HTA:APPLICATION>
<STYLE>
a{
text-decoration:none;
}
.infoCLS{
overflow:auto;
width:100%;
height:300px;

}
.alertDiv {
background-color:white;
top:0px;
left:0px;
position:absolute;
z-index:9999;
width:102%;
height:100%;
text-align:center;
padding-top:80px;
}
.alertDiv .fieldset{
text-align:left;
width:500px;
height:400px;
overflow:auto;
}
.alertClose{
border:0px solid blue;
color:blue;
background-color:white;
}
input {
border:1px solid buttonface;
margin:0px 5px;
}
body{
font-size:12px;
line-height:20px;
}
fieldset{
margin-bottom:15px;
padding:5px;
}
.redRadio{
border:0px solid red;
width:15px;
height:15px;
}
.redSpan{

}
.typeInput{
width:300px;
border:0px none;
border-bottom:1px solid red;
}
</STYLE>
</HEAD>
<BODY>
<FIELDSET><LEGEND>选项/配置</LEGEND>
<SPAN>指定整理文件类型</SPAN><INPUT id=fileType value="|mp3|ape|flac|" style="width:500px;" />tip:使用|分隔各种类型<BR>
<SPAN>选择整理前文件夹</SPAN><INPUT id=buildPath4 type=file style="width:500px;" />tip:选择文件夹中的一个文件即可<BR>
<SPAN>选择整理后文件夹</SPAN><INPUT id=buildPath2 type=file style="width:500px;" />tip:选择文件夹中的一个文件即可<BR>
<INPUT value=开始整理 type=button onclick="doing(this)" id="startBtn" />
<INPUT value=停止整理 type=button onclick="stoping()" id="stopBtn" />
<INPUT onclick=saveCFG(); value=保存配置 type=button disabled> </FIELDSET>

<fieldset>
<legend>操作面板</legend>
<div id="moveBar">
</div>
</fieldset>

<FIELDSET><LEGEND>信息框 <A title=清除下面的内容 onclick=clearInfo(); href="#1">清屏</A>
<a href=# onclick="this.firstChild.click();"><input type=checkbox id="scrollTip" />自动滚屏</a>
</LEGEND>
<DIV id=info class=infoCLS>
<pre>
使用帮助:

程序目的在于将不规则的歌曲文件整理成按歌手/乐器类型做目录,歌曲名字做文件名规则存放,方便快速整理;

整理思路:
把待整理文件存放一个临时目录中,注意不能存放于整理后的目录之下,因为这样可能会导致移动后的目录指向移动前的目录中,出现永远无法处理干净问题;
指定整理后存放目录;
程序自动读取所有的待整理文件分解出上级目录名和文件名,这时可以使用自动脚本对目录名或文件名进行处理,或是手动修改后应用就完成一个文件整理过程;
只要是简化新建目录,移动,改名步骤;
因为待整理的文件命名格式是不统一,无法完全自动化整理,还是需要人工确认的;
自动脚本可以通过修改js方式自增减;
</pre>
</DIV></FIELDSET>
<SCRIPT>
function getObj(id){
return document.getElementById(id);
}

function getObjs(name){
return document.getElementsByName(name);
}

function clearInfo(){
getObj('info').innerHTML = '';
}

function tipMSG(str){
var div = document.createElement('DIV');
div.innerHTML = str;
div = getObj('info').appendChild(div);

if (getObj('scrollTip').checked){
div.scrollIntoView(true);
}
}

function createFs(){
return new ActiveXObject("Scripting.FileSystemObject");
}

/*
* 保存配置:将当前运行html替换掉旧的html
*/
function saveCFG(){
var newHtml = document.documentElement.outerHTML;
var htaPath = location.pathname;
var forWrite = 2;
var charset = false;//ascii
var fs = createFs();
var fp = fs.OpenTextFile(htaPath, forWrite, charset);
fp.write(newHtml);
fp.close();
fs = fp = null;
showAlertDiv({html:'保存完成!'});
}

function stoping(){
clearTimeout(window.moveTimer);
window.foldersObj = null;
window.filesObj = null;
window.folder2 = null;
showBar('');
getObj('startBtn').value = '开始整理';
tipMSG('处理已停止.');
}

function getFileFolder(pathStr){
return createFs().getFile(pathStr).parentFolder;
}

function checkSetting(){
var fs = createFs();

getObj('fileType').value = getObj('fileType').value.toLowerCase();

if (! /^\|\w+(\|\w+)*\|$/.test(getObj('fileType').value)){
showAlertDiv({html:'请指定处理类型或格式不正确,正确格式:|mp2|mp3|ape|'});
getObj('fileType').focus();
return false;
}

if (! fs.FileExists(getObj('buildPath4').value)){
showAlertDiv({html:'请选择有效的整理前的文件夹'});
getObj('buildPath4').focus();
return false;
}

if (! fs.FileExists(getObj('buildPath2').value)){
showAlertDiv({html:'请选择有效的整理后的文件夹'});
getObj('buildPath2').focus();
return false;
}

return true;
}

function doing(obj){
if (checkSetting() && (obj.value == '开始整理') ){
window.foldersObj = null;
window.filesObj = null;
window.folder2 = getFileFolder(getObj('buildPath2').value);
var folderStr = getFileFolder(getObj('buildPath4').value);
tipMSG('处理文件夹: ' + folderStr);
recFolders(folderStr);
recFiles(folderStr);
moveFile();
obj.value = '整理中...';
}
}

function recFolders(folderStr){
if (! window.foldersObj) window.foldersObj = [];

var fs = createFs();
var folders = new Enumerator(fs.getFolder(folderStr).subFolders);

for (; ! folders.atEnd(); folders.moveNext()){
window.foldersObj.push(folders.item());
}
}

function recFiles(folderStr){
if (! window.filesObj) window.filesObj = [];

var fs = createFs();
var files = new Enumerator(fs.getFolder(folderStr).files);

for (; ! files.atEnd(); files.moveNext()){
var ext = fs.GetExtensionName(files.item());
ext = ext.toLowerCase();

if (getObj('fileType').value.indexOf('|' + ext + '|') < 0){//不必移动类型
tipMSG('类型不符,跳过:' + files.item());
continue;
}

window.filesObj.push(files.item());
}
}

function moveFile(){
if (! window.filesObj || ! window.filesObj.length ){//本文件夹内的文件已处理完成
if (! window.foldersObj || ! window.foldersObj.length){//处理结束
var tipStr = '处理已完成,如果还有剩余的文件,是被忽略的类型.';
tipMSG(tipStr);
showAlertDiv({html:tipStr});
stoping();
return true;
}else{//处理下个待处理文件夹
var folderStr = window.foldersObj.pop();
tipMSG('下轮处理文件夹: ' + folderStr);
recFolders(folderStr);
recFiles(folderStr);
time4move();
}
}else{//处理某文件
var file = window.filesObj.pop();
moveFileCFG(file);
}
}

function moveFileCFG(file){
tipMSG('处理文件: ' + file);
var fs = createFs();
var name = fs.GetBaseName(file);
var ext = fs.GetExtensionName(file);
var html = '';
html += '<fieldset>';
html += '<legend>移动操作面板</legend>';
html += '将文件 ' + file + '<br />';
html += '移动为<br />';
html += window.folder2 + '\\';

html += '<span onclick="this.firstChild.click();">';
html += '<input type=radio name="type" id="autoFolder" class=redRadio title="选中时,点击下面的自动脚本将会改变文件夹名输入框中的内容"/>';
html += '<input value="' + name + '" id="newFolderName" class=typeInput title="你也可以手动修改此输入框内容" />';
html += '</span>';
html += '\\';

html += '<span onclick="this.firstChild.click();">';
html += '<input type=radio name="type" id="autoFile" checked class=redRadio title="选中时,点击下面的自动脚本将会改变文件名输入框中的内容" />';
html += '<input value="' + name + '" id="newFileName" class=typeInput title="你也可以手动修改此输入框内容"/>';
html += '</span>';

html += '.' + ext + '<br />';

html += '<input type=button value="应用" onclick="callAlertFuns(\'moveFiling\');"/> ';
html += '<input type=button value="跳过" onclick="callAlertFuns(\'skip\');"/> ';
html += '<input type=button value="删除并跳过" onclick="callAlertFuns(\'delSkip\');"/> ';
html += '</fieldset>';

html += '<fieldset>';
html += '<legend>自动处理脚本(<span style="color:red;">请在上面的红线点击选中要操作对象</span>)</legend>';

for (var i = 0; i < window.autoMethods.length; i++){//列出所有的自动处理方法
var cm = window.autoMethods[i];
html += '<a href=# onclick="callMethod(' + i+ ');">' + cm.title + '</a> ';
}

html += '</fieldset>';

window.alertFuns = {file:file, ext:ext};
window.alertFuns.moveFiling = function (){
var folder = getObj('newFolderName').value;
var newName = getObj('newFileName').value;
var ext = window.alertFuns.ext;
showBar('');
moveFiling(window.alertFuns.file, folder, newName, ext);
};
window.alertFuns.skip = function (){
showBar('');
tipMSG('跳过本文件');
time4move();
};
window.alertFuns.delSkip = function (){
showBar('');
createFs().DeleteFile(window.alertFuns.file, true);
tipMSG('删除并跳过本文件');
time4move();
};

showBar(html);
}

function moveFiling(file, folder, newName, ext){
var fs = createFs();
var newFolder = window.folder2 + '/' + folder;

if (! fs.FolderExists(newFolder)){
tipMSG('创建文件夹:' + newFolder);
fs.CreateFolder(newFolder);
}

var newFile = newFolder + '/' + newName + '.' + ext;

if (fs.FileExists(newFile)){//同名存在
var tipStr = '同名文件已存在,是否覆盖?<br /><br />';
var fileInfo = '(' + (fs.GetFile(file).size / 1048576) + ' MB) ';
tipStr += '旧文件(待删除):' + fileInfo + file + ';<br />';
fileInfo = '(' + (fs.GetFile(newFile).size / 1048576) + ' MB) ';
tipStr += '新文件(待移动):' + fileInfo + newFile + ';<br /><br />';
tipStr += '确认后删除旧文件,使用新文件<br /><br />';
tipStr += '<input type=button value="覆盖" onclick="callAlertFuns(\'rewrite\');"/> ';
tipStr += '<input type=button value="跳过" onclick="callAlertFuns(\'skip\');" /> ';
tipStr += '<input type=button value="删除新文件并跳过" onclick="callAlertFuns(\'delSkip\');" /> ';
window.alertFuns = {file:file, newFile:newFile};
window.alertFuns.rewrite = function (){
showBar('');
fs.DeleteFile(window.alertFuns.newFile, true);//只读属性的也删除
fs.MoveFile(window.alertFuns.file, window.alertFuns.newFile);
time4move();
};
window.alertFuns.skip = function (){
showBar('');
tipMSG('跳过不处理.');
time4move();
};
window.alertFuns.delSkip = function (){
showBar('');
tipMSG('删除新文件且跳过覆盖不处理.');
fs.DeleteFile(window.alertFuns.newFile, true);//只读属性的也删除
fs.MoveFile(window.alertFuns.file, window.alertFuns.newFile);
time4move();
};

showBar(tipStr);
}else{
fs.MoveFile(file, newFile);
time4move();
}
}

function time4move(ms){
if (! isNaN(ms) || (ms < 1000) ){
ms = 1000;
}

clearTimeout(window.moveTimer);
window.moveTimer = setTimeout(moveFile, ms);
}

function showBar(str){
getObj('moveBar').innerHTML = str;
}

function showAlertDiv(obj){
var div = getObj('alertDiv');

if (! div){
div = document.createElement('DIV');
div.id = 'alertDiv';
div.className = 'alertDiv';
var divHtml = '<fieldset class="fieldset">';
divHtml += '<legend>对话框 ';
divHtml += '<input type=button onclick="hideAlertDiv();" id="alertClose" value=关闭 class=alertClose />';
divHtml += '</legend>';
divHtml += '<div id="alertDivContent">' + obj.html + '</div>';
divHtml += '</fieldset>';
div.innerHTML = divHtml;
div = document.body.appendChild(div);
}else{
getObj('alertDivContent').innerHTML = obj.html;
div.style.display = 'block';
}

document.body.style.overflow = 'hidden';

if (! obj.noClose){
getObj('alertClose').style.display = 'inline';
setTimeout(function (){getObj('alertClose').focus();}, 100);
}else{
getObj('alertClose').style.display = 'none';
}
}

function hideAlertDiv(){
getObj('alertDiv').style.display = 'none';
document.body.style.overflow = 'auto';
}

function callAlertFuns(name, caller){
window.alertFuns[name].call(caller);
}

function callMethod(i){
var cm = window.autoMethods[i];

try{
var doObj = getObj('autoFile').checked ? getObj('newFileName') : getObj('newFolderName');
cm.funer(doObj, window.alertFuns.file);
}catch(e){
alert({html:'运行自动处理[' + cm.title + ']出错: ' + e.description});
}
}

window.autoMethods = [];

//==============================
/*
* 自动处理集
* 增加格式

window.autoMethods.push(
{
title:'自动处理名字,用于显示',
funer:function(doObj, file){
//*
//* 参数说明:
//* doObj: 处理的对象,文件名或文件夹input对象
//* file: 待处理文件的路径字符串
//* 自动处理函数体
//*
}
}
);

*/

window.autoMethods.push(
{
title:'下划线换空格',
funer:function(doObj, file){
doObj.value = doObj.value.replace(/_/g, ' ');
}
}
);

window.autoMethods.push(
{
title:'用目录',
funer:function(doObj, file){
doObj.value = getFileFolder(file).name.replace(/^.+[\\\/]/, '');
}
}
);

window.autoMethods.push(
{
title:'用_前节',
funer:function(doObj, file){
doObj.value = doObj.value.replace(/ *_.*$/, '');
}
}
);

window.autoMethods.push(
{
title:'用_后节',
funer:function(doObj, file){
doObj.value = doObj.value.replace(/^.*_ */, '');
}
}
);

window.autoMethods.push(
{
title:'用-前节',
funer:function(doObj, file){
doObj.value = doObj.value.replace(/ *\-.*$/, '');
}
}
);

window.autoMethods.push(
{
title:'用-后节',
funer:function(doObj, file){
doObj.value = doObj.value.replace(/^.*\- */, '');
}
}
);

window.autoMethods.push(
{
title:'去俩首尾空格',
funer:function(doObj, file){
getObj('newFileName').value = getObj('newFileName').value.replace(/^ +| +$/, '');
getObj('newFolderName').value = getObj('newFolderName').value.replace(/^ +| +$/, '');
}
}
);

</SCRIPT>
</BODY></HTML>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐