Skip to content

Commit 586f602

Browse files
author
reg499
authored
File Attributes Manager
1 parent a50923f commit 586f602

File tree

7 files changed

+515
-1
lines changed

7 files changed

+515
-1
lines changed

README.md

Lines changed: 161 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,161 @@
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.

binding.gyp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "file_attributes",
5+
"sources": [ "src/file_attributes.cc" ],
6+
"include_dirs": [
7+
"<!(node -e \"require('node-addon-api').include\")"
8+
]
9+
}
10+
]
11+
}

index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const addon = require("bindings")("file_attributes");
2+
3+
function setFileAttributes(filePath, modes) {
4+
return new Promise((resolve, reject) => {
5+
try {
6+
const modeList = modes.split(",");
7+
modeList.forEach((mode) => {
8+
addon.setFileAttributes(filePath, mode.trim());
9+
});
10+
resolve(true);
11+
} catch (error) {
12+
reject(error);
13+
}
14+
});
15+
}
16+
17+
function getFileAttributes(filePath) {
18+
return new Promise((resolve, reject) => {
19+
try {
20+
const attributes = addon.getFileAttributes(filePath);
21+
resolve(attributes);
22+
} catch (error) {
23+
reject(error);
24+
}
25+
});
26+
}
27+
28+
module.exports = {
29+
setFileAttributes,
30+
getFileAttributes,
31+
};

package.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "node-file-attributes",
3+
"version": "0.0.2",
4+
"description": "A Node.js native addon for managing file attributes on Windows, such as hidden, system, read-only, archive, compression, and indexing.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "node test/test.js",
8+
"build": "node-gyp configure build"
9+
},
10+
"dependencies": {
11+
"bindings": "^1.5.0",
12+
"nan": "^2.15.0"
13+
},
14+
"devDependencies": {
15+
"node-addon-api": "^5.0.0",
16+
"node-gyp": "^9.0.0"
17+
},
18+
"keywords": [
19+
"file",
20+
"attributes",
21+
"hidden",
22+
"system",
23+
"readonly",
24+
"archive",
25+
"compression",
26+
"indexing",
27+
"windows",
28+
"node-gyp",
29+
"native-addon",
30+
"windows-attributes"
31+
],
32+
"author": "reg499",
33+
"license": "MIT",
34+
"gypfile": true,
35+
"engines": {
36+
"node": ">=12.0.0"
37+
},
38+
"repository": {
39+
"type": "git",
40+
"url": "https://github.com/reg499/node-file-attributes.git"
41+
},
42+
"bugs": {
43+
"url": "https://github.com/reg499/node-file-attributes/issues"
44+
},
45+
"homepage": "https://github.com/reg499/node-file-attributes"
46+
}

0 commit comments

Comments
 (0)