← Back to Node.js Course | Chapter 5: File System | Lesson 5 of 7

Directory के Operations

Directories fs से बनाई, listed, और inspect की जा सकती हैं।

In this page:

  1. Directory Operations
Syntax
javascript
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

javascript
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. #}
आम गलतियां
  1. nested folders के लिए recursive भूल जाना
  2. यह मान लेना कि readdir full paths return करता है
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.