RapidMod icon

Node File Watcher

RapidMod | PRO | 04/04/24 01:26:32 PM UTC (Edited) | 0 ⭐ | 6206 👁️ | Never ⏰ | []
TypeScript |

2.71 KB

|

None

|

0 👍

/

0 👎

/**
 * File Watcher and Processor
 * 
 * Description:
 * This script, authored by RapidMod, utilizes the Chokidar library to watch for new files in a specified directory
 * and processes them in the order they were added. It's designed to monitor an upload directory,
 * automatically adding new files to a processing queue. Each file is then processed according
 * to the logic defined in the `processFile` function. This setup is ideal for applications that
 * need to handle file uploads and process them sequentially, ensuring no file is overlooked and
 * each is handled in a timely manner.
 * 
 * Features:
 * - Watches a specified directory for new file additions.
 * - Ignores hidden files (those beginning with a dot).
 * - Maintains a queue of files to be processed, ensuring order based on creation time.
 * - Processes each file sequentially through a customizable processing function.
 * 
 * Getting Started:
 * 1. Ensure Node.js is installed on your system.
 * 2. Install Chokidar via npm with `npm install chokidar`.
 * 3. Modify the `uploadDir` variable to point to your target directory.
 * 4. Implement your file processing logic in the `processFile` function.
 * 5. Run the script using `node <script_name>.js`.
 * 
 * The script will then monitor the specified directory and log a message to the console
 * each time a new file is processed. Modify the `processFile` function to fit your specific
 * processing requirements.
 * 
 * Note: This script is designed to run indefinitely. Terminate it manually when it's no longer needed.
 * 
 * Author: RapidMod
 * Website: https://rapidmod.io/
 */
 
const chokidar = require('chokidar');
const path = require('path');
const fs = require('fs');
 
const uploadDir = '/my/upload/dir';
const watcher = chokidar.watch(uploadDir, {
  ignored: /^\./, // Ignore hidden files
  persistent: true
});
 
const fileQueue = [];
 
// Function to process a file
function processFile(filePath) {
  // Implement your file processing logic here
  console.log(`Processing file: ${filePath}`);
}
 
// Process files in the queue in the order they were created
function processQueue() {
  if (fileQueue.length > 0) {
    const nextFile = fileQueue.shift();
    processFile(nextFile);
  }
}
 
watcher
  .on('add', (filePath) => {
    // Add the new file to the queue
    fileQueue.push(filePath);
    // Sort the queue based on file creation time
    fileQueue.sort((a, b) => {
      return fs.statSync(a).birthtimeMs - fs.statSync(b).birthtimeMs;
    });
    // Process the files in the queue
    processQueue();
  });
 
console.log(`Watching for new files in directory: ${uploadDir}`);
 
// Keep the script running
setInterval(() => {}, 1000);
 
 
 
 
 
 
 
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