2004.10.23發表於blog.csdn.net/zxub
前些時候,做了個資料備份功能。一般的話,我們用一些可視化編程工具很快就可以實現,我做的是用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;'> ";
str+="<input type='button' name='button2' value='開始備份' onClick='GP_popupConfirmMsg(\"確認在 [ \"+document.CurrentFolder+\" ] 下備份嗎?\");if (document.MM_returnValue) {bakFiles(view.value)}' style='FONT-SIZE: 12px;'> ";
str+="<input type='button' name='button3' value='開始還原' onClick='GP_popupConfirmMsg(\"確認還原為 [ \"+document.CurrentFolder+\" ] 中的檔案嗎?\");if (document.MM_returnValue) {RecoverFiles(view.value)}' style='FONT-SIZE: 12px;'> ";
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" > </td>
</tr>
<tr>
<td width="406" id="Drv"> </td>
</tr>
<tr>
<td height="5"></td>
</tr>
<tr>
<td id="Folder"> </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>
自行設計好自己要備份的檔案夾路徑就可以了。