Visual Data Backup using JavaScript [original]

Source: Internet
Author: User

Published on blog.csdn.net/zxub

Previously, a data backup function was provided. In general, we can use some visual programming tools to quickly implement it. I am using JavaScript to view partitions and directories. Here we only talk about directory backup, the directory to be backed up has already been set. The selected directory is to be backed up. To back up a single file, you can change several functions.
The Code is as follows:
<! 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> visual data backup using 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] + $/; // match any character string, including spaces, tabs, and page breaks, which is not a blank character.
If (patrn.exe C (s) return true
Else return false;
}
 
Function insertbutton ()
{
VaR STR = "", str2 = "";
STR = "<input type = 'button 'name = 'submit 'value = 'create a directory 'onclick = 'setnewfolder (view. value) 'style = 'font-size: 12px; '> & nbsp ;";
STR + = "<input type = 'button 'name = 'button2' value = 'Start backup 'onclick = 'gp _ popupconfirmmsg (\" OK in [\ "+ document. currentFolder + \ "] backup? \ "); If (document. mm_returnvalue) {bakfiles (view. Value)} 'style = 'font-size: 12px; '> & nbsp ;";
STR + = "<input type = 'button 'name = 'button3' value = 'start to restore 'onclick = 'gp _ popupconfirmmsg (\" confirm to restore to [\ "+ document. in CurrentFolder +? \ "); If (document. mm_returnvalue) {recoverfiles (view. Value)} 'style = 'font-size: 12px; '> & nbsp ;";
STR + = "<input type = 'button 'name = 'button4' value = 'Delete the backup 'onclick = 'gp _ popupconfirmmsg (\" confirm that the backup is deleted in [\ "+ document. in CurrentFolder +? \ "); If (document. mm_returnvalue) {deletebakfolder (view. Value)} 'style = 'font-size: 12px; '> ";
Window. tbutton. innerhtml = STR;
Str2 = "<SPAN class = 'style1'> the folder you selected is: </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) // determines whether a folder exists.
{
VaR FSO;
FSO = new activexobject ("scripting. FileSystemObject ");
If (FSO. folderexists (FLDR ))
{Return true ;}
Else
{Return false ;}
}

Function showparentfoldername (filespec) // display the parent directory
{
VaR FSO, S = "";
FSO = new activexobject ("scripting. FileSystemObject ");
S + = FSO. getparentfoldername (filespec );
Return (s );
}
Function showdrivelist () // display the partition structure
{
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> select the drive letter first </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) // display the directory structure
{
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) + "> double-click here to return to the upper-level directory </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 (\ "cannot select a directory containing spaces or the current directory has been deleted! \ ") ;}} 'Ondblclick = 'if (folderisexists (this. value) {document. currentFolder = This. value; view. value = CurrentFolder; showfolderlist (this. value)} else {If (this. value) Alert (\ "the selected directory cannot contain spaces. Please confirm! \ ")} '>" + Folderarr. Join () + "</SELECT> ";
}
Catch (E)
{
Alert ("No directory selected or the selected directory contains spaces. Please select again! ")}
}
Function setnewfile (PATH) // create a new file to confirm it is the folder where the backup is located
{
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) // Delete the backup folder
{
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 ("the directory where the backup is not to be deleted. Please confirm! ");
}
Function setnewfolder (PATH) // create a directory
{
VaR newfoldername;
VaR FSO = new activexobject ("scripting. FileSystemObject ");
Newfoldername = Window. Prompt ("Enter the new folder name :","");
Try {
If (legalinput (newfoldername) & (newfoldername ))
{
VaR folderfullpath = path + "\" + newfoldername;
If (FSO. folderexists (folderfullpath ))
{
Alert ("a folder with the same name already exists. Please change the folder name! ");
}
Else if (FSO. folderexists (PATH ))
{
FSO. createfolder (folderfullpath );
Alert ("the new folder has been created! ");
Showfolderlist (document. All. View. value );
}
Else
{
If (PATH)
{Alert ("the target folder has been deleted. Please confirm! ");
Showfolderlist (showparentfoldername (FSO. getdrive (FSO. getdrivename (PATH) + ":\\")));
}
Else alert ("the target folder cannot be blank. Please confirm! ");
}
}
Else if (newfoldername) Alert ("the name of the Input Folder cannot contain spaces. Please enter it again! ");
}
Catch (E)
{Alert ("the entered folder name contains invalid characters. Please enter it again! ")}
}
Function getsourcepath () // obtain the file path to be backed up.
{
Add the address selection function of the file to be backed up}

Function bakfiles (folderpath) // backup
{
Sourcepath = getsourcepath ();
VaR targetpath = "";
VaR today = new date ();
VaR FSO = "";
Targetpath = today. getyear () + "year" + (today. getmonth () + 1) + "month" + today. getdate () + "day" + today. gethours () + "-" + today. getminutes () + "-" + today. getseconds ();
Targetpath = folderpath + "//" + "data backup" + "_" + "(" + targetpath + ")";
FSO = new activexobject ("scripting. FileSystemObject ");
Try
{
If (FSO. folderexists (sourcepath ))
{
FSO. copyfolder (sourcepath, targetpath );
Setnewfile (targetpath );
Showfolderlist (showparentfoldername (targetpath ));
}
Else alert ("there is no directory for storing backup data in the system installation directory. Please find a backup folder to restore it! ");
}
Catch (E)
{Alert ("No directory selected, please confirm! ");}
}
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 ("data has been restored successfully! ");
}
Catch (E)
{
Alert ("because the directory where data is stored is being used, this directory is now closed. Please click the restore option again! ");
}
}
Else alert ("the restored directory selected is incorrect. Please select the correct backup file storage directory! ");
}

// -->
</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>
You can design the folder path you want to back up.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.