Directory के Operations
Directories fs से बनाई, listed, और inspect की जा सकती हैं।
In this page:
Syntax
fs.mkdir('directory', { recursive: true }, (err) => {});
fs.readdir('directory', (err, names) => {});
fs.readdir('directory', { withFileTypes: true }, (err, entries) => {});
Directory Operations
mkdir folders बनाता है (recursive parents बनाता है), readdir names list करता है, और withFileTypes files को folders से अलग बताता है। stat size और type reveal करता है। इन्हें combine करना recursive directory walkers जैसे tools बनाता है।
Note:
प्रति entry एक extra stat से बचने के लिए withFileTypes के साथ readdir इस्तेमाल करें।
उदाहरण: Directory operations
const fs = require("fs");
fs.mkdirSync("proj/src", { recursive: true });
fs.writeFileSync("proj/readme.md", "# hi");
fs.writeFileSync("proj/src/app.js", "1");
const entries = fs.readdirSync("proj", { withFileTypes: true });
console.log(entries.map((e) => e.name + (e.isDirectory() ? "/" : "")).sort());
// Output:
// [ 'readme.md', 'src/' ]
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- nested folders के लिए recursive भूल जाना
- यह मान लेना कि readdir full paths return करता है
- EEXIST handle न करना
चैप्टर सारांश
- mkdir folders बनाता है
- readdir names list करता है
- withFileTypes entries identify करता है
- stat details देता है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: