Sorting Logic
Understanding how forg decides where to move your files is key to building an effective configuration.
The Rule Matcher
When forg scans a directory, it processes each file through your list of rules in the order they appear in config.json.
- Iterate Rules: For each file, forg looks at the first rule in the config.
- Regex Match: It checks if the filename matches the
patterndefined in that rule. - First Match Wins:
- If the file matches, it is assigned to the destination
pathof that rule. - Crucially, forg then stops looking at any further rules for that file.
- If it doesn't match, it moves to the next rule.
- If the file matches, it is assigned to the destination
Why "First Match Wins"?
This logic allows you to create specific overrides for general rules.
Example Scenario
Imagine you want all images to go to Pictures, but you want screenshots specifically to go to Pictures/Screenshots.
[
{
"pattern": ".*Screenshot.*",
"path": "Pictures/Screenshots"
},
{
"pattern": ".*\\.(png|jpg|jpeg)$",
"path": "Pictures"
}
]
- A file named
Screenshot_2023.pngmatches the first rule. It goes toPictures/Screenshots. The second rule is never even checked. - A file named
vacation.pngdoes not match the first rule, so it moves to the second. It matches and goes toPictures.
Screenshot_2023.png would match the general image rule first and end up in Pictures, never reaching the specific screenshot rule!
Deterministic Execution & Grouping
forg performs a deterministic, two-phase execution to ensure predictable behavior and high performance:
- Scanning Phase: The tool performs a global scan of the target directory, matching files against rules and constructing a lexicographically sorted map of operations using a
BTreeMap. - Movement Phase: Movement is executed in groups based on the destination directory.
This architecture explains why execution reports are organized by destination and ensures that file operations are processed in a consistent order regardless of the underlying filesystem's directory entry sequence.
Unmatched Files
If a file doesn't match any rules in your configuration, forg simply leaves it where it is. It won't be moved, and it won't be reported as an error. It will, however, be counted in the "Total Files" scanned in the execution report.