/// <summary> /// Returns whether or not the given file matches the tag filter /// </summary> /// <param name="file"></param> /// <returns></returns> public bool Matches(TaggedFilePath file) { // HACK: If we're searching for untagged files, then // return true if there are no tags if (untagged) { return(!file.Tags.Any()); } // Return false if any of the required tags are missing foreach (string t in requiredTags) { if (!file.Tags.Contains(t)) { return(false); } } // Return false if any of the forbidden tags are present foreach (string t in forbiddenTags) { if (file.Tags.Contains(t)) { return(false); } } // It passed the filter return(true); }
/// <summary> /// Creates a duplicate, but with the tags set to the given array. /// </summary> public TaggedFilePath SetTags(string[] newTags) { // Create a duplicate, but with the tags set to newTags. TaggedFilePath output = new TaggedFilePath { beforeTags = beforeTags, afterTags = afterTags, hasTagArea = newTags.Length == 0 ? false : true, tags = newTags, ParentFolder = ParentFolder, Extension = Extension, IsFolder = IsFolder }; // If there isn't already a tag area, then we need to insert // one before the extension. if (!hasTagArea && Extension != "") { int lengthWithoutExt = beforeTags.Length - Extension.Length; output.beforeTags = beforeTags.Substring(0, lengthWithoutExt); output.afterTags = Extension; } // Return it return(output); }
/// <summary> /// Changes the tags on the specified file and applies the change /// on the filesystem. Returns the path to the renamed file. /// </summary> /// <param name="src"></param> /// <param name="dst"></param> public static TaggedFilePath ChangeTagsAndSave(TaggedFilePath file, string[] newTags) { // Get the new path for the file TaggedFilePath changed = file.SetTags(newTags); // Move the file on the filesystem if (file.IsFolder) { Directory.Move(file.FullPath, changed.FullPath); } else { File.Move(file.FullPath, changed.FullPath); } // Return the changed path return(changed); }
/// <summary> /// A mode that sorts files by a number at the beginning of their name /// eg: 0.jpg < 1.jpg < 11.jpg /// </summary> /// <param name="f"></param> /// <returns></returns> private static IComparable ComicSort(TaggedFilePath f) { // Extract the first number you can find out of the name var beforeDigits = f.Name.SkipWhile(c => !Char.IsDigit(c)); // Skip to the first digit var digits = beforeDigits.TakeWhile(c => Char.IsDigit(c)); // Go all the way up to the first non-digit // Try to parse it as an int string s = new string(digits.ToArray()); int result; bool success = int.TryParse(s, out result); // If it doesn't have a number, just default it to a super-high number so it appears last if (!success) { return(int.MaxValue); } return(result); }