README: document nit_env.sh
[nit.git] / contrib / sort_downloads / README.md
1 Script to sort content of a folder in many folders according to their names.
2
3 This scripts sorts files from a given directory to subfolders in the destination directory. It uses directory names as patterns to sort the files and thus moves each file to a directory with a similar name. Multiple directories may be used to specify the patterns to look for, but the files will only be copied to subfolders in the target directory. This features ensures a clean support for a multi-disk system.
4
5 # Installation and usage
6
7 If you're lucky enough to have a Nit interpreter, directly run `src/sort_downloads.nit`.
8
9 Otherwise, compile with `make` and run with `bin/sort_downloads`.
10
11 Check `sort_downloads --help` for command line options.
12
13 # Configuration
14
15 The main configuration is in the header of `sort_downloads.nit`, in the config class. You may modify it as you need. Notice that the `~` is supported.
16
17 You can also have alternative configurations by adding another Nit module, refining the Config class and calling super as main. See the scenario "Sort only older files" for an example.
18
19 # Scenarios
20
21 Here are some usage scenario.
22
23 ## Music downloads
24
25 You download all of your music to the `~/Downloads` directory but want to sort them in the subfolders of `~/Music`. You must first make sure that there are the appropriate folders in the `~/Music` directory, probably one per artist. Then modify the Config class in `sort_downloads.nit` with something like this:
26
27     class Config
28             var source_dir = "~/Downloads/"
29             var dest_dir = "~/Music/"
30             var regex_source_dirs: Array[String] = ["~/Music/"]
31             var elapsed_days = 0
32     end
33
34 ## New hard drive
35
36 Your old hard drive is full and you bought a new one. You now want to copy your music only on the new hard drive without having to recreate all the folders. You can still use the old folders as a reference for patterns and only copy to the new hard drive. Modify the Config class like so:
37
38     class Config
39             var source_dir = "~/Downloads/"
40             var dest_dir = "/media/new-drive/Music/"
41             var regex_source_dirs: Array[String] = ["~/Music/", dest_dir] # here we use the local variable dest_dir
42             var elapsed_days = 0
43     end
44
45 ## Multiple configurations, sort videos
46
47 If you need more than one configuration, let's say to sort videos, you can use Nit class refinement. Create a separate Nit module next to `src/sort_downloads.nit` named `sort_videos.nit` and use something like:
48
49     #!/usr/bin/env nit
50
51     import sort_downloads
52
53     redef class Config
54             redef fun source_dir do return "/media/new-drive/video-downloads"
55             redef fun dest_dir do return "/media/new-drive/Videos"
56             redef fun regex_source_dirs do return ["~/Videos", dest_dir]
57             redef fun elapsed_days do return 0
58     end
59
60     super # this executes the program
61
62 ## Sort only older files
63
64 You want to automate the process with a cron entry, but still want to have your files available in the download directory for a few days. You can use the `elapsed_days` attribute of the Config class. When set to 7, only files that have not been modified for 7 days will be sorted and moved.
65
66 # Author and license
67
68 Created by Alexis Laferrière
69
70 Licensed under the Apache License Version 2.0