static List <Location> FilterBySupportedLocations(this ReferenceLocationCollection collection) { return (collection .Where(location => location.LocationType == LocationType.ElectronicAddress && ( location.Address.LinkedResourceType == LinkedResourceType.AttachmentRemote || location.Address.LinkedResourceType == LinkedResourceType.AttachmentFile || location.Address.LinkedResourceType == LinkedResourceType.AbsoluteFileUri || location.Address.LinkedResourceType == LinkedResourceType.RelativeFileUri )).ToList()); }
static IEnumerable <Location> GetAvailable(this ReferenceLocationCollection locations) { return((from location in locations where location.LocationType == LocationType.ElectronicAddress && ((location.Address.LinkedResourceType == LinkedResourceType.AttachmentRemote && location.Address.CachingStatus == CachingStatus.Available) || location.Address.LinkedResourceType == LinkedResourceType.AttachmentFile || location.Address.LinkedResourceType == LinkedResourceType.AbsoluteFileUri || location.Address.LinkedResourceType == LinkedResourceType.RelativeFileUri ) && File.Exists(location.Address.Resolve().GetLocalPathSafe()) select location).ToList()); }
public static void Main() { //**************************************************************************************************************** // EXPORT ATTACHMENTS TO CATEGORY FOLDER STRUCTURE // V1.0 -- 2016-09-21 // V1.1 -- 2016-09-29 - handle non-[Citavi] attachments // V1.2 -- 2016-09-30 - check for valid file names // V1.3 -- 2016-10-13 - switch for creation of all category folders (regardless of content) // - check if [Local] or [LAN] attachment is reachable before trying to copy it // V1.4 -- 2019-06-04 - export attachments that do not have a category to an additional folder // EDIT HERE // Variables to be changed by user bool createFoldersForAllCategories = false; // set to false if only category folders for references with attachments are required // DO NOT EDIT BELOW THIS LINE // **************************************************************************************************************** if (Program.ProjectShells.Count == 0) { return; //no project open } int changeCounter = 0; int errorCounter = 0; //iterate over all references in the current filter (or over all, if there is no filter) List <Reference> references = Program.ActiveProjectShell.PrimaryMainForm.GetFilteredReferences(); if (references == null) { return; } //reference to active Project Project activeProject = Program.ActiveProjectShell.Project; if (activeProject == null) { return; } //get root folder to export to string exportPath; FolderBrowserDialog folderDialog = new FolderBrowserDialog(); folderDialog.Description = "Please select root folder for export"; folderDialog.SelectedPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); if (folderDialog.ShowDialog() == DialogResult.OK) { exportPath = folderDialog.SelectedPath; } else { return; } // create category structure as folder structure if all categories are required if (createFoldersForAllCategories) { DebugMacro.WriteLine("Creating category folder structure ..."); List <Category> allCategories = activeProject.AllCategories.ToList(); List <string> allCategoryPaths = new List <string>(); foreach (Category category in allCategories) { string[] categoryPaths = category.GetPath(true).Split(new string[] { " > " }, StringSplitOptions.None); for (int i = 0; i < categoryPaths.Length; i++) { categoryPaths[i] = MakeValidFileName(categoryPaths[i]); } string categoryPath = String.Join(@"\", categoryPaths); allCategoryPaths.Add(categoryPath); } foreach (string categoryPath in allCategoryPaths) { string fullPath = exportPath + @"\" + categoryPath; CreateFolderStructure(fullPath); } } // export reference Attachments DebugMacro.WriteLine("Processing references ..."); foreach (Reference reference in references) { DebugMacro.WriteLine("Processing " + reference.ShortTitle + " ... "); //establish whether or not there are ATTACHMENTS List <Location> referenceAttachments = new List <Location>(); ReferenceLocationCollection referenceLocations = reference.Locations; foreach (Location location in referenceLocations) { if (location.LocationType == LocationType.ElectronicAddress && (location.AddressUri.AddressInfo == ElectronicAddressInfo.Attachment || location.AddressUri.AddressInfo == ElectronicAddressInfo.AbsoluteFileUri || location.AddressUri.AddressInfo == ElectronicAddressInfo.RelativeFileUri) ) { referenceAttachments.Add(location); } } if (referenceAttachments == null || referenceAttachments.Count == 0) { continue; } DebugMacro.WriteLine("Number of attachments found: " + referenceAttachments.Count.ToString()); //establish Category tree List <string> referenceCategoryPaths = new List <string>(); foreach (Category category in reference.Categories) { string[] categoryPaths = category.GetPath(true).Split(new string[] { " > " }, StringSplitOptions.None); for (int i = 0; i < categoryPaths.Length; i++) { categoryPaths[i] = MakeValidFileName(categoryPaths[i]); } string categoryPath = String.Join(@"\", categoryPaths); referenceCategoryPaths.Add(categoryPath); } if (!referenceCategoryPaths.Any()) { referenceCategoryPaths.Add("0 No category"); } // create folders if necessary ... foreach (string referenceCategoryPath in referenceCategoryPaths) { string fullPath = exportPath + @"\" + referenceCategoryPath; CreateFolderStructure(fullPath); // ... and export attachments foreach (Location referenceAttachment in referenceAttachments) { string sourcePath = referenceAttachment.AddressUri.AbsoluteUri.GetLocalPathSafe(); string destinationPath = fullPath + @"\" + Path.GetFileName(sourcePath); DebugMacro.WriteLine("Copying " + sourcePath + " --> " + destinationPath); //check if source exists if (!File.Exists(sourcePath)) { DebugMacro.WriteLine("Source file not found."); errorCounter++; continue; } bool tryAgain = true; while (tryAgain) { try { File.Copy(sourcePath, destinationPath, true); changeCounter++; tryAgain = false; } catch (Exception e) { tryAgain = false; DialogResult directoryError = MessageBox.Show("An error occurred creating a folder: " + e.Message, "Error creating folder", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); if (directoryError == DialogResult.Abort) { return; } else if (directoryError == DialogResult.Retry) { tryAgain = true; } else { errorCounter++; break; }; } } } } } activeProject.Save(); // Message upon completion string message = "Finished.\n {0} files copied\n {1} errors occurred"; message = string.Format(message, changeCounter.ToString(), errorCounter.ToString()); MessageBox.Show(message, "Macro", MessageBoxButtons.OK, MessageBoxIcon.Information); }
public static void Main() { //**************************************************************************************************************** // EXPORT ATTACHMENTS TO CATEGORY FOLDER STRUCTURE // V1.5 -- 2019-05-29 - option for creating Location in Citavi for each exported file // v1.6 -- 2019-06-04 - folder for attachments without categories // EDIT HERE // Variables to be changed by user bool createFoldersForAllCategories = true; // set to false if only category folders for references with attachments are required bool createLocationForExportedFiles = true; // create a new Location in the Citavi project that points to the exported file // local projects only string noCategoryFolder = "0 No Category"; // Name of the folder for attachments without categories // DO NOT EDIT BELOW THIS LINE // **************************************************************************************************************** if (Program.ProjectShells.Count == 0) { return; //no project open } int foundCounter = 0; int changeCounter = 0; int errorCounter = 0; //iterate over all references in the current filter (or over all, if there is no filter) List <Reference> references = Program.ActiveProjectShell.PrimaryMainForm.GetFilteredReferences(); if (references == null) { return; } //reference to active Project Project activeProject = Program.ActiveProjectShell.Project; bool isCloudProject = activeProject.DesktopProjectConfiguration.ProjectType == ProjectType.DesktopCloud; if (activeProject == null) { return; } //get root folder to export to string exportPath; FolderBrowserDialog folderDialog = new FolderBrowserDialog(); folderDialog.Description = "Please select root folder for export"; folderDialog.SelectedPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); if (folderDialog.ShowDialog() == DialogResult.OK) { exportPath = folderDialog.SelectedPath; } else { return; } // create category structure as folder structure if all categories are required if (createFoldersForAllCategories) { DebugMacro.WriteLine("Creating category folder structure ..."); List <Category> allCategories = activeProject.AllCategories.ToList(); List <string> allCategoryPaths = new List <string>(); foreach (Category category in allCategories) { string[] categoryPaths = category.GetPath(true).Split(new string[] { " > " }, StringSplitOptions.None); for (int i = 0; i < categoryPaths.Length; i++) { categoryPaths[i] = MakeValidFileName(categoryPaths[i]); } string categoryPath = String.Join(@"\", categoryPaths); allCategoryPaths.Add(categoryPath); } foreach (string categoryPath in allCategoryPaths) { string fullPath = exportPath + @"\" + categoryPath; CreateFolderStructure(fullPath); } } // export reference Attachments foreach (Reference reference in references) { DebugMacro.WriteLine("Processing references ..."); DebugMacro.WriteLine("Processing " + reference.ShortTitle + " ... "); //establish whether or not there are ATTACHMENTS List <Location> referenceAttachments = new List <Location>(); ReferenceLocationCollection referenceLocations = reference.Locations; foreach (Location location in referenceLocations) { if (location.LocationType == LocationType.ElectronicAddress && (location.Address.LinkedResourceType == LinkedResourceType.AttachmentRemote || location.Address.LinkedResourceType == LinkedResourceType.AttachmentFile || location.Address.LinkedResourceType == LinkedResourceType.AbsoluteFileUri || location.Address.LinkedResourceType == LinkedResourceType.RelativeFileUri)) { referenceAttachments.Add(location); } } if (referenceAttachments == null || referenceAttachments.Count == 0) { continue; } DebugMacro.WriteLine("Number of attachments found: " + referenceAttachments.Count.ToString()); //establish Category tree List <string> referenceCategoryPaths = new List <string>(); foreach (Category category in reference.Categories) { string[] categoryPaths = category.GetPath(true).Split(new string[] { " > " }, StringSplitOptions.None); for (int i = 0; i < categoryPaths.Length; i++) { categoryPaths[i] = MakeValidFileName(categoryPaths[i]); } string categoryPath = String.Join(@"\", categoryPaths); referenceCategoryPaths.Add(categoryPath); } if (!referenceCategoryPaths.Any()) { referenceCategoryPaths.Add(noCategoryFolder); } // create folders if necessary ... foreach (string referenceCategoryPath in referenceCategoryPaths) { string fullPath = exportPath + @"\" + referenceCategoryPath; CreateFolderStructure(fullPath); // ... and export attachments foreach (Location referenceAttachment in referenceAttachments) { //string sourcePath = referenceAttachment.AddressUri.AbsoluteUri.GetLocalPathSafe(); Uri sourceUri = referenceAttachment.Address.Resolve(); string sourcePath = sourceUri.LocalPath; string destinationPath = String.Empty; if (referenceAttachment.Address.LinkedResourceType == LinkedResourceType.AttachmentRemote) { if (referenceAttachment.Address.LinkedResourceStatus != LinkedResourceStatus.Attached) { continue; } if (referenceAttachment.Address.CachingStatus != CachingStatus.Available) { continue; } destinationPath = fullPath + @"\" + referenceAttachment.FullName; } else { destinationPath = fullPath + @"\" + Path.GetFileName(sourcePath); } DebugMacro.WriteLine("Copying " + sourcePath + " --> " + destinationPath); //check if source exists if (!File.Exists(sourcePath)) { DebugMacro.WriteLine("Source file not found."); errorCounter++; continue; } bool tryAgain = true; bool success = false; while (tryAgain) { try { File.Copy(sourcePath, destinationPath, true); changeCounter++; tryAgain = false; success = true; } catch (Exception e) { tryAgain = false; DialogResult directoryError = MessageBox.Show("An error occurred creating a folder: " + e.Message, "Error creating folder", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); if (directoryError == DialogResult.Abort) { return; } else if (directoryError == DialogResult.Retry) { tryAgain = true; } else { errorCounter++; break; }; } } if (createLocationForExportedFiles && success && !isCloudProject) { CreateLocation(reference, destinationPath); } } } } // Message upon completion string message = "Finished.\n {0} files copied\n {1} thought files created\n {2} errors occurred"; message = string.Format(message, changeCounter.ToString(), foundCounter.ToString(), errorCounter.ToString()); MessageBox.Show(message, "Macro", MessageBoxButtons.OK, MessageBoxIcon.Information); }