利用JAVASCRIPT做个简单的文件管理器

<!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支持-->

用xytree 实现的文档管理器,可以随意增加/删除自己的文档节点. 可以在文档中添加图片,文件链接,可以给文字添加着重色,改变字体大小. 是一个灵巧的资料管理器. 解压缩后运行Index.hta 即可, 打开后参考里面的使用说明: 1. 添加节点: 在左边的文本框输入文档名称, 点击添加,即可增加新节点到当前目录下. 2. 删除节点: 勾上需要删除的节点,点击删除, 只会删除子节点, 不会删除父节点. 3. 改名字: 单击需要改名字的节点, 填写新名字, 点击"改名字". 4. 移动节点: 单击需要移动的节点, 低级"移动到...", 会弹出一个新窗口. 在新窗口中选择父节点, 将移动整个子节点树到新的父节点下. 5. 上下移动: 点击上箭头和下箭头来移动当前节点. 6. 添加文档内容: 选中节点,点击"编辑", 即可实现文本输入. 可以改变选中的文本的大小,颜色,状态等. 7. 添加链接: 在浏览窗口添加需要作为链接的文档或HTTP 地址, 点击添加. 8. 移除链接: 点击节点, 点击"移除". 9. 添加图片: 在浏览窗口选择图片, 点击"图片"按钮. 10.文档备份: 文档的所有修改会产生一个备份,位置在\Backup\下. 11.资源文件: 资源文件\xyTree\config.xml, 目录树位于此处. 请不要手动修改次文件, 以免造成不必要的数据丢失或者软件崩溃. 注意,根结点没有文档功能; 要改变字体颜色和显示状态, 必须先选中需要改变的文字. -- by talence@tom.com
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值