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.
- // this is a "load" module of a C# application for inventory
- // management. upon firing the "load" event from the
- // main menu screen, this module will test to see if an automatic
- // back up of the master inventory data file, "inventory.txt" and the
- // detail transaction file associated with each master inventory item
- // have been performed in the last 7 days. if not, then it will
- // automatically be performed before the main menu is accessible
- // to the user.
- private void FormGeneric_Load(object sender, System.EventArgs e)
- {
- // declare variables used in the "load" event of the main menu.
- 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;
- // size of each record in the back up log data file.
- const long BULEN = 32;
- // size of each record in the master inventory data file.
- const long MASTERLEN = 60;
- // size of each record in the transaction detail data file.
- const long DETAILEN = 75;
- // character arrays used to manipulate the data.
- 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;
- // set the boundaries of the menu screen.
- screen_width = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width;
- screen_height = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Height;
- this.SetBounds(1, 1, screen_width, screen_height);
- // set the start position of the menu screen to the center of the viewport.
- this.StartPosition = FormStartPosition.CenterScreen;
- // get the current working directory of the installation.
- currdir = Directory.GetCurrentDirectory();
- currdir = currdir.Substring(0, currdir.Length - 9);
- // get the date of the last auto back up event by opening
- // the back up log file, "bulog.txt".
- 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();
- // use the TryParse method to split the date portion of the
- // string "stringVal" into month, day and year components.
- // adjust the parsed year component for the 21st century.
- 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;
- // put the month, day and year components into a DateTime
- // object called "last_auto_backup_date".
- DateTime last_auto_backup_date = new DateTime(yearvar, monthvar, dayvar, 0, 0, 0);
- // subtract 7 days from today's date then see if it is greater than
- // or equal to the date of the last auto back up; if true then do another
- // one for today.
- 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)
- {
- .
- .
- .
- }

Douglas MillerPosted May 7, 2014, 11:33 PM
I will be updating this article soon with coding that has more descriptive variables indicative of what they do in the code. Just because I'm used to using arcane sounding, non-descriptive programming variables that doesn't mean the rest of the world is......sorry about that......I dropped the ball!
Douglas MillerPosted Apr 28, 2014, 7:56 PM
You are welcome.....glad you liked it!
Mahesh ChandPosted Apr 25, 2014, 1:51 PM
Thank you for sharing Doug.