|
1 |
| -# node-file-attributes |
| 1 | +# File Attributes Manager - Node.js Addon |
| 2 | + |
| 3 | +**File Attributes Manager** is a Node.js native addon built with C++ for managing file attributes on Windows. It provides a simple interface to set and query various file attributes like hidden, system, read-only, archive, compression, indexing, and more. This addon leverages Windows APIs to interact with file attributes at the OS level, offering more control and precision than what is typically available in standard Node.js modules. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Set file as hidden or visible** (`hide`, `show`) |
| 8 | +- **Mark file as system** or remove system attribute (`system`, `nosystem`) |
| 9 | +- **Set file as read-only** or make it writable (`readonly`, `writable`) |
| 10 | +- **Archive flag management** (`archive`, `noarchive`) |
| 11 | +- **Temporary file flag management** (`temporary`, `notemporary`) |
| 12 | +- **Set file to normal** (resets all custom attributes) |
| 13 | +- **Enable NTFS compression** or remove it (`compress`, `nocompress`) |
| 14 | +- **Control content indexing** (`not_indexed`, `indexed`) |
| 15 | +- **Multiple attributes** can be applied at once (`e.g., hide,readonly`) |
| 16 | +- **Query file attributes** in JSON format |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +Before installing the package, make sure you have Node.js and npm installed on your machine. |
| 21 | + |
| 22 | +To install the package, run the following command: |
| 23 | + |
| 24 | +```bash |
| 25 | +npm install node-file-attributes |
| 26 | +``` |
| 27 | + |
| 28 | +> **Note**: This module works only on **Windows** platforms. |
| 29 | +
|
| 30 | +### Prerequisites |
| 31 | + |
| 32 | +- **Node.js**: Ensure you have Node.js installed. The addon is built using Node.js native addons (C++). |
| 33 | +- **Windows OS**: The file attribute management is based on Windows-specific APIs, so this module will only work on Windows environments. |
| 34 | + |
| 35 | +## Usage |
| 36 | + |
| 37 | +Once installed, you can start using the module by importing it into your project. Below are some common examples of how to use the module. |
| 38 | + |
| 39 | +### Importing the Module |
| 40 | + |
| 41 | +```javascript |
| 42 | +const { |
| 43 | + setFileAttributes, |
| 44 | + getFileAttributes, |
| 45 | +} = require("node-file-attributes"); |
| 46 | +``` |
| 47 | + |
| 48 | +### Setting File Attributes |
| 49 | + |
| 50 | +You can use the `setFileAttributes()` function to set various attributes on a file or folder. The function supports multiple attributes at once. |
| 51 | + |
| 52 | +```javascript |
| 53 | +const filePath = "path/to/your/file.txt"; |
| 54 | + |
| 55 | +// Hide and set the file as read-only |
| 56 | +setFileAttributes(filePath, "hide,readonly") |
| 57 | + .then(() => console.log("Attributes set successfully")) |
| 58 | + .catch((err) => console.error("Error:", err)); |
| 59 | +``` |
| 60 | + |
| 61 | +### Querying File Attributes |
| 62 | + |
| 63 | +To query the current attributes of a file, use the `getFileAttributes()` function. It returns a JSON object showing the status of different attributes. |
| 64 | + |
| 65 | +```javascript |
| 66 | +getFileAttributes(filePath) |
| 67 | + .then((attributes) => console.log("File attributes:", attributes)) |
| 68 | + .catch((err) => console.error("Error querying file attributes:", err)); |
| 69 | +``` |
| 70 | + |
| 71 | +The response will be a JSON object like this: |
| 72 | + |
| 73 | +```json |
| 74 | +{ |
| 75 | + "hidden": true, |
| 76 | + "system": false, |
| 77 | + "readonly": true, |
| 78 | + "archive": true, |
| 79 | + "temporary": false, |
| 80 | + "not_indexed": false |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +### Available Modes |
| 85 | + |
| 86 | +You can set the following modes using the `setFileAttributes()` function: |
| 87 | + |
| 88 | +| Mode | Description | |
| 89 | +| ------------- | --------------------------------------------- | |
| 90 | +| `hide` | Marks the file as hidden | |
| 91 | +| `show` | Makes the file visible (removes hidden flag) | |
| 92 | +| `system` | Marks the file as a system file | |
| 93 | +| `nosystem` | Removes the system file flag | |
| 94 | +| `readonly` | Sets the file as read-only | |
| 95 | +| `writable` | Removes the read-only flag | |
| 96 | +| `archive` | Marks the file as archived | |
| 97 | +| `noarchive` | Removes the archive flag | |
| 98 | +| `temporary` | Marks the file as temporary | |
| 99 | +| `notemporary` | Removes the temporary flag | |
| 100 | +| `compress` | Enables NTFS compression | |
| 101 | +| `nocompress` | Disables NTFS compression | |
| 102 | +| `not_indexed` | Prevents the file from being indexed | |
| 103 | +| `indexed` | Allows the file to be indexed | |
| 104 | +| `normal` | Resets all attributes (makes the file normal) | |
| 105 | + |
| 106 | +You can also pass multiple modes as a comma-separated list, e.g., `"hide,readonly"`. |
| 107 | + |
| 108 | +## Example Code |
| 109 | + |
| 110 | +Here are some more examples of using the module to set different attributes: |
| 111 | + |
| 112 | +```javascript |
| 113 | +const filePath = "test-file.txt"; |
| 114 | + |
| 115 | +/* Set the file as hidden */ |
| 116 | +setFileAttributes(filePath, "hide") |
| 117 | + .then(() => console.log("File is now hidden")) |
| 118 | + .catch((err) => console.error("Error:", err)); |
| 119 | + |
| 120 | +/* Remove the hidden attribute */ |
| 121 | +setFileAttributes(filePath, "show") |
| 122 | + .then(() => console.log("File is now visible")) |
| 123 | + .catch((err) => console.error("Error:", err)); |
| 124 | + |
| 125 | +/* Set the file as read-only */ |
| 126 | +setFileAttributes(filePath, "readonly") |
| 127 | + .then(() => console.log("File is now read-only")) |
| 128 | + .catch((err) => console.error("Error:", err)); |
| 129 | + |
| 130 | +/* Remove the read-only attribute */ |
| 131 | +setFileAttributes(filePath, "writable") |
| 132 | + .then(() => console.log("File is now writable")) |
| 133 | + .catch((err) => console.error("Error:", err)); |
| 134 | + |
| 135 | +/* Query current file attributes */ |
| 136 | +getFileAttributes(filePath) |
| 137 | + .then((attributes) => console.log("File attributes:", attributes)) |
| 138 | + .catch((err) => console.error("Error:", err)); |
| 139 | +``` |
| 140 | + |
| 141 | +## Building from Source |
| 142 | + |
| 143 | +If you want to build the addon from the source code, you can clone the repository and run the following commands: |
| 144 | + |
| 145 | +```bash |
| 146 | +# Install dependencies |
| 147 | +npm install |
| 148 | + |
| 149 | +# Build the addon |
| 150 | +npm run build |
| 151 | +``` |
| 152 | + |
| 153 | +This will compile the C++ code and generate the necessary bindings for Node.js. |
| 154 | + |
| 155 | +## Contributing |
| 156 | + |
| 157 | +Contributions are welcome! If you encounter any issues, feel free to create a GitHub issue or submit a pull request. Make sure to follow the coding standards and best practices when contributing. |
| 158 | + |
| 159 | +## License |
| 160 | + |
| 161 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
0 commit comments