{"record":{"id":"ceac8a3deb725725","repo":"arduino/Arduino","slug":"sorry-the-folder-0-already-exists","errorCode":null,"errorMessage":"Sorry, the folder \"{0}\" already exists.","messagePattern":"Sorry, the folder \"(.+?)\" already exists\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"arduino-core/src/processing/app/Sketch.java","lineNumber":247,"sourceCode":"        return i;\n      i++;\n    }\n    return -1;\n  }\n\n  /**\n   * Check if renaming/saving this sketch to the given folder would\n   * cause a problem because: 1. The new folder already exists 2.\n   * Renaming the primary file would cause a conflict with an existing\n   * file. If so, an IOEXception is thrown. If not, the name of the new\n   * primary file is returned.\n   */\n  protected File checkNewFoldername(File newFolder) throws IOException {\n    String newPrimary = FileUtils.addExtension(newFolder.getName(), DEFAULT_SKETCH_EXTENSION);\n    // Verify the new folder does not exist yet\n    if (newFolder.exists()) {\n      String msg = I18n.format(tr(\"Sorry, the folder \\\"{0}\\\" already exists.\"), newFolder.getAbsoluteFile());\n      throw new IOException(msg);\n    }\n\n    // If the folder is actually renamed (as opposed to moved somewhere\n    // else), check for conflicts using the new filename, but the\n    // existing folder name.\n    if (!newFolder.getName().equals(folder.getName()))\n      checkNewFilename(new File(folder, newPrimary));\n\n    return new File(newFolder, newPrimary);\n  }\n\n  /**\n   * Check if renaming or adding a file would cause a problem because\n   * the file already exists in this sketch. If so, an IOEXception is\n   * thrown.\n   *\n   * @param newFile\n   *          The filename of the new file, or the new name for an","sourceCodeStart":229,"sourceCodeEnd":265,"githubUrl":"https://github.com/arduino/Arduino/blob/a0df6e0e83b652c72bc78b0a1376c54d6ebc3bee/arduino-core/src/processing/app/Sketch.java#L229-L265","documentation":"checkNewFoldername validates the target folder for a rename/saveAs and throws IOException if the destination folder already exists on disk. This prevents overwriting or merging into an existing sketch directory.","triggerScenarios":"Sketch.renameTo() or Sketch.saveAs() called with a File path for a folder that already exists on the filesystem.","commonSituations":"Renaming a sketch to a name that collides with an existing sketch in the same directory; saveAs to a folder created earlier by a failed save; case-insensitive filesystems colliding on names differing only by case (Windows/macOS).","solutions":["Choose a different folder name that does not exist yet","Delete or move the existing folder if it is no longer needed","Check File.exists() before calling renameTo/saveAs","If case-insensitivity is the issue, pick a name unique ignoring case"],"exampleFix":"// before\nsketch.saveAs(new File(sketchesDir, \"Blink\")); // fails if Blink exists\n// after\nFile target = new File(sketchesDir, \"Blink\");\nif (target.exists()) target = new File(sketchesDir, \"Blink2\");\nsketch.saveAs(target);","handlingStrategy":"validation","validationCode":"if (target.exists()) {\n  throw new IllegalArgumentException(\"Target folder exists: \" + target);\n}","typeGuard":"static boolean isFreeFolderName(File parent, String name) {\n  return !new File(parent, name).exists();\n}","tryCatchPattern":"try {\n  sketch.renameTo(newFolder);\n} catch (IOException e) {\n  System.err.println(\"Name collision: \" + e.getMessage());\n  // prompt user for a different name\n}","preventionTips":["Always File.exists() the destination before renameTo/saveAs","Generate names with a uniqueness loop (name, name-1, name-2...)","Remember Windows/macOS filesystems are case-insensitive","Clean up folders left by failed save attempts"],"tags":["filesystem","rename","collision"],"backgroundTag":"file-already-exists","analyzedSha":"a0df6e0e83b652c72bc78b0a1376c54d6ebc3bee","analyzedAt":"2026-09-06T10:13:38.901Z","contentChangedAt":"2026-09-06T10:13:38.901Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}