/// <summary> /// Saves files from a certain folder over to another folder /// </summary> /// <param name="foldernames">Folder to copy from</param> /// <param name="newfoldername">Folder to copy to</param> /// <param name="pathtocompareto">path to compare use as an example</param> public static void FolderCopy(string pathtocompareto, string newfoldername, string[] locationstosearch, params string[] foldernames) { string correctdirectory = FolderFinder((Path.GetDirectoryName(pathtocompareto).ToString()), foldernames); // note the files from "foldername" into a list so that you can search the network for them List <string> filenames = Directory.GetFiles(correctdirectory).ToList <string>(); // copy from the latest directory and save to "newfoldername" with current date YEAR-MO-DA format string folderwdate = Path.Combine(newfoldername, TimeUtility.DateFormatter(DateTime.Now)); if (!Directory.Exists(folderwdate)) { Directory.CreateDirectory(folderwdate); } // find the latest files on the network and give the name of the latest location for each of them foreach (string file_fullpath in filenames) { string locationlatest = FindLatest(locationstosearch, file_fullpath); try { File.Copy(file_fullpath, Path.Combine(folderwdate, Path.GetFileName(file_fullpath)), true); } catch (Exception e) { Console.WriteLine("The following error occured during the program, " + e.StackTrace.ToString()); } } }
/// <summary> /// Archives a folder in it's current location /// </summary> /// <param name="objecttobearchived">File or folder to be archived</param> /// <param name="usehour">value to decide whether to use the current hour in the archive folder name</param> public static void Archive(string objecttobearchived, bool usehour) { // string for newfolder with date and time string folderwdateandtime = Path.Combine(objecttobearchived, TimeUtility.DateFormatter(DateTime.Now, true)); // copies the contents of the folder in question to the dated folder, as well as any items within it DirectoryCopy(objecttobearchived, folderwdateandtime, true); }
/// <summary> /// Archives the requested folder to location selected /// </summary> /// <param name="foldertobearchived"></param> /// <param name="directorytosearchforarchiveparent">Location from which search should begin</param> /// <param name="archiveparentfoldername">Folder where archive should be saved</param> public static void Archive(string foldertobearchived, string directorytosearchforarchiveparent, string archiveparentfoldername) { // gets path of folder which should be archived string archivedirectorypath = FolderFinder(directorytosearchforarchiveparent, foldertobearchived); // gets the path the folder under which the archive should be placed string archiveparent = FolderFinder(directorytosearchforarchiveparent, archiveparentfoldername); // gets the future name of archive folder; includes today's date info string folderwdate = Path.Combine(archiveparent, TimeUtility.DateFormatter(DateTime.Now, false)); // copies all files + subdirectories to archive location DirectoryCopy(archivedirectorypath, folderwdate, true); }