Skip to main content

Move files based on file properties and given criteria

Project description


fmover

An open source file manager CLI that enables moving files based on file properties and specified criteria.

A demo of fmover moving files in a directory

Description

Tired of moving files manually? Given a file path and a configuration, fmover will move files based on the specified criteria.

The program can currently move files based on the properties: file source, file extension and file name. These properties are specified in the configuration which is a json file.

An example configuration looks like this:

  {
  "COMMAND": [
    {"NAME(Analysis) & WHERE_FROM(www.uni.com)": "WHERE_FROM(www.uni.com)"}, 
    {"WHERE_FROM(*)": "WHERE_FROM(*)"}
  ],
  "WHERE_FROM": {
    "www.uni.com": "/Users/user/Documents/Uni",
    "www.uni2.com": "/Users/user/Documents/Uni2"
  },
  "NAME": {
    "Analysis": "/Users/user/Documents/Uni/Analysis"
  },
  "FILE_EXTENSION": {
    ".pdf": "/Users/user/Documents/PDF",
    ".docx": "/Users/user/Documents/DOCX",
    ".pptx": "/Users/user/Documents/PPTX"
  }
}

The command section specifies which properties and respective patterns should be considered and where to move a file if that file has those properties. (e.g. the first command in natural language would be: "If a file has the name Analysis and was obtained from www.uni.com, move it to /Users/user/Documents/Uni"). The remaining sections specify which properties and patterns to consider.

Some examples of what the program will do with the configuration above:

  • If a file was downloaded from www.uni.com and the file name contains the word "Analysis", the file will be moved to the folder "/Users/user/Documents/Uni".
  • If a file extension is ".pdf", the file will be moved to the folder "/Users/user/Documents/PDF".

