A Node.js application that monitors a command file and performs file system operations based on text commands. This project demonstrates file watching, asynchronous file operations, and command parsing in Node.js.
- File Creation: Create new files with custom paths
- File Deletion: Remove files from the filesystem
- File Renaming: Rename/move files to new locations
- Content Addition: Append content to existing files
- Real-time Monitoring: Watches command.txt for changes and executes commands automatically
The application monitors command.txt for changes and parses commands to perform file operations:
- The app starts and opens
command.txtfor reading - A file watcher monitors the command file for changes
- When the file changes, the app reads and parses the command
- The corresponding file operation is executed
create a file <path/to/file.txt>
Creates a new file at the specified path. If the file already exists, it will not be overwritten.
delete the file <path/to/file.txt>
Removes the specified file from the filesystem.
rename the file <old/path.txt> to <new/path.txt>
Renames or moves a file from the old path to the new path.
add to the file <path/to/file.txt> this content: <your content here>
Appends the specified content to the end of the file.
- Clone or download this project
- Navigate to the project directory
- No additional dependencies required - uses only built-in Node.js modules
- Start the application:
node app.js- Open
command.txtin a text editor - Add one of the supported commands (see above)
- Save the file - the command will be executed automatically
- Check the console for operation results
- Start the application:
node app.js - Edit
command.txtand add:create a file test.txt - Save the file - a new
test.txtwill be created - Edit
command.txtagain:add to the file test.txt this content: Hello World! - Save - "Hello World!" will be appended to test.txt
- Edit
command.txt:rename the file test.txt to renamed.txt - Save - the file will be renamed
- Edit
command.txt:delete the file renamed.txt - Save - the file will be deleted
The application includes comprehensive error handling:
- File existence checks before creation
- Graceful handling of missing files during deletion/renaming
- Error logging for filesystem operations
- Duplicate content prevention in add operations
- Language: JavaScript (Node.js)
- Core Modules:
fs/promisesfor asynchronous file operations - Architecture: Event-driven with file watching
- Pattern: Command pattern with text-based interface
This project demonstrates:
- Asynchronous file system operations in Node.js
- File watching and event handling
- Command parsing and execution
- Error handling in file operations
- Buffer manipulation for file reading
- The application runs continuously until manually stopped (Ctrl+C)
- Commands are processed in the order they appear in command.txt
- Only one command is processed per file change
- The command file is monitored in real-time