ProgramGuru

File Organizer

File Organizer — purpose and prerequisites

A file organizer groups files by extension and moves them into appropriate folders. In real use, check destination collisions, special files, permissions, and path traversal; provide a dry-run option before moving anything. The primary example plans actions without touching the user's filesystem.

What you’ll learn

  • Explain the purpose of file organizer.
  • Identify the typed contract, runtime responsibilities and common pitfalls.
  • Run the example and extend its behavior with a practical exercise.

How the idea works

The extension determines a destination category. Because the code only prints a plan, students can inspect it before enabling actual filesystem moves with fs.rename.

Moving user files automatically without collision checks could overwrite valuable data; test a dry run and reject conflicting paths.

Visualize the flow

File Organizer — learning flowSource filenames → Extension classifier → Dry-run move planFile Organizer1Source filenames2Extension classifier3Dry-run move planProject core logic is executable; integrations may need setup.
A three-step view of the responsibilities and the observable result.

Run the checked TypeScript example

This source was compiled using TypeScript 5.8.3 with strict checking, then executed with Node.js 22.16.0. Its stdout below is the actual captured output. The core logic is runnable without a framework or a remote API; integration-specific examples appear in a separate lab when needed.

TypeScript source

import { extname, basename } from "node:path";
function folderFor(file: string): string {
  const ext = extname(file).toLowerCase();
  if ([".png", ".jpg", ".webp"].includes(ext)) return "images";
  if ([".pdf", ".txt", ".docx"].includes(ext)) return "documents";
  return "other";
}
for (const name of ["photo.png", "notes.pdf", "archive.zip"]) {
  console.log(`${basename(name)} -> ${folderFor(name)}/`);
}

Verified output

photo.png -> images/
notes.pdf -> documents/
archive.zip -> other/

Compile: npx tsc --strict --target es2022 --module commonjs file-organizer.ts; run: node file-organizer.js. For examples importing node: modules, install @types/node as a development dependency; for browser or React projects use the separate lab setup.

Trace the execution

The extension determines a destination category. Because the code only prints a plan, students can inspect it before enabling actual filesystem moves with fs.rename.

Before editing the code, predict what the output will be for one changed input. Run it and compare your prediction with the actual result.

Common mistake and correction

Moving user files automatically without collision checks could overwrite valuable data; test a dry run and reject conflicting paths.

Determine whether the issue belongs to the type system, a trust boundary, or actual runtime behavior before choosing a correction.

Your turn: extend the example

Write a dry-run command listing planned moves in a temporary directory. Implement safe actual moves only after reviewing the plan.

Record expected success and failure outcomes, run the code, and compare the observed behavior. Keep the exercise compatible with strict type checking.

Check your understanding

Which statement correctly describes file organizer in the example?
Choose one answer. You can try again.

Answer and explanation

The extension determines a destination category.

The extension determines a destination category. Because the code only prints a plan, students can inspect it before enabling actual filesystem moves with fs.rename.

Key idea to remember

The extension determines a destination category. Because the code only prints a plan, students can inspect it before enabling actual filesystem moves with fs.rename.

Apply the same reasoning to the practice task, then integrate it into a real app only when the runtime and dependency setup are ready.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Form Validation Application →