Further information on the configuration is given in the section [The Configuration](#The configuration).

Getting Started

Dependencies

You need to have at least Python 3.6 installed on your system. You can download it from here.

The extended file attribute "WHERE_FROM" which stores where the file was downloaded/obtained from is currently only available on macOS. If you are on another operating system, you can still use the program but you will not be able to use the this property.

Installing

Install with pip:

pip install fmover

Usage

To get an overview of the available commands, run:

fmover -h

Editing, creating and deleting configurations

You will have a default configuration file once you install the program.

To list all available configurations, run:

fmover list

To print the content of a configuration file, run:

fmover print your_config_name

To create a new configuration file, run:

fmover create new_config

To open and eventually edit a configuration file in your default text editor, run:

fmover open your_config_name

To delete a configuration file, run:

fmover delete your_config_name

Moving files

To move one file based on the configuration, run:

fmover move /path/to/file your_config_name

To move all files in a directory based on the configuration, run:

fmover move-all /path/to/directory your_config_name

To get a pop-up notification when files are moved, run:

fmover move-all /path/to/directory your_config_name --notify

To create destination folders if they do not exist, run:

fmover move-all /path/to/directory your_config_name --force

To only get the information where the files would be moved without actually moving them, run:

fmover move-all /path/to/directory your_config_name --dry-run

The configuration

The configuration defines what properties to consider and how to move files based on those properties. There are Commands and Properties. Commands specify which properties to consider and where to move a file if it has those properties. A property specifies which patterns to consider and where to move a file if it has those patterns.

Commands

  • The configuration must have the outer key "COMMAND" with a value which is a list of singleton dictionaries. These singleton dictionaries are commands which consist of antecedents (as keys) and consequents (as values).
  • An antecedent consist of tokens which are seperated by "&". A consequent only has one token.
  • Tokens consist of a parameter and a pattern. The tokens have the following form: "PARAMETER(PATTERN)". The parameter can be one of the following: "NAME", "FILE_EXTENSION", "WHERE_FROM". The pattern can be a string or a wildcard "*". A wildcard matches any pattern in the respective property.
  • If the parameter is declared in the command it must be a property in the config.

This is an example of a valid command section:

  {
  "COMMAND": [
    {
      "NAME(Tests) & FILE_EXTENSION(.pdf) & WHERE_FROM(www.uni.com": "NAME(Tests)"
    },
    {
      "WHERE_FROM(*)": "WHERE_FROM(*)"
    }
  ]
  }

Properties

  • The configuration can have (it's optional) the outer keys "NAME", "FILE_EXTENSION" and "WHERE_FROM" with values which are dictionaries.
  • The keys of these dictionaries are patterns and the values are paths.

This is an example of a valid property section:

  {
  "NAME": {
    "Tests": "/Users/user/Documents/Tests",
    "Analysis": "/Users/user/Documents/Analysis"
  },
  "FILE_EXTENSION": {
    ".pdf": "/Users/user/Documents/PDF",
    ".docx": "/Users/user/Documents/DOCX",
    ".pptx": "/Users/user/Documents/PPTX"
  },
  "WHERE_FROM": {
    "www.uni.com": "/Users/user/Documents/Uni",
    "www.uni2.com": "/Users/user/Documents/Uni2"
  }
}

Example Behavior

Let us merge these two sections to get a full configuration:

  {
  "COMMAND": [
    {
      "NAME(Tests) & FILE_EXTENSION(.pdf) & WHERE_FROM(www.uni.com": "NAME(Tests)"
    },
    {
      "WHERE_FROM(*)": "WHERE_FROM(*)"
    }
  ],
  "NAME": {
    "Tests": "/Users/user/Documents/Tests",
    "Analysis": "/Users/user/Documents/Analysis"
  },
  "FILE_EXTENSION": {
    ".pdf": "/Users/user/Documents/PDF",
    ".docx": "/Users/user/Documents/DOCX",
    ".pptx": "/Users/user/Documents/PPTX"
  },
  "WHERE_FROM": {
    "www.uni.com": "/Users/user/Documents/Uni",
    "www.uni2.com": "/Users/user/Documents/Uni2"
  }
}

Now let us consider the following file:

  • The file name is "Tests_Algebra".
  • The file extension is ".pdf".
  • The file was downloaded from "www.uni.com".

In the antecedent of the first command, all tokens are satisfied. Therefore, the consequent of the first command is executed. The consequent is "NAME(Tests)". This means that the file will be moved to the value of the key "Tests" in the property "NAME". In this case, the file will be moved to "/Users/user/Documents/Tests".

Now let us consider the following file:

  • The file name is "Test".
  • The file extension is ".pdf".
  • The file was downloaded from "www.uni.com".

In the antecedent of the first command, the first token is not satisfied, because "Test" does not contain "Tests". Therefore, the consequent of the first command is not executed. The antecedent of the second command is satisfied, because there exists a pattern in the property "WHERE_FROM" which matches with where the file was obtained from. Therefore, the consequent of the second command is executed and the file will be moved to the value of the pattern "www.uni.com" in the property "WHERE_FROM", which is "/Users/user/Documents/Uni".

Automator Folder Action on macOS

On macOs, you can combine the program with the Automator Folder Action to automatically move files to the correct folder when they are downloaded. To do this, follow these steps:

  1. Open Automator.
  2. Click on "New Document".
  3. Select "Folder Action" and click "Choose".
  4. At "Folder action receives files and folders added to:" select the folder you want to monitor.
  5. At the library on the left, search for "Run Shell Script" and double click it.
  6. At the "Shell" field, select "pass input as arguments".
  7. Paste the following code into your terminal and copy the output:
echo "for f in"' "$@"' "\ndo\n    $(which fmover) move"' "$f" default'"\ndone"
  1. Paste the output into the "Shell" field.
  2. Replace "default" with the name of your configuration.
  3. Save the action.

Each time a file is added to the folder, the action will be executed and the file will be moved to the correct folder.

Help

"WHERE_FROM" doesn't work on my mac. What can I do?

Your Browser might not store any information about where a file was downloaded from. To find out if your browser stores this information, download a file and then run the following command in your terminal:

xattr -p com.apple.metadata:kMDItemWhereFroms /path/to/downloaded/file

If the command returns nothing, your browser does not store this information. If it returns something, it does.

I know that it works on Chrome. If you know if it works on other browsers, please let me know.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments

Inspiration, code snippets, etc.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fmover-0.1.0.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

fmover-0.1.0-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file fmover-0.1.0.tar.gz.

File metadata

  • Download URL: fmover-0.1.0.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.0 Darwin/22.2.0

File hashes

Hashes for fmover-0.1.0.tar.gz
Algorithm Hash digest
SHA256 31b743afc6f5bd4b6806c7e4cbe417ec3a7237f1bd13e0d1b867327a3dfab2eb
MD5 88ec6de60e2f836662a789e373c67c3d
BLAKE2b-256 bf8f76cc1246079846886e7991e55fd9381e30dca17c532e9c28ec58a3199905

See more details on using hashes here.

File details

Details for the file fmover-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: fmover-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.0 Darwin/22.2.0

File hashes

Hashes for fmover-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f1bfeff42532d10bbc7387f47866adf89d558244152ae6e3d188fdb482928496
MD5 55c45f7a8dffdb90970732420f6969d7
BLAKE2b-256 5f08fbd9bf3497dd79603ba8bf3b5cdf5fba3d5f2c721574d5ac885d87e42c91

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page