Answered by:
Backup Specific Folders

Question
-
User-1391672913 posted
Hi,
I have created a backup button that copies all folders and its contents in a backup folder. However, I want to just include specific folder names to be copied in the backup folder. Anyone had any ideas of how to do it? Any help would be appreciated.
this is my code on the backup button:
protected void BackupButton_Click(object sender, EventArgs e) { string serverPath; if (ServerList.SelectedItem.Value == "1") { serverPath = "10.13.58.17"; } else if (ServerList.SelectedItem.Value == "2") { serverPath = "10.13.58.33"; } else { serverPath = "10.99.99.99"; } string sourcePath = @"\\" + serverPath + "\\C$\\TEST FOLDER\\DESTINATION FOLDER"; string targetPath = @"\\" + serverPath + "\\C$\\TEST FOLDER\\BACKUP FOLDER"; string dateToday = DateTime.Now.ToString("dd-MMM-yyyy"); var createBackupFolder = Directory.CreateDirectory(Path.Combine(targetPath, "BACKUP_" + dateToday)); if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } string subFolder = targetPath + createBackupFolder; //Create all of the directories foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories)) Directory.CreateDirectory(dirPath.Replace(sourcePath, subFolder)); //Copy all the files & Replaces any files with the same name foreach (string newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories)) File.Copy(newPath, newPath.Replace(sourcePath, subFolder), true); }
Sincerely,
NoobNoob
Tuesday, January 28, 2020 1:53 AM
Answers
-
User409696431 posted
Iterate through a list of folders you want to backup, and set the paths to those specific folders.
Change:
string sourcePath = @"\\" + serverPath + "\\C$\\TEST FOLDER\\DESTINATION FOLDER"; string targetPath = @"\\" + serverPath + "\\C$\\TEST FOLDER\\BACKUP FOLDER"; string dateToday = DateTime.Now.ToString("dd-MMM-yyyy"); var createBackupFolder = Directory.CreateDirectory(Path.Combine(targetPath, "BACKUP_" + dateToday)); if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } string subFolder = targetPath + createBackupFolder; //Create all of the directories foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories)) Directory.CreateDirectory(dirPath.Replace(sourcePath, subFolder)); //Copy all the files & Replaces any files with the same name foreach (string newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories)) File.Copy(newPath, newPath.Replace(sourcePath, subFolder), true);
To
string sourcePath = @"\\" + serverPath + "\\C$\\TEST FOLDER\\DESTINATION FOLDER"; string targetPath = @"\\" + serverPath + "\\C$\\TEST FOLDER\\BACKUP FOLDER"; string dateToday = DateTime.Now.ToString("dd-MMM-yyyy"); var createBackupFolder = Directory.CreateDirectory(Path.Combine(targetPath, "BACKUP_" + dateToday)); if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } string subFolder = targetPath + createBackupFolder; //Create a list of the folders you want to backup, example: List<string> folders = new List<string>{"\FolderOne", "\FolderTwo", "\Folderthree"}; //New variables to hold the folder-specific paths string foldersourcePath = "";
string foldersubfolder = ""; // Iterate through the folders and append them to sourcePath and subfolder, then do the backup foreach(string folder in folders){ foldersourcePath = sourcePath + folder; foldersubfolder = subfolder + folder; //Create all of the directories foreach (string dirPath in Directory.GetDirectories(foldersourcePath, "*", SearchOption.AllDirectories)) Directory.CreateDirectory(dirPath.Replace(foldersourcePath, foldersubFolder)); //Copy all the files & Replaces any files with the same name foreach (string newPath in Directory.GetFiles(foldersourcePath, "*.*", SearchOption.AllDirectories)) File.Copy(newPath, newPath.Replace(foldersourcePath, foldersubFolder), true);
}- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 28, 2020 4:50 AM
All replies
-
User409696431 posted
Iterate through a list of folders you want to backup, and set the paths to those specific folders.
Change:
string sourcePath = @"\\" + serverPath + "\\C$\\TEST FOLDER\\DESTINATION FOLDER"; string targetPath = @"\\" + serverPath + "\\C$\\TEST FOLDER\\BACKUP FOLDER"; string dateToday = DateTime.Now.ToString("dd-MMM-yyyy"); var createBackupFolder = Directory.CreateDirectory(Path.Combine(targetPath, "BACKUP_" + dateToday)); if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } string subFolder = targetPath + createBackupFolder; //Create all of the directories foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories)) Directory.CreateDirectory(dirPath.Replace(sourcePath, subFolder)); //Copy all the files & Replaces any files with the same name foreach (string newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories)) File.Copy(newPath, newPath.Replace(sourcePath, subFolder), true);
To
string sourcePath = @"\\" + serverPath + "\\C$\\TEST FOLDER\\DESTINATION FOLDER"; string targetPath = @"\\" + serverPath + "\\C$\\TEST FOLDER\\BACKUP FOLDER"; string dateToday = DateTime.Now.ToString("dd-MMM-yyyy"); var createBackupFolder = Directory.CreateDirectory(Path.Combine(targetPath, "BACKUP_" + dateToday)); if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } string subFolder = targetPath + createBackupFolder; //Create a list of the folders you want to backup, example: List<string> folders = new List<string>{"\FolderOne", "\FolderTwo", "\Folderthree"}; //New variables to hold the folder-specific paths string foldersourcePath = "";
string foldersubfolder = ""; // Iterate through the folders and append them to sourcePath and subfolder, then do the backup foreach(string folder in folders){ foldersourcePath = sourcePath + folder; foldersubfolder = subfolder + folder; //Create all of the directories foreach (string dirPath in Directory.GetDirectories(foldersourcePath, "*", SearchOption.AllDirectories)) Directory.CreateDirectory(dirPath.Replace(foldersourcePath, foldersubFolder)); //Copy all the files & Replaces any files with the same name foreach (string newPath in Directory.GetFiles(foldersourcePath, "*.*", SearchOption.AllDirectories)) File.Copy(newPath, newPath.Replace(foldersourcePath, foldersubFolder), true);
}- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 28, 2020 4:50 AM -
User-1391672913 posted
Hi KathyW,
You saved me. Thank you very much for your help!
Sincerely,
NoobNoob
Tuesday, January 28, 2020 6:23 AM -
User409696431 posted
You're welcome. By the way, if your folder structure that you want to backup is multi-level, don't forget to create the intermediate directories before trying to create a lower-level one. SInce you know what the folders are you want to backup, you'll know if this is the case.
Tuesday, January 28, 2020 7:59 AM -
User-1391672913 posted
Noted with this. Thank you so much!
Tuesday, January 28, 2020 8:01 AM