Automatically move files from one folder to another
npm install meirochokidar to automatically move files around your system*. It's currently still in development and hasn't been tested alot.
chokidar for file detection_)
npm install meiro
npm install chokidar
`
Then require and initialize it with chokidar.
`js
const chokidar = require('chokidar');
const meiro = require('meiro')({
chokidar : chokidar,
logger : console.log / not yet implemented /
});
`
Methods
$3
- Returns an instance of FSWatcher from chokidar.watch()
- Starts a new watcher which automatically moves files from Folder A to Folder B.
- Example:
`js
// async/await
const watcher = await meiro.getWatcher({ / options /});
// default promise
meiro.startWatcher({ / options /})
.then( watcher => {})
.catch( e => {});
// watcher is an instance of FSWatcher from chokidar.watch()
// with already attached event handler for 'add' event
`
##### Options for meiro.startWatcher(options)
- watchFolder (required) : Folder from which files should get moved from. Must be a directory. Uses globe pattern for wildcard subfolder. Example: C:/Documents// Will look in each subfolder of Documents .
- targetExt (optional) : Only files with specified file extension will be moved. Example: "mov" will only look for *.mov files. You can only pass an array like ["mov", "mxf"] to look for multiple file extensions. If not specified all files will be moved.
- destination (required) : Folder to which files should get moved to. Must be a directory.
- before (optional) : Takes a function with 1 parameter. Will be triggered if a matching file was found, before it gets moved. Parameter is a string value with the full path to the matching file. Should return true if the file should get moved, false if not. Can be used for additional filtering. Example:
`js
let options = {
before : (pathToFile) => {
return true / File should get moved /
}
}
`
- filename (optional) : Takes a function with 1 parameter. Will be triggered before the file gets moved. Parameter is a string value with the filename (incl. ext). Should return a string value with the new filename. You need to take care of the file extension yourself. Can be used to rename files. Example:
`js
let options = {
filename : (filename) => {
return Date.now() + path.extname(filename);
// 1574954935145.mov
}
}
`
- after(optional) : Takes a function with 2 parameters. Will be triggerd after the file was moved. First parameter is a string value with the old path to the file, second parameter is a string value with the new path to the file. Example:
`js
let options = {
after : (from, to) => {
console.log(File was moved from ${from} to ${to});
}
}
`
- ignoreInitial (optional) - _defaut : false_ - If set to false then meiro already gets triggered for existing files. If set to true meiro only gets triggered for new files.
- disableGlobbing (optional) - _default : false_ - If set to true then the string passed to watchFolder is treated as literal path name, even if it looks like a glob pattern.
- awaitWriteFinish (optional) - default : false - By default, the meiro will fire when a file first appears on disk, before the entire file has been written. options.awaitWriteFinish can be set to an object in order to adjust timing params:
- awaitWriteFinish.stabilityThreshold - _default : 6000_ - Amount of time in milliseconds for a file size to remain constant before emitting its event.
- awaitWriteFinish.pollInterval - _default: 200_ - File size polling interval, in milliseconds.
How does it work ?
Meiro heavily depends on chokidar. Chokidar does most of the work underground to detect new files. Meiro is just a convenient wrapper in order to automatically move files from A to B. Check out chokidar for more details - it's awesome!
Example Usecase 1 : If you get multiple files of various filetypes (extension) written into one directory (e.g. from another service) and you want to sort them automatically into specific subdirectories. For example .mov & .mxf into a ./video directory and *.jpg into a ./images` directory.