CSDN博客

img zxub

利用javascript实现可视化数据备份[原创]

发表于2004/10/23 14:37:00  1144人阅读

分类: javascript

  前些时候,做了个数据备份功能。一般的话,我们用一些可视化编程工具很快就可以实现,我做的是用javascript实现分区、目录的查看,这里只讲目录备份,且要备份的目录已经定了,选取的是要备份到的地方,要实现单个文件的备份,可以改几个函数实现,这里不多说。
  废话不多说了,代码如下:
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>用javascript实现可视化数据备份</title>
<script language="JavaScript" type="text/JavaScript">
<!--
function GP_popupConfirmMsg(msg) { //v1.0
  document.MM_returnValue = confirm(msg);
}
function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}
//-->
</script>
<script language="JavaScript" type="text/JavaScript">
<!--
function LegalInput(s)
    {
    var patrn=/^[^/s]+$/;   //匹配任何头尾不是空白字符,包括空格、制表符、换页符等所形成的字符串.
 if (patrn.exec(s)) return true
 else return false; 
    }
 
function insertButton()
{
 var str="",str2="";
 str="<input type='button' name='Submit' value='新建目录' onClick='SetNewFolder(view.value)' style='FONT-SIZE: 12px;'>&nbsp;&nbsp;";
 str+="<input type='button' name='button2' value='开始备份' onClick='GP_popupConfirmMsg(/"确认在 [ /"+document.CurrentFolder+/" ] 下备份吗?/");if (document.MM_returnValue) {bakFiles(view.value)}' style='FONT-SIZE: 12px;'>&nbsp;&nbsp;";
 str+="<input type='button' name='button3' value='开始还原' onClick='GP_popupConfirmMsg(/"确认还原为 [ /"+document.CurrentFolder+/" ] 中的文件吗?/");if (document.MM_returnValue) {RecoverFiles(view.value)}' style='FONT-SIZE: 12px;'>&nbsp;&nbsp;";
 str+="<input type='button' name='button4' value='删除该备份' onClick='GP_popupConfirmMsg(/"确认删除备份在 [ /"+document.CurrentFolder+/" ] 中的文件吗?/");if (document.MM_returnValue) {DeleteBakFolder(view.value)}' style='FONT-SIZE: 12px;'>";
 window.tbutton.innerHTML=str;
 str2="<span class='style1'>您所选择的文件夹为:</span>      <input name='view' type='text' id='view' size='71' readonly='true' style='border:1px black solid;FONT-SIZE: 12px;color:#336699;background-color:#F7F3DE'>";
 window.ShowView.innerHTML=str2;
}
function FolderIsExists(fldr)  //判断文件夹是否存在
{
   var fso;
   fso = new ActiveXObject("Scripting.FileSystemObject");
   if (fso.FolderExists(fldr))
    { return true;}
  else
    { return false;}     
}

function ShowParentFolderName(filespec)    //显示上级目录
   {
     var fso, s = "";
     fso = new ActiveXObject("Scripting.FileSystemObject");
     s += fso.GetParentFolderName(filespec);
     return(s);
   }
