Sunday, May 8, 2011

How to Backup Using Visual Basic


1. Open Visual Basic (VB) and create a project using the “Standard EXE” template. From the top level VB menu, click on “File” and then “Save Project As.” Save both the form and project with the name “MyBackup”. Click “Project” from the top menu and select “Components.” Scroll down the list of components until you find “Microsoft Common Dialog Control 6.0.” Click on the check box next to it to add this control to the ToolBox.
2. Add the following controls to the form by double-clicking on them in the ToolBox on the left of the screen:<br /><br />Two Labels (icon is a large letter A)<br />Two CommandButtons (icon is a small rectangle)<br />One Frame (small square with tiny letters “xy”)<br />One each DriveListBox (small rectangle with dotted line), DirListBox (folder image) and FileListBox (tiny page with corner turned down and horizontal lines)<br />One CommonDialog control (small rectangle with what looks like a floppy disk)<br /><br />Drag these around on the form so first label is at the top, the three list boxes are below this, lined up horizontally and within the Frame, the two CommandButtons are below the Frame and aligned horizontally, and the final label is below everything else. The CommonDialog control can be in any open space on the form since it does not display at run time.
3. Click on each item in turn to open its properties in the right hand panel. Change the Caption properties to read as follows:<br /><br />Label1: Navigate to your original file below and highlight it, then click on Backup.<br />Command1: Backup<br />Command2: Exit<br />Frame1: Drive/Folder Contents<br />Label2: blank, that is, delete the Caption
4. Click on “View” and then “Code” in the top level VB Menu. Type the following lines of code exactly as they appear below:<br /><br />Option Explicit<br />Private Sub Command2_Click()<br />End<br />End Sub<br />Private Sub Form_Load()<br /> MyBackup.Show<br />End Sub<br />Private Sub Dir1_Change()<br /> File1.Path = Dir1.Path<br />End Sub<br />Private Sub Command1_Click()<br />Dim strSourceFile, strDestinationFile, strFolder1, strFolder2 As String<br />CommonDialog1.Filter = \"Office Files |*.doc|*.docx| *.xls| *.xlsx|*.txt|\"<br /> strFolder1 = Dir1.Path<br /> strSourceFile = File1.FileName<br /> strSourceFile = strFolder1 & \"\\\" & strSourceFile<br /> strFolder2 = Dir1.Path<br /> strDestinationFile = InputBox(\"Enter file name for the backup\")<br /> strDestinationFile = strFolder2 & \"\\\" & strDestinationFile & \".bak\"<br /> FileCopy strSourceFile, strDestinationFile<br /> lblBackup.Visible = True<br />lblBackup.Caption = \"Your file has been backed up with the name \" & strDestinationFile <br />End Sub<br />Private Sub btnCancel_Click()<br /> Unload Me<br />End Sub<br />Private Sub Drive1_Change()<br />Dir1.Path = Drive1.Drive<br />End Sub<br />Private Sub File1_Change()<br /> File1.SetFocus<br />End Sub
5. Click “Project” in the top level VB menu and then “MyBackup Properties.” In the box next to “Standard EXE” with the heading “Startup Object,” use the pull-down menu to select “MyBackup.” Click “OK” and then press “F5” to run and test the program. If it works, then click “File” and “Make MyBackup.exe” to compile it into a full application. If there are problems, press “F8” to step through the lines one at a time to identify the line in error and correct it as above.