Swift Academy · Lesson

Managing Directories

Create, list, and remove directories.

Lesson 3 of 413 steps

Managing Directories is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

Managing Directories

FileManager is the central object for creating, listing, moving, and deleting files and folders.

The Shared FileManager

Use FileManager.default for most operations. It provides a thread-safe shared instance.

import Foundation

let fm = FileManager.default
print("Got the default file manager")

Creating a Directory

createDirectory(at:withIntermediateDirectories:) makes a folder, optionally creating any missing parents.

import Foundation

let fm = FileManager.default
let dir = URL(fileURLWithPath: "/tmp/myapp/cache")
try? fm.createDirectory(at: dir, withIntermediateDirectories: true)
print("Directory created")

Checking Existence

fileExists(atPath:) reports whether a path exists, and an inout flag tells you if it is a directory.

import Foundation

let fm = FileManager.default
var isDir: ObjCBool = false
let exists = fm.fileExists(atPath: "/tmp", isDirectory: &isDir)
print("Exists: \(exists), isDir: \(isDir.boolValue)")

Listing Contents

contentsOfDirectory(at:includingPropertiesForKeys:) returns the URLs inside a folder.

import Foundation

let fm = FileManager.default
let dir = URL(fileURLWithPath: "/tmp")
if let items = try? fm.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil) {
    print("Item count: \(items.count)")
}

Listing Just Names

The path-based contentsOfDirectory(atPath:) returns plain filename strings when you do not need full URLs.

import Foundation

let fm = FileManager.default
if let names = try? fm.contentsOfDirectory(atPath: "/tmp") {
    print("Names count: \(names.count)")
}

Removing Items

removeItem(at:) deletes a file or an entire directory tree. Use with care.

import Foundation

let fm = FileManager.default
let url = URL(fileURLWithPath: "/tmp/myapp/cache/old.txt")
try? fm.removeItem(at: url)
print("Removed item if it existed")

Moving and Renaming

moveItem(at:to:) relocates or renames; copyItem(at:to:) duplicates a file.

import Foundation

let fm = FileManager.default
let src = URL(fileURLWithPath: "/tmp/a.txt")
let dst = URL(fileURLWithPath: "/tmp/b.txt")
try? fm.moveItem(at: src, to: dst)
print("Moved if source existed")

Enumerating Deeply

A FileManager.DirectoryEnumerator walks an entire subtree recursively, yielding every nested URL.

import Foundation

let fm = FileManager.default
let root = URL(fileURLWithPath: "/tmp")
if let e = fm.enumerator(at: root, includingPropertiesForKeys: nil) {
    var count = 0
    for case _ as URL in e { count += 1; if count > 5 { break } }
    print("Walked some entries: \(count)")
}

File Attributes

attributesOfItem(atPath:) returns metadata like size and modification date in a dictionary.

import Foundation

let fm = FileManager.default
if let attrs = try? fm.attributesOfItem(atPath: "/tmp"),
   let size = attrs[.size] {
    print("Has size attribute: \(size)")
}

Handling Failures

Directory operations throw on permission or missing-path errors. Wrap them in do/catch to respond gracefully.

import Foundation

let fm = FileManager.default
do {
    try fm.removeItem(at: URL(fileURLWithPath: "/no/such/file"))
} catch {
    print("Remove failed: \(error.localizedDescription)")
}

Quick Check

Which option makes createDirectory also create missing parent folders?

Recap

FileManager.default creates folders with createDirectory, checks fileExists, lists with contentsOfDirectory, deletes with removeItem, and moves or copies items. You also enumerated subtrees and read attributes. Next: the app sandbox directories.

Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Managing Directories” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Managing Directories”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “Managing Directories”?

Create, list, and remove directories. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “Managing Directories” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. URLs and File Paths
  2. Reading and Writing Data
  3. Managing Directories
  4. App Sandbox Directories
← Back to Swift Academy