arduino/Arduino · error · IOException

Could not create directory "{0}"

Error message

Could not create directory "{0}"

What it means

saveAs validates the target folder, then creates it with mkdirs(); this error is thrown when mkdirs() returns false and the sketch directory could not be created. It means the save destination could not be materialized on disk.

Source

Thrown at arduino-core/src/processing/app/Sketch.java:343

   * Save this sketch under the new name given. Unlike renameTo(), this
   * leaves the existing sketch in place.
   *
   * @param newFolder
   *          The new folder name for this sketch. The new primary
   *          file's name will be derived from this.
   *
   * @throws IOException
   *           When a problem occurs. The error message should be
   *           already translated.
   */
  public void saveAs(File newFolder) throws IOException {
    // Check intented rename (throws if there is a problem)
    File newPrimary = checkNewFoldername(newFolder);

    // Create the folder
    if (!newFolder.mkdirs()) {
      String msg = I18n.format(tr("Could not create directory \"{0}\""), newFolder.getAbsolutePath());
      throw new IOException(msg);
    }

    // Save the files to their new location
    for (SketchFile file : files) {
      if (file.isPrimary())
        file.saveAs(newPrimary);
      else
        file.saveAs(new File(newFolder, file.getFileName()));
    }


    // Copy the data folder (this may take a while.. add progress bar?)
    if (getDataFolder().exists()) {
      File newDataFolder = new File(newFolder, "data");
      // Check if data folder exits, if not try to create the data folder
      if (!newDataFolder.exists() && !newDataFolder.mkdirs()) {
        String msg = I18n.format(tr("Could not create directory \"{0}\""), newFolder.getAbsolutePath());
        throw new IOException(msg);

View on GitHub (pinned to a0df6e0e83)

Solutions

  1. Verify the parent directory exists and is writable before saving
  2. Ensure no regular file occupies the target path
  3. Pick a location with write access (Documents, home directory)
  4. Check disk space and path length/character restrictions

Example fix

// before
sketch.saveAs(new File("/readonly", "MySketch"));
// after
File target = new File("/readonly", "MySketch");
File parent = target.getParentFile();
if (parent != null && parent.canWrite() && !target.exists()) {
  sketch.saveAs(target);
} else {
  System.err.println("Cannot create " + target);
}
Defensive patterns

Strategy: validation

Validate before calling

File parent = target.getParentFile();
if (parent == null || !parent.isDirectory() || !parent.canWrite() || target.exists()) {
  throw new IllegalArgumentException("Cannot create " + target);
}

Type guard

static boolean canCreateDir(File target) {
  File p = target.getParentFile();
  return !target.exists() && p != null && p.isDirectory() && p.canWrite();
}

Try / catch

try {
  sketch.saveAs(newFolder);
} catch (IOException e) {
  System.err.println("Save failed, cannot create dir: " + newFolder);
}

Prevention

When it happens

Trigger: Sketch.saveAs(newFolder) where newFolder cannot be created: parent directory missing/unwritable, a file (not a directory) already exists at the path, or a path length/permission problem.

Common situations: Saving into a read-only location (Program Files, read-only USB stick); saving to a path whose parent doesn't exist or is a file; insufficient disk space or quotas; invalid characters in the path on Windows.

Understand the failure class

Background: mkdir permission denied (EACCES): failed to create directory errors explained — this error's family across 32 libraries.

Related errors


AI-assisted analysis of arduino/Arduino@a0df6e0e83 (2026-09-06). Data as JSON: /api/errors/60a09768f1d9bd64. Report an issue: GitHub.