function ShowDriveList()    //显示分区结构
{
   var fso, s, n, e, x,drvarr,i;
   document.CurrentFolder="";
   i=0;
   drvarr=new Array(27);
   fso = new ActiveXObject("Scripting.FileSystemObject");
   e = new Enumerator(fso.Drives);
   s = "";
   drvarr[i]="<OPTION value='' selected>请先选择驱动器盘符</OPTION>";
   i+=1;
   for (; !e.atEnd(); e.moveNext())
   {
      x = e.item();
   s="";
      s = s + x.DriveLetter;
      s += " - ";
      if (x.IsReady)
       {
   n = x.VolumeName;
         s +=n;
      drvarr[i] ="<OPTION value="+x.DriveLetter+"://"+">"+s+"</OPTION>";
      i+=1;
    }   
   }
   window.Drv.innerHTML="<SELECT ID='DrvList' style='width:150px;FONT-SIZE: 12px;color:#336699;background-color:#F7F3DE' onChange='if (this.value) {insertButton();view.value=this.value;document.CurrentFolder=this.value;ShowFolderList(this.value)}' >"+drvarr.join()+"</SELECT>"
}
 function ShowFolderList(folderspec)    //显示目录结构
 {
   var fso, f, fc, s,folderarr,i;
   try {
     fso = new ActiveXObject("Scripting.FileSystemObject");
     f = fso.GetFolder(folderspec);
     fc = new Enumerator(f.SubFolders);
     s = "";
     i=0;
     folderarr=new Array();
     if (ShowParentFolderName(document.CurrentFolder))
     {
       folderarr[i] ="<OPTION value="+ShowParentFolderName(document.CurrentFolder)+">双击这里返回上一级目录</OPTION>";
       i+=1;
     }
     for (; !fc.atEnd(); fc.moveNext())
     {
      s=fc.item();
   folderarr[i] ="<OPTION value="+s+">"+s+"</OPTION>";
   i+=1;  
     }
        window.Folder.innerHTML="<SELECT ID='FoldList' size='20' style='width:560px;FONT-SIZE: 12px;color:#336699;background-color:#F7F3DE' onClick='if (FolderIsExists(this.value)) {document.CurrentFolder=this.value;view.value=CurrentFolder;} else {if (this.value) {alert(/"不能选择含有空格的目录或当前目录已被删除!/");}}' onDblClick='if (FolderIsExists(this.value)) {document.CurrentFolder=this.value;view.value=CurrentFolder;ShowFolderList(this.value)} else {if (this.value) alert(/"所选择的目录不能含有空格,请确认!/")}'>"+folderarr.join()+"</SELECT>";
  }
 catch (e)
   {
      alert("没选择目录或所选择的目录含有空格,请重新选择!")}
}
 function SetNewFile(Path)    //新建立一个文件,以确认是备份所在文件夹
   {
      var fso = new ActiveXObject("Scripting.FileSystemObject");
   var TargetFile=Path+"//RP_Bak.confirm";
      var a = fso.CreateTextFile(TargetFile, true);
      a.WriteLine("This is the RP_Bak confirm.");
      a.Close();
 }
 function DeleteBakFolder(Path)     //删除备份文件夹
   {
      var fso = new ActiveXObject("Scripting.FileSystemObject");
   var ConfirmFile=Path+"//RP_Bak.confirm";
   if (fso.FileExists(ConfirmFile))
     {
    fso.DeleteFolder(Path,true);
    document.CurrentFolder=ShowParentFolderName(Path)
    ShowFolderList(document.CurrentFolder);
    document.all.view.value=document.CurrentFolder;
   }
      else alert("所要删除的不是备份所在的目录,请确认!");  
   }
 function SetNewFolder(Path)    //新建目录
 {
   var NewFolderName; 
   var fso = new ActiveXObject("Scripting.FileSystemObject");
   NewFolderName=window.prompt("请输入新文件夹名称:","");
   try{
    if (LegalInput(NewFolderName) && (NewFolderName))
    {
     var FolderFullPath=Path+"//"+NewFolderName;
    if (fso.FolderExists(FolderFullPath))
   {
     alert("已经存在同名文件夹,请更换文件夹名称!");
   }
     else if (fso.FolderExists(Path))
    {
      fso.CreateFolder(FolderFullPath);
      alert("新文件夹已经创建!");
      ShowFolderList(document.all.view.value);
    }
    else
    {
    if (Path)
      {alert("目标文件夹已被删除,请确认!");
       ShowFolderList(ShowParentFolderName(fso.GetDrive(fso.GetDriveName(path)+"://")));
      }
     else alert("目标文件夹不能为空,请确认!");
     }     
  }
  else if (NewFolderName) alert("输入的文件夹名称不能含有空格,请重新输入!");
 }
 catch (e)
 { alert("输入的文件夹名称含有非法字符,请重新输入!")}
 }
 function GetSourcePath()    //取得要备份的文件路径.
    {
     这里加上自己要备份的文件的地址选取函数    }

function bakFiles(folderpath)    //备份
  {
 SourcePath=GetSourcePath();
 var targetPath="";
 var today=new Date();
 var fso="";
 targetPath=today.getYear()+"年"+(today.getMonth()+1)+"月"+today.getDate()+"日"+today.getHours()+"-"+today.getMinutes()+"-"+today.getSeconds();
 targetPath=folderpath+"//"+"数据备份"+"_"+"("+targetPath+")";   
    fso = new ActiveXObject("Scripting.FileSystemObject");
 try
 {
  if (fso.FolderExists(SourcePath))
    {
   fso.CopyFolder(SourcePath, targetPath);
   SetNewFile(targetPath);
   ShowFolderList(ShowParentFolderName(targetPath));
    }
   else alert("系统安装目录中已无要备份数据存放目录,请找个备份文件夹进行还原!");
 }
 catch(e)
 { alert("没选择目录,请确认!");}
  }
  function RecoverFiles(folderpath)
  {
    var fso = new ActiveXObject("Scripting.FileSystemObject");
 var ConfirmFile=folderpath+"//RP_Bak.confirm";
 if (fso.FileExists(ConfirmFile))
  {
       TargetPath=GetSourcePath();
    try
    {
     if (fso.FolderExists(TargetPath))
   {
     fso.DeleteFolder(TargetPath,true);
   }   
     fso.CreateFolder(TargetPath);
     fso.CopyFolder(folderpath, TargetPath);
     alert("数据已成功还原!");
  }
  catch(e)
  {
     alert("由于数据存放的目录正被使用,现将该目录关闭,请再点击还原选项!");   
  }
   }
  else alert("所选择的还原目录有误,请选择正确的备份文件存放目录!");
  }
  
//-->
</script>
<style type="text/css">
<!--
.style1 {
 font-size: 12px;
 color: #336699;
}
-->
</style>
</head>

<body onLoad="ShowDriveList()">
<table width="560"  border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="40" >&nbsp;</td>
  </tr>
  <tr>
    <td width="406" id="Drv">&nbsp;</td>
  </tr>
  <tr>
    <td height="5"></td>
  </tr>
  <tr>
    <td id="Folder">&nbsp;</td>
  </tr>
  <tr>
    <td height="5"></td>
  </tr>
  <tr>
    <td id="ShowView"></td>
  </tr>
  <tr>
    <td height="30" valign="bottom" id="tbutton"></td>
  </tr>
</table>
</body>
</html>
  自行设计好自己要备份的文件夹路径就可以了。

阅读全文
0 0

相关文章推荐

img
取 消
img