<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN " "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd ">
<HTML xmlns= "http://www.w3.org/1999/xhtml ">
<HEAD>
<TITLE> 居于JavaScript 文件管理器 </TITLE>
<META http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<META content= "MSHTML 6.00.5730.11 " name= "GENERATOR ">
<style>
table{ background-color:#ffff00; }
th { background-color:#ff0000; }
tr { background-color:#ffffff; }
#Layer2{
position:absolute;
background:#ffffff;
left:0px;
top:0px;
width:100%;
height:100%;
z-index:5;
}
</style>
<script language= "JavaScript ">
var MyDir = "E://temp// "; //设置管理目录
var MyFile = " ";
function delfile(s){ //删除文件函数
var fso=new ActiveXObject( "Scripting.FileSystemObject ");
var rfile = s;
rfile = rfile.replace(g, " ");
var mdir = getPath(rfile);
try{
fso.DeleteFile(unescape(rfile));
alert( "删除文件成功 ");
}catch(e){
alert( "删除文件失败 ");
}
readDirs(mdir);
}
function deldir(s){ //删除目录函数
var fso=new ActiveXObject( "Scripting.FileSystemObject ");
var rfile = unescape(s);
rfile = rfile.replace(g, " ");
var mdir = getPath(rfile);
try{
fso.deleteFolder(rfile);
alert( "删除目录成功 ");
}catch(e){
alert( "删除目录失败 ");
}
readDirs(mdir);
}
function renameDir(s){ //重命名目录函数
var newname = prompt( "更改名称: /n 请输入新名称,JavaScript将为你更改名称 ", "newname ");
var mdir = " ";
if(newname != " " && newname != null){
var fso=new ActiveXObject( "Scripting.FileSystemObject ");
var rfile = unescape(s);
rfile = rfile.replace(g, "/ ");
var Folder=fso.getFolder(rfile);
newname = mdir +newname;
newname = newname.replace(g, "/ ");
try{
Folder.Name = newname;
alert( "重命名目录成功 ");
}catch(e){ alert( "重命名目录失败 "); }
mdir = document.getElementById( 'showpath ').innerHTML;
readDirs(mdir);
}
}
function renameFile(s){ //重命名文件函数
var newname = prompt( "更改名称: /n 请输入新名称,JavaScript将为你更改名称 ", "newname ");
var mdir = " ";
if(newname != " " && newname != null){
var fso=new ActiveXObject( "Scripting.FileSystemObject ");
var rfile = s;
rfile = rfile.replace(g, "/ ");
var Folder=fso.getFile(rfile);
newname = mdir +unescape(newname);
newname = newname.replace(g, "/ ");
try{
Folder.Name = newname;
alert( "重命名文件成功 ");
}catch(e){ alert( "重命名文件失败 "); }
mdir = document.getElementById( 'showpath ').innerHTML;
readDirs(mdir);
}
}
function newDir(){ //创建目录函数
var newname = prompt( "请输入新名称,JavaScript创建新目录 ", "newname ");
if(newname != " " && newname != null){
var mpath = document.getElementById( 'showpath ').innerHTML;
mpath = mpath.replace(g, "// ");
newname = mpath + "// " + unescape(newname);
newname = newname.replace(//g, "// ");
var mdir = getPath(newname);
fso=new ActiveXObject( "Scripting.FileSystemObject ");
try{
fso.CreateFolder(newname);
alert( "创建目录成功 ");
}catch(e){ alert( "创建目录失败 "); }
readDirs(mdir);
}
}
function newFile(){ //创建文件函数
var newname = prompt( "请输入新名称,JavaScript创建新文件 ", "newname ");
if(newname != " " && newname != null){
var mpath = document.getElementById( 'showpath ').innerHTML;
mpath = mpath.replace(g, "// ");
newname = mpath + "// " + unescape(newname);
newname = newname.replace(//g, "// ");
var mdir = getPath(newname);
fso=new ActiveXObject( "Scripting.FileSystemObject ");
try{
f1=fso.CreateTextFile(newname,true);
try{
f1.WriteLine(document.getElementById( 'filecontent ').value);
}catch(e){
f1.WriteLine( "JavaScript Hello World! ");
}
f1.WriteBlankLines(1);
document.getElementById( 'marea ').style.zIndex = 1;
f1.Close();
alert( "创建文件成功 ");
}catch(e){ alert( "创建文件失败 "); }
readDirs(mdir);
}
}
function readDirs(path){ //读取文件目录列表
var fso = new ActiveXObject( "Scripting.FileSystemObject ");
var f = fso.GetFolder(path);
var fc = new Enumerator(f.files);
var r = g;
var rfile = " ";
var rpath = " ";
var mpath = path;
MyFile = " ";
if(path == this.MyDir){
path = this.MyDir;
path = path.replace(r, " ");
}else{
path = getpDir(path);
}
document.getElementById( 'showdir ').innerHTML = " ";
this.MyFile += " <table width=/ "100%/ " border=/ "0/ " cellSpacing=/ "1/ " cellPadding=/ "1/ "> /n ";
this.MyFile += " <tr> ";
this.MyFile += " <th> <a href=/ "#/ " onClick=/ "readDirs( ' "+ path + " ')/ " title=/ "上一级/ "> 上一级 </a> ";
this.MyFile += " </th> <th> 名称[ <a href=/ "#/ " onClick=/ "CreateFile();/ "> 新建文件 </a> ][ <a href=/ "#/ " onClick=/ "newDir();/ "> 新建目录 </a> ] </th> ";
this.MyFile += " <th> 动作 </th> ";
this.MyFile += " </tr> /n ";
fk = new Enumerator(f.SubFolders);
for (; !fk.atEnd(); fk.moveNext()){
rpath = fk.item();
rpath = unescape(rpath);
rpath = rpath.replace(r, " ");
this.MyFile += " <tr> <td> DIR </td> /n ";
this.MyFile += " <td> <a href=/ "#/ " onClick=/ "readDirs( ' " +rpath + " ');/ "> " + fk.item() + " </a> </td> /n ";
this.MyFile += " <td> <a href=/ "#/ " onClick=/ "deldir( ' " +rpath + " ');/ "> 删除 </a> |/n ";
this.MyFile += " <a href=/ "#/ " onClick=/ "renameDir( ' " +rpath + " ');/ "> 重命名 </a> |/n ";
this.MyFile += " <a href=/ "#/ " onClick=/ "readDirs( ' " +rpath + " ');/ "> 浏览 </a> /n ";
this.MyFile += " </td> </tr> /n ";
}
for (; !fc.atEnd(); fc.moveNext()){
rfile = fc.item();
rfile = unescape(rfile);
rfile = rfile.replace(r, " ");
this.MyFile += " <tr> <td> File </td> /n ";
this.MyFile += " <td> " + fc.item() + " </td> /n ";
this.MyFile += " <td> <a href=/ "#/ " onClick=/ "delfile( ' " +rfile + " ');/ "> 删除 </a> |/n ";
this.MyFile += " <a href=/ "#/ " onClick=/ "readfiles( ' " +rfile + " ');/ "> 编辑 </a> |/n ";
this.MyFile += " <a href=/ "#/ " onClick=/ "renameFile( ' " +rfile + " ');/ "> 重命名 </a> |/n ";
this.MyFile += " <a href=/ "file:// " +rfile.replace(//g, "// ") + "/ " target=/ "Fview/ "> 浏览 </a> /n ";
this.MyFile += " </td> </tr> /n ";
}
this.MyFile += " </table> ";
path = path.replace(//g, "// ");
document.getElementById( 'showdir ').innerHTML = MyFile;
document.getElementById( "showpath ").innerHTML = mpath;
}
function getpDir(s){ //获取父目录函数
var r, re;
var s = unescape(s);
var pdir = " ";
re = g;
r = s.split(re);
for(i=0;i <r.length-1;i++){
pdir += r[i] + " ";
}
return pdir;
}
function getPath(s){ //格式化路径
var r, re;
var s = unescape(s);
var pdir = " ";
re = g;
r = s.split(re);
for(i=0;i <r.length-1;i++){
pdir += r[i] + "// ";
}
return pdir;
}
function CreateFile(){ //显示文件编辑区
document.getElementById( 'marea ').style.zIndex = 10;
}
function OffFile(){ //关闭目录编辑区
document.getElementById( 'marea ').style.zIndex = 1;
document.getElementById( 'marea1 ').style.zIndex = 2;
}
function readfiles(s){ //读取文件编缉
var ForReading=1;
var getFile;
var message;
var fso = " ";
var ts = " ";
var str = " ";
fso=new ActiveXObject( "Scripting.FileSystemObject ");
var ms = fso.GetFile(s);
ts=fso.OpenTextFile(s,ForReading);
message = "[FileName: " + ms.Name + "][Filesize: " + ms.size + " bytes] ";
str=ts.ReadAll();
document.getElementById( 'marea1 ').style.zIndex = 10;
document.getElementById( 'fmessage ').innerHTML = message;
document.getElementById( 'filecontent1 ').value = unescape(str);
document.getElementById( 'mpath ').value = unescape(s);
}
function writefiles(){ //写入文件
var ts = " ";
var s = document.getElementById( 'mpath ').value;
s = s.replace(g, " ");
fso=new ActiveXObject( "Scripting.FileSystemObject ");
ForAppending = 2;
try{
ts =fso.OpenTextFile(unescape(s),ForAppending,false);
ts.WriteLine(unescape(document.getElementById( 'filecontent1 ').value));
document.getElementById( 'marea1 ').style.zIndex = 2;
alert( "更改文件成功 ");
ts.close();
}catch(e){
alert( "更改文件失败 ");
}
}
</script>
</HEAD>
<!--接上面生成一文件-->
<BODY topmargin= "0 " leftmargin= "0 " rightmargin= "0 ">
<div id= "marea " style= "position:absolute; width:100%; height:100%; left:0px; top:0px; z-index:1; " align= "center ">
<form name= "form1 " method= "post " action= " ">
<textarea name= "filecontent " id= "filecontent " style= "width:100%;height:500px; "> </textarea>
<div id= "submitbutton " style= "background-color:#ffffff; ">
<input type= "button " name= "button1 " value= "关闭 " onClick= "OffFile(); " />
<input type= "button " name= "button2 " value= "提交 " onClick= "newFile(); "/>
</div>
</form> </div>
<div id= "marea1 " style= "position:absolute; width:100%; height:100%; left:0px; top:0px; z-index:2; " align= "center ">
<form name= "form2 " method= "post " action= " ">
<textarea name= "filecontent1 " id= "filecontent1 " style= "width:100%;height:500px; "> </textarea>
<div id= "submitbutton1 " style= "background-color:#ffffff; ">
<input type= "button " name= "button3 " value= "关闭 " onClick= "OffFile(); " />
<input type= "button " name= "button4 " value= "提交 " onClick= "writefiles(); "/>
<input type= "hidden " name= "mpath " id= "mpath " value= " " />
<span id= "fmessage "> </span>
</div>
</form> </div>
<div id= "Layer2 ">
<div id= "showdir "> </div>
当前路径: <span id= "showpath "> </span>
<script language= "javascript ">
readDirs(MyDir); //读取文件、目录列表
</script>
</div>
</body>
</html>
<!-- 上面是我写出来的源码,如果对大家有帮助请给我加一点分,在IE、傲游测试通过,前提必须要有FSO支持-->