Create basic backup file rotation functions and direcories.

This commit is contained in:
Tim Young 2017-08-05 12:41:42 -05:00
parent 362b3a60a3
commit 22552ce1a9

View File

@ -1010,6 +1010,68 @@ namespace EduNetworkBuilder
return OFD; return OFD;
} }
/// <summary>
/// Move a file out of the way, making a rotating backup at the same time.
/// </summary>
/// <param name="SourceFileWithDir">A source filename with a directory</param>
/// <param name="DestDir">The directory where to put the file</param>
/// <param name="rotation">The number of rotation backups to make</param>
public static void MoveFileWithRotation(string SourceFileWithDir, string DestDir, int rotation)
{
if (!File.Exists(SourceFileWithDir)) return; //we do not rotate if the source does not exist
if (!Directory.Exists(DestDir))
Directory.CreateDirectory(DestDir);
//Rotate the old file first, then move the new one over.
string dest_file = Path.Combine(DestDir, Path.GetFileName(SourceFileWithDir));
RotateFile(dest_file, rotation);
File.Move(SourceFileWithDir, dest_file);
}
/// <summary>
/// Move a file out of the way, making a rotating backup at the same time. backup to appdata
/// </summary>
/// <param name="SourceFileWithDir">A source filename with a directory</param>
/// <param name="rotation">The number of rotation backups to make</param>
public static void MoveFileWithRotation(string SourceFileWithDir, int rotation)
{
string StandardDest = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "EduNetworkBuilder");
MoveFileWithRotation(SourceFileWithDir, StandardDest, rotation);
}
/// <summary>
/// Given a file, rotate it with the specified number of versions.
/// </summary>
/// <param name="filename"></param>
/// <param name="count"></param>
public static void RotateFile(string filename, int count)
{
if (count < 1) return;
string currentfile = GenFilenameWithInt(filename, count);
string rotatefile;
if (File.Exists(currentfile)) File.Delete(currentfile); //The last rotation gets deleted
for(int i=count -1; i >= 1; i--)
{
currentfile = GenFilenameWithInt(filename, i);
rotatefile = GenFilenameWithInt(filename, i + 1);
if (File.Exists(currentfile))
File.Move(currentfile, rotatefile);
}
//The final case is when there is no version number on the file.
currentfile = filename;
rotatefile = GenFilenameWithInt(filename, 1);
if (File.Exists(currentfile))
File.Move(currentfile, rotatefile);
}
private static string GenFilenameWithInt(string filename, int version)
{
string FileOnly = Path.GetFileNameWithoutExtension(filename);
string FileWithInt = FileOnly + "-" + version.ToString("00") + Path.GetExtension(filename);
string Combined = Path.Combine(Path.GetDirectoryName(filename), FileWithInt);
return Combined;
}
public static void NotImplimentedMessage() public static void NotImplimentedMessage()
{ {
MessageBox.Show(Translate("NB_NotImplimented")); MessageBox.Show(Translate("NB_NotImplimented"));