INTRODUCTION
One of the great things about programming code is the fact that it can be harnessed to automate small, but important chores that are often overlooked. This is especially critical in a business where one typically has too much to do with insufficient time to do it. Here, I will discuss C# code I used for an actual customer of mine. It selectively performs an automated data back up operation upon entering the inventory application I designed for them.
HAS A BACK UP OPERATION OCCURRED IN THE LAST 7 DAYS?
This automated data back up code is fired upon entering the Winform’s “Load Event”. After the initial chores are taken care of, the back up log data file, “bulog.txt”, will be accessed. The last appended record to the back up log will be read since it represents the last time an automated back up was performed. Next, the month/day/year components from the last back up date will be retrieved to construct a DateTime object I call “last_auto_backup_date”. The last back up date will be compared to the date 7 days prior to the current date. If the date from 7 days ago is greater than or equal to the last automated back up date, then a new automated back up operation will commence. The programming below illustrates how the date manipulation is done.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private void FormGeneric_Load(object sender, System.EventArgs e)
-
- {
-
-
-
- int screen_width, screen_height, yearvar, monthvar, dayvar, a;
-
- string stringVal, currdir, item_file_name, bkupDir, strmonthvar, strdayvar, string_file_name_backedup;
-
- string string_file_name;
-
- string string_auto_backup_date, string_auto_backup_description, stringError;
-
- long sizeofdatafile, sizeofdatafile2, datafileoffset, datafileoffset2, filelengthvar;
-
-
-
- const long BULEN = 32;
-
-
-
- const long MASTERLEN = 60;
-
-
-
- const long DETAILEN = 75;
-
-
-
- char[] recordatavar = new char[MASTERLEN - 2];
-
- char[] recordatavar_transactions = new char[DETAILEN - 2];
-
- char[] backup_log_data = new char[BULEN - 2];
-
- char[] char_auto_backup_date, char_auto_backup_description;
-
-
-
- screen_width = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width;
-
- screen_height = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Height;
-
- this.SetBounds(1, 1, screen_width, screen_height);
-
-
-
- this.StartPosition = FormStartPosition.CenterScreen;
-
-
-
- currdir = Directory.GetCurrentDirectory();
-
- currdir = currdir.Substring(0, currdir.Length - 9);
-
-
-
-
-
- FileInfo datafileinfo = new FileInfo(currdir + "bulog.txt");
-
- sizeofdatafile = datafileinfo.Length;
-
- StreamReader streamobj = new StreamReader(currdir + "bulog.txt");
-
- streamobj.BaseStream.Seek(sizeofdatafile - BULEN, SeekOrigin.Begin);
-
- stringVal = streamobj.ReadLine();
-
- streamobj.Close();
-
-
-
-
-
-
-
- int.TryParse(stringVal.Substring(0, 2), out monthvar);
-
- int.TryParse(stringVal.Substring(3, 2), out dayvar);
-
- int.TryParse(stringVal.Substring(6, 2), out yearvar);
-
- yearvar = yearvar + 2000;
-
-
-
-
-
- DateTime last_auto_backup_date = new DateTime(yearvar, monthvar, dayvar, 0, 0, 0);
-
-
-
-
-
-
-
- System.DateTime today = System.DateTime.Now;
-
- System.TimeSpan duration = new System.TimeSpan(7, 0, 0, 0);
-
- System.DateTime seven_days_prior_date = today.Subtract(duration);
-
- if (seven_days_prior_date >= last_auto_backup_date)
-
- {
-
- .
- .
- .
-
- }
PREP WORK FOR FILE NAMING CONVENTION IS NEEDED TO PREVENT OVERWRITES ON EXISTING BACKED UP DATA FILESBefore the actual data back up operation can begin, a little preparation work is needed. The backed up file name for the master inventory data file and each of its detail inventory data files must be constructed. This must be done in a way that makes them unique so they won’t overwrite any other master inventory data file or detail inventory data files in the target back up directory. For the master inventory data file, I concatenate the string “inventory_” to a 2 character current month string followed by a 2 character current day string and then a 4 character current year string and then the string “.txt”. For example, if today’s date is 02/06/2013 and an automatic back up operation is performed, then the master inventory file name in the target back up directory would be “inventory_02062013.txt”. A similar methodology is used to name the detail inventory data files that are being copied. Let’s say the detail inventory data file name as found in a given master inventory record is “item0012”. Using our example, its counterpart back up data file in the target back up directory would be “item0012_02062013.txt”. Incorporating the current date into the back up data file’s naming convention ensures that it will always be unique and no previously backed up data files will ever be overwritten.
LOOP AROUND THE MASTER INVENTORY DATA FILE TO PROCESS EACH DETAIL INVENTORY DATA FILE
The first phase of the back up operation will be to back up the master inventory data file itself to the target directory. The second phase will sequentially loop around the source master inventory data file to retrieve each of the file names of the detail inventory data files. Each detail inventory data file contains the inventory transactions that occurred for a particular part number. Before this data file will be copied to the target destination, the program will verify that it does exist on the source drive. If this is not true, the program will advance to the next sequential detail inventory data file that is contained in the master inventory data file.
After verification, the source detail inventory data file will be written to its counterpart back up detail inventory data file in the target back up directory. After the source detail inventory data file has been written, its file stream will be closed. Then the program will repeat this for each successive inventory detail transaction file. Lastly, the source master inventory data file’s stream will then be closed after the last detail transaction data file has been backed up.
HANDLING CORRUPTED DETAIL INVENTORY DATA FILES
Each detail inventory data file size will be measured to ensure that each of its records are of "DETAILEN" size. This is a methodology I use to make sure no extra/errant characters have been inserted into a given data file. This would throw a wrench into the back up process and make the back up operation abort. If the code does detect an erroneous record, the file name is then concatenated to a variable I call "stringError". If there is a problem with one or more detail transaction data files, then this variable will be displayed as a message at the end of processing. Also, the back up log data file will not be updated with a successful back up operation record. Please click here to see the entire code readout.
UPDATE THE BACK UP LOG DATA FILE
After the automated back up has been completed, the program will note this in the back up log data file on the assumption no corrupted detail inventory data files were detected during the back up operation. If even one is detected, the back up log will not be updated and instead a message will appear on screen noting the detail inventory data file(s) that were not processed due to the presence of non-homogenous or errant data records that were encountered during processing.
The back up log is updated so future date comparisons between the date of the last back up operation and 7 days prior to the current date will be accurate. This is actually a pretty simple task. The string "AUTO-BACKUP__PERFORMED" will be concatenated to the end of the string equivalent of the current date. This information will then be appended to the end of the back up log data file, “bulog.txt”.
CONCLUSION
Software development is a vehicle to increased efficiency in the workplace. Given today’s demands on staff and their limited time, manual methods of getting tasks done even on a computer may not be the best choice. My automated back up code for the inventory application I created is just one example of how programming can be used to liberate employees from mundane tasks so they can focus on more important issues at hand.