2017年3月31日金曜日

ウィンドウを閉じると自動で削除されるフォルダ

一時的なファイルの置き場所(用が済んだら削除したい)が欲しい時があります。
ウィンドウを開くと自動で専用のテンポラリフォルダが作成され、閉じると削除前の確認メッセージを経て削除される、というプログラムを作成しました。




実行画面



ウィンドウを閉じると表示されるメッセージ



「キャンセル」を押すと再度ウィンドウが表示されます。
「OK」を押すとフォルダが削除されます。

以下ソース
vbs=function(){
var sc=new ActiveXObject('ScriptControl')
sc.Language = 'VBScript'
sc.AddCode((function(){/*
F GetTypeName(obj) : GetTypeName = TypeName(obj): EF
F vbMsgBox(str, buttons, title) : vbMsgBox = msgbox(str, buttons, title): EF
F vbInputBox(str, title, default) : vbInputBox = inputbox(str, title, default): EF
F getAdminMembers(pcName)
Dim objGroup, objUser, str
set objGroup = GetObject("WinNT://" & pcName & "/Administrators")
str = ""
For Each objUser in objGroup.Members
str = str & objUser.Name & vbLf
Next
getAdminMembers = str
EF
Class TerminaterClass
Public funTer
Private Sub Class_Terminate: funTer: End Sub
Public Sub setF(f): set funTer=f: End Sub
End Class
F getTerminater
Set getTerminater = New TerminaterClass
EF
Sub a(f)
dim b
set b = New TerminaterClass
b.C(f)
msgbox 1111
End Sub
*/}+'').split('/*')[1].split('*/')[0].replace(/(\s)EF(\s)/g,'$1End F$2').replace(/(\s)F(\s)/g,'$1Function$2'))
return {
sc:sc,
msgbox:function(prompt, buttons, title){
// [buttons]
// 0:OK、 1:OK・キャンセル、 2:中止・再試行・無視、 3:はい・いいえ・キャンセル、 4:はい・いいえ、 5:再試行・キャンセル
// 0x10:警告メッセージアイコン, 0x20:問い合わせメッセージアイコン、0x30:注意メッセージアイコン、 0x40:情報メッセージアイコン
// 0:第1ボタンを標準ボタン、 0x100:第2ボタンを、 0x200:第3ボタン、 0x300:第4
return sc.Run('vbMsgBox', prompt, buttons, title)
},
inputbox:function(prompt, title, def){return sc.Run('vbInputBox', prompt, title, def)},
getAdminNames:function(PC名){var arr=sc.Run('getAdminMembers', PC名).split('\n'); arr.pop(); return arr},
setTerminate:function(fun){
var obj=sc.Run('getTerminater')
obj.setF(fun)
return obj
}
}
}()
AXO = function(n){return new ActiveXObject(n)}
fs = AXO('Scripting.FileSystemObject')
shell = AXO('WScript.Shell')
pathTMP = fs.GetSpecialFolder(2)+'\\'+(new Date()).getTime()
pathHTA = pathTMP+'.hta'
fs.CreateFolder(pathTMP)
ws = fs.CreateTextFile(pathHTA)
ws.Write((''+function(){/*
<html>
<title>一時フォルダ</title>
<style type="text/css">
.max{width:100%;height:100%}
.w-max{width:100%}
</style>
<body>
<table class=max>
<tr height=1>
<td>
<table class=w-max>
<td><input id=inp class=w-max value="-path-"></td>
<td width=1><button id=btn>開く</button></td>
</table>
</td>
</tr>
<tr><td><iframe id=ifr class=max style="border:solid 1px #000" src="-path-"></iframe></td></tr>
</table>
</body>
<script>
resizeTo(400,400)
btn.onclick=function(){
path = inp.value
;(new ActiveXObject('WScript.Shell')).run(0<path.indexOf(' ') ? ('"'+path+'"') : path)
}
</script>
</html>
*/}).split('/*')[1].split('*/')[0].replace(/-path-/g,pathTMP))
ws.Close()
while(true){
shell.run(pathHTA, 1, true)
if(vbs.msgbox('['+pathTMP+']を削除します', 1, '一時フォルダ')!=2){break}
}
fs.DeleteFolder(pathTMP)
fs.DeleteFile(pathHTA)

0 件のコメント:

コメントを投稿