Created
March 17, 2015 06:07
-
-
Save dnasca/eb2277c7b0661f8ab287 to your computer and use it in GitHub Desktop.
a little console backup program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Simple File Copy\n"); | |
Console.WriteLine("Any files that have been modified in source directory within last 24 hours will be backed up.\n"); | |
// ask the user for the source directory, store it as a string | |
Console.Write("What directory path would you like to backup from?: "); | |
var sourcePath = Console.ReadLine() + @"\"; | |
// ask the user for the destination directory, store it as a string | |
Console.Write("\nWhat directory path would you like to backup to?: "); | |
var targetPath = Console.ReadLine() + @"\"; | |
Console.WriteLine("\nSource Directory: {0}", sourcePath); | |
Console.WriteLine("Target Directory: {0}\n", targetPath); | |
// create an array of files from the sourcePath and store as strings | |
string[] filesInThisDirectory = Directory.GetFiles(sourcePath); | |
// iterate through every element in the filesInThisDirectory array | |
foreach (var file in filesInThisDirectory) | |
{ | |
// call the method that evaluates if a file has been recently modified | |
if (!HasFileBeenModified(file)) continue; // if bool method returns false, break out of foreach loop | |
// if bool method returns true, proceed through the loop body as normal | |
var fileName = Path.GetFileName(file); // create a reference to the file (in the array) the loop is currently on | |
Console.WriteLine("Copying: {0}", fileName); | |
File.Copy(file, targetPath + fileName, true); // if we've gotten this far, it's because WasFileRecentlyModified was returned as true, | |
// so let's copy the file and go to the next element in the array | |
} | |
Console.WriteLine("\nAll files modified within the last 24 hours have been copied to the destination directory ... Press any key to exit."); | |
Console.ReadKey(); | |
} | |
// create a method that returns bool after evaluating if a file has been recently modified | |
public static bool HasFileBeenModified(string fileName) | |
{ | |
var fileNameInfo = new FileInfo(fileName); // initializes a new instance of the FileInfo class (acts like a wrapper for the file path) | |
// create a reference to the value of the last time the file was modified | |
var fileModifiedTime = fileNameInfo.LastWriteTimeUtc; | |
//create a reference to the time of 'right now' | |
var rightNow = DateTimeOffset.UtcNow; // UTC unambiguously identifies the instant in time that the value represents | |
// DateTimeOffsetUtc.Now will give us a value that represents the local time on the computer | |
// where the code is running -- the offset we get will tell us how that value relates to UTC, | |
// and will yield a different offset when in standard time or Daylight Savings Time. | |
// compare the files last modified time to the current time | |
return fileModifiedTime.AddDays(1) >= rightNow; // The result of this question will return true or false. | |
// The Yes/No question were asking is: If the file's last | |
// modified timestamp had 24 hours added to its value, | |
// would that value be greater(or equal) to the value of 'right now'? | |
// If it is greater(or equal), then return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment