arduino/Arduino · error · IOException

'Unable to create folder: ' + destFile

Error message

'Unable to create folder: ' + destFile

What it means

FileUtils.copy recursively copies a folder tree and throws IOException "Unable to create folder: <path>" when File.mkdir() fails for a needed subdirectory. This typically happens when the destination is not writable, a same-named file already exists, or a parent directory is missing. The message names the exact directory that could not be created.

Solutions

  1. Check that the destination folder exists and is writable by the current user
  2. Delete/rename any FILE that has the same name as the folder being created
  3. Create parent directories first (destFolder.mkdirs()) before copying
  4. Run with sufficient privileges or choose a writable destination

Example fix

// before
File destFolder = new File(sketchbook, "libraries/MyLib");
FileUtils.copy(src, destFolder);
// after
if (!destFolder.exists() && !destFolder.mkdirs()) {
  throw new IOException("Cannot create destination " + destFolder);
}
FileUtils.copy(src, destFolder);
Defensive patterns

Strategy: validation

Validate before calling

if (!destFolder.isDirectory()) destFolder.mkdirs();
if (!destFolder.canWrite()) throw new IOException("Not writable: " + destFolder);
File probe = new File(destFolder, sourceFolder.listFiles()[0].getName());
if (probe.exists() && probe.isFile() && sourceFolder.listFiles()[0].isDirectory())
  throw new IOException("File blocks folder: " + probe);

Type guard

boolean canCopyInto(File dest) {
  return dest != null && (dest.isDirectory() || dest.mkdirs()) && dest.canWrite();
}

Try / catch

try {
  FileUtils.copy(src, dest);
} catch (IOException e) {
  if (e.getMessage().startsWith("Unable to create folder"))
    fixPermissionsOrPath(new File(e.getMessage().replace("Unable to create folder: ", "")));
  else throw e;
}

Prevention

When it happens

Trigger: Calling FileUtils.copy(sourceFolder, destFolder) when a subdirectory destFolder/<name> does not exist and destFile.mkdir() returns false (no write permission on destFolder, a file with the same name exists, or destFolder itself is missing/nonexistent).

Common situations: Copying example libraries or sketches into a read-only sketchbook; destination path where a FILE exists with the folder's name; copying into a path whose parent was deleted or not yet created; running without permissions (Program Files on Windows, root-owned dirs on Linux).

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/8535ce123ccdd22f. Report an issue: GitHub.

Appendix: source

Thrown at arduino-core/src/processing/app/helpers/FileUtils.java:72

      fis = new FileInputStream(source);
      fos = new FileOutputStream(dest);
      byte[] buf = new byte[4096];
      int readBytes = -1;
      while ((readBytes = fis.read(buf, 0, buf.length)) != -1) {
        fos.write(buf, 0, readBytes);
      }
    } finally {
      IOUtils.closeQuietly(fis);
      IOUtils.closeQuietly(fos);
    }
  }

  public static void copy(File sourceFolder, File destFolder) throws IOException {
    for (File file : sourceFolder.listFiles()) {
      File destFile = new File(destFolder, file.getName());
      if (file.isDirectory() && !SOURCE_CONTROL_FOLDERS.contains(file.getName())) {
        if (!destFile.exists() && !destFile.mkdir()) {
          throw new IOException("Unable to create folder: " + destFile);
        }
        copy(file, destFile);
      } else if (!file.isDirectory()) {
        copyFile(file, destFile);
      }
    }
  }

  public static void recursiveDelete(File file) {
    if (file == null) {
      return;
    }
    if (file.isDirectory()) {
      File[] files = file.listFiles();
      if (files == null) {
        return;
      }
      for (File current : files) {

View on GitHub (pinned to a0df6e0e83)