<!--
{
  "availability" : [
    "iOS: 5.0.0 -",
    "iPadOS: 5.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.7.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/FileManager/createDirectory(at:withIntermediateDirectories:attributes:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSFileManager(im)createDirectoryAtURL:withIntermediateDirectories:attributes:error:"
  },
  "title" : "createDirectory(at:withIntermediateDirectories:attributes:)"
}
-->

# createDirectory(at:withIntermediateDirectories:attributes:)

Creates a directory with the given attributes at the specified URL.

```
func createDirectory(at url: URL, withIntermediateDirectories createIntermediates: Bool, attributes: [FileAttributeKey : Any]? = nil) throws
```

## Parameters

`url`

A file URL that specifies the directory to create. If you want to specify a relative path, you must set the current working directory before creating the corresponding [`NSURL`](/documentation/Foundation/NSURL) object. This parameter must not be `nil`.

`createIntermediates`

If <doc://com.apple.documentation/documentation/Swift/true>, this method creates any nonexistent parent directories as part of creating the directory in `url`. If <doc://com.apple.documentation/documentation/Swift/false>, this method fails if any of the intermediate parent directories does not exist.

`attributes`

The file attributes for the new directory. You can set the owner and group numbers, file permissions, and modification date. If you specify `nil` for this parameter, the directory is created according to the umask(2) macOS Developer Tools Manual Page of the process. The Supporting Types section lists the global constants used as keys in the `attributes` dictionary. Some of the keys, such as [`hfsCreatorCode`](/documentation/Foundation/FileAttributeKey/hfsCreatorCode) and [`hfsTypeCode`](/documentation/Foundation/FileAttributeKey/hfsTypeCode), do not apply to directories.

## Discussion

If you specify `nil` for the `attributes` parameter, this method uses a default set of values for the owner, group, and permissions of any newly created directories in the path. Similarly, if you omit a specific attribute, the default value is used. The default values for newly created directories are as follows:

- Permissions are set according to the umask of the current process. For more information, see umask.
- The owner ID is set to the effective user ID of the process.
- The group ID is set to that of the parent directory.

If you want to specify a relative path for url, you must set the current working directory before creating the corresponding [`NSURL`](/documentation/Foundation/NSURL) object.

> Handling Errors in Swift:
> In Swift, this method returns `Void` and is marked with the `throws` keyword to indicate that it throws an error in cases of failure.
> 
> You call this method in a `try` expression and handle any errors in the `catch` clauses of a `do` statement, as described in [Error Handling](https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html) in [The Swift Programming Language](https://docs.swift.org/swift-book/) and `About Imported Cocoa Error Parameters`.

## See Also

[`setAttributes(_:ofItemAtPath:)`](/documentation/Foundation/FileManager/setAttributes(_:ofItemAtPath:))

Sets the attributes of the specified file or directory.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)