Managing Directories
Create, list, and remove directories.
Managing Directories is a free Swift Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Frequently asked questions
Is the “Managing Directories” lesson free?
Yes — the full text of “Managing Directories” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
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
- URLs and File Paths
- Reading and Writing Data
- Managing Directories
- App Sandbox Directories