public List <DocumentInfo> EnumDocuments( [Parameter("The document repository folder that contains the documents to return " + @"(note: the root folder is '\')", DefaultValue = @"\")] string path, [Parameter("An optional pattern that document names should match; may include wildcards ? and *", DefaultValue = "*")] string name, [Parameter("If true, recurses into each sub-folder encountered, and returns its content as well", DefaultValue = false)] bool includeSubFolders, [Parameter("The document type(s) to return", DefaultValue = EDocumentType.All)] EDocumentType documentType, [Parameter("A visibility setting used to determine whether public, private or both " + "types of documents should be returned", DefaultValue = EPublicPrivate.Public)] EPublicPrivate visibility, IOutput output) { var nameRE = FileUtilities.ConvertWildcardPatternToRE(name); List <DocumentInfo> docs = new List <DocumentInfo>(); if (_documentCache.ContainsKey(path)) { _log.TraceFormat("Locating matching documents at {0}", path); docs.AddRange(_documentCache[path].Where(doc => nameRE.IsMatch(doc.Name) && doc.IsDocumentType(documentType) && doc.IsVisible(visibility))); if (includeSubFolders) { // Recurse into sub-directory foreach (var folder in GetFolders(path, visibility)) { docs.AddRange(EnumDocuments(AddFolderToPath(path, folder.Name), name, includeSubFolders, documentType, visibility, null)); } } } else { _log.WarnFormat("The path '{0}' does not exist", path); } if (output != null) { // TODO: Add support for outputting any field of DocumentInfo output.SetHeader("Name", 30, "Document Type", "Timestamp", "Description"); foreach (var doc in docs) { output.WriteRecord(doc.Name, doc.DocumentType, doc.Timestamp, doc.Description); } output.End(); } return(docs); }
public int DeleteDocuments( [Parameter("The path to the folder from which to delete documents")] string path, [Parameter("The name of the document(s) to delete; may include wildcards ? and *")] string name, [Parameter("Set to true to delete matching documents in sub-folders as well", DefaultValue = false)] bool includeSubFolders, [Parameter("The document type(s) to delete; use All to include all documents that " + "match the name, path, and any other criteria", DefaultValue = EDocumentType.All)] EDocumentType documentType, [Parameter("Filter documents to be deleted to public, private or both", DefaultValue = EPublicPrivate.Both)] EPublicPrivate visibility, IOutput output) { int count = 0; List <DocumentInfo> docs = EnumDocuments(path, name, includeSubFolders, documentType, visibility, null); docs.Reverse(); // So we delete folder content before folders var paths = new string[1]; var names = new string[1]; if (docs.Count > 1) { output.InitProgress("Deleting documents", docs.Count); } foreach (var doc in docs) { paths[0] = doc.Folder; names[0] = doc.Name; HFM.Try("Deleting document {0}", doc.Name, () => _documents.DeleteDocuments(paths, names, (int)doc.DocumentType, (int)doc.DocumentFileType, false)); count++; if (output.IterationComplete()) { break; } if (doc.DocumentType == EDocumentType.Folder) { _documentCache.Remove(AddFolderToPath(doc.Folder, doc.Name)); } } output.EndProgress(); // Update cache LoadCache(path, includeSubFolders); _log.InfoFormat("Successfully deleted {0} documents", count); return(count); }
/// <summary> /// Returns the details of all sub-folders in the given folder. /// </summary> public IEnumerable <DocumentInfo> GetFolders(string path, EPublicPrivate visibility) { if (_documentCache.ContainsKey(path)) { return(_documentCache[path].Where(doc => doc.DocumentType == EDocumentType.Folder && doc.IsVisible(visibility))); } else { return(null); } }
public void ExtractDocuments( [Parameter("The source folder from which document extraction is to take place")] string path, [Parameter("A document name pattern identifying the documents to extract", DefaultValue = '*')] string name, [Parameter("The file system path where extracted documents are to be placed")] string targetDir, [Parameter("A flag indicating whether documents in sub-folders should be included", DefaultValue = false)] bool includeSubFolders, [Parameter("The document type(s) to be extracted", DefaultValue = EDocumentType.All)] EDocumentType documentType, [Parameter("The document file type(s) to be extracted", DefaultValue = EDocumentFileType.All)] EDocumentFileType documentFileTypes, [Parameter("The type of documents to extract (public, private, or both)", DefaultValue = EPublicPrivate.Both)] EPublicPrivate visibility, [Parameter("Flag indicating whether existing files should be overwritten", DefaultValue = true)] bool overwrite, IOutput output) { string tgtPath, filePath; int extracted = 0; var docs = EnumDocuments(path, name, includeSubFolders, documentType, visibility, null); if (!Directory.Exists(targetDir)) { Directory.CreateDirectory(targetDir); } output.InitProgress("Extracting documents", docs.Count); foreach (var doc in docs) { tgtPath = Path.Combine(targetDir, FileUtilities.PathDifference(path, doc.Folder)); filePath = Path.Combine(tgtPath, doc.DefaultFileName); if (doc.IsFolder) { if (!Directory.Exists(filePath)) { _log.FineFormat("Creating directory {0}", filePath); Directory.CreateDirectory(filePath); } } else if (overwrite || !File.Exists(filePath)) { _log.FineFormat("Extracting document {0} to {1}", doc.Name, filePath); var content = GetDocument(doc.Folder, doc.Name, doc.DocumentType); using (FileStream fs = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None)) { fs.Write(content, 0, content.Length); } // Update last modified time to match document timestamp File.SetLastWriteTime(filePath, doc.Timestamp); extracted++; } if (output.IterationComplete()) { break; } } output.EndProgress(); _log.InfoFormat("Successfully extracted {0} documents to {1}", extracted, targetDir); }
public bool IsVisible(EPublicPrivate visibility) { return(visibility == EPublicPrivate.Both || (visibility == EPublicPrivate.Public && !IsPrivate) || (visibility == EPublicPrivate.Private && IsPrivate)); }