2017年2月9日木曜日

HTA画面上にドロップされたフォルダの中身をリストアップする

拡張子がwsfのファイルなら、処理させたいフォルダやファイルをwsfファイルのアイコン上にドラッグ&ドロップすれば、ドロップしたアイテムを引数として与えた状態でwsfファイルを起動できます。
しかしhtaではそれが出来ません。(レジストリを変更すればできますが、できれば触りたくないですし)

次善の策として、HTA画面上にiframeを配置して、そこにドロップされたフォルダのPathを取得してフォルダ内アイテムをリストアップするサンプルを作成しました。


起動画面


エリア内にフォルダをドロップした結果


ソース
<html>
<head>
<title>フォルダ内ファイルリスト取得</title>
<style type="text/css">
.max{width:100%;height:100%;}
</style>
</head>
<body>
<table class=max>
<tr><td><iframe name=ifrName id=ifr class=max style="border:solid 1px #000;"></iframe></td></tr>
<tr height=1><td><button id=btn>cls</button></td></tr>
<tr height=1><td><input id=inp0 style="width:100%;"></td></tr>
<tr height=1><td><input id=inp1 style="width:100%;"></td></tr>
<tr height=1><td><select id=sel></select></td></tr>
</table>
</body>
<script>
resizeTo(400,300)
onload = function(){ btn.onclick() }
getEI=function(id){return document.getElementById(id)}
btn.onclick = function(){
var ifr_=getEI('ifr'), sw初期化, swロック
ifr_.onreadystatechange=function(){
if(swロック || this.readyState!='complete'){return}
if(sw初期化){
var doc=ifr_.contentWindow.document
swロック = true
doc.open()
doc.write('ここにフォルダをドラッグ&ドロップしてください')
doc.close()
return sw初期化 = swロック = false
}
inp0.value = ifr.location
inp1.value = decodeURI(inp0.value.replace(/^file:/,'').replace(/^\/+(\w:)/,'$1'))
sel更新()
ifr初期化()
}
var ifr初期化=function(){
sw初期化 = true
ifr.location = 'about:blank'
}
ifr初期化()
inp0.value=inp1.value=''
sel初期化()
}
sel初期化=function(){
var ops=sel[OP]
while(ops[0]){ ops[0]=null }
}
sel更新=function(){
sel初期化()
var ops=sel[OP], path=inp1.value
if(!fs.FolderExists(path)){return alert('フォルダPathが取得できませんでした')}
for0L(getFileList(path.replace(/[\\\/]$/,'')+'\\*.xls*'), function(i,name){ ops[i] = new Option(name, path+'\\'+name) })
}
// 以下、汎用
iT='innerText', iH='innerHTML', PA='parentNode', FI='firstChild', CN='childNodes', NE='nextSibling', PR='previousSibling', sI='selectedIndex', iB='insertBefore', OP='options', LEN='length'
AXO=function(name){return new ActiveXObject(name)}
shell = AXO('WScript.Shell')
fs = AXO('Scripting.FileSystemObject')
gsf2 = fs.GetSpecialFolder(2)
Read = function(p){var o=fs.OpenTextFile(p), s=o.AtEndOfStream?'':o.ReadAll(); o.Close(); return s}
ReadU = function(p){var o=fs.OpenTextFile(p,1,false,true), s=o.AtEndOfStream?'':o.ReadAll(); o.Close(); return s}
Write = function(path,v){
var flagErr
try{ with(fs.CreateTextFile(path)){Write(v);return Close()} }catch(e){ flagErr=true }
if(flagErr){
// UTF-8でなければ保存できない文字列を含んでいる場合、エラーになる。試しにUTF-8で保存してみる。
try{ with(fs.CreateTextFile(path,true,true)){Write(v);return Close()} }catch(e){
// 書き込み禁止などによるエラーと推測される。
alert('下記Pathファイルを保存できませんでした。\n'+path+'\n\nv(先頭3行)['+(v+'').split('\n').slice(0,3).join('\n')+']\n\ncaller:'+arguments.callee.caller)
}
}
}
RAS = function(p){return Read(p).split('\r\n')}
for0L = function(arr,fun){for(var i=0,L=arr[LEN],res;i<L;i++){if(res=fun(i,arr[i])){return res}}}
forIn = function(obj,fun){var name,res; for(name in obj){if(res=fun(name, obj[name])){return res}}}
execCMD=function(コマンド){
var path0=gsf2+'/'+getTime(), path1=path0+'err', obj={}, fun=function(path,name){if(fs.FileExists(path)){ obj[name]=Read(path); fs.DeleteFile(path) }}
shell.Run('cmd /C '+コマンド + ' > '+path0+' 2> '+path1, 0, true)
fun(path0, 'StdOut')
fun(path1, 'StdErr')
return obj
}
getDirList = function(path, swFile){
var pathTMP=gsf2+'/getDirList_'+getTime()+'.txt', arr
var ret=execCMD('dir /A'+(swFile?'-':'')+'D /B "'+path+'" > "'+pathTMP+'"')
arr = ret.StdOut ? ret.StdOut.split('\r\n') : []
arr.pop()
return arr
}
getFileList=function(path){return getDirList(path, true)}
getTime=function(){return (new Date()).getTime()}
</script>
</html>



0 件のコメント:

コメントを投稿