{"record":{"id":"17f22ac09da0a8b4","repo":"arduino/Arduino","slug":"the-sketch-already-contains-a-file-named-0","errorCode":null,"errorMessage":"The sketch already contains a file named \"{0}\"","messagePattern":"The sketch already contains a file named \"(.+?)\"","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"arduino-core/src/processing/app/Sketch.java","lineNumber":273,"sourceCode":"\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\n   *          existing file.\n   */\n  protected void checkNewFilename(File newFile) throws IOException {\n    // Verify that the sketch doesn't have a filem with the new name\n    // already, other than the current primary (index 0)\n    if (findFileIndex(newFile) >= 0) {\n      String msg = I18n.format(tr(\"The sketch already contains a file named \\\"{0}\\\"\"), newFile.getName());\n      throw new IOException(msg);\n    }\n\n  }\n\n  /**\n   * Rename this sketch' folder to the given name. Unlike saveAs(), this\n   * moves the sketch directory, not leaving anything in the old place.\n   * This operation does not *save* the sketch, so the files on disk are\n   * moved, but not modified.\n   *\n   * @param newFolder\n   *          The new folder name for this sketch. The new primary\n   *          file's name will be derived from this.\n   *\n   * @throws IOException\n   *           When a problem occurs. The error message should be\n   *           already translated.\n   */","sourceCodeStart":255,"sourceCodeEnd":291,"githubUrl":"https://github.com/arduino/Arduino/blob/a0df6e0e83b652c72bc78b0a1376c54d6ebc3bee/arduino-core/src/processing/app/Sketch.java#L255-L291","documentation":"checkNewFilename verifies the new file name is not already part of the sketch and throws IOException when findFileIndex locates it among existing sketch files. Called by rename operations and Sketch.addFile to keep sketch file names unique.","triggerScenarios":"Renaming a sketch tab to a name that another tab already uses, or addFile() importing a file whose name collides with an existing sketch file (other than the primary at index 0).","commonSituations":"Adding a .h/.cpp whose name matches an existing tab; renaming a tab to an existing tab name (e.g. \"sketch\" variants); importing files from a library folder containing duplicates.","solutions":["Pick a different name not used by any tab in the sketch","Delete the existing conflicting file first if it is obsolete","Before addFile, scan the sketch folder for a file with the same name","Uniquify programmatically (append a suffix) before adding"],"exampleFix":"// before\nsketchFile.renameTo(\"sensor.ino\"); // fails if sensor.ino exists\n// after\nString newName = \"sensor.ino\";\nif (sketch.folderFile(newName).exists()) {\n  newName = \"sensor_copy.ino\";\n}\nsketchFile.renameTo(newName);","handlingStrategy":"validation","validationCode":"boolean taken = Arrays.stream(sketch.getFiles())\n    .anyMatch(f -> f.getFileName().equals(newName));\nif (taken) throw new IllegalArgumentException(\"Name in use: \" + newName);","typeGuard":"static boolean nameIsFree(Sketch sketch, String newName) {\n  return sketch.findFileIndex(new File(sketch.getFolder(), newName)) < 0;\n}","tryCatchPattern":"try {\n  sketchFile.renameTo(newName);\n} catch (IOException e) {\n  System.err.println(\"Duplicate name: \" + e.getMessage());\n}","preventionTips":["Before addFile, scan existing tabs for the same name","Auto-uniquify names with a numeric suffix when importing","Keep tab names distinct from library file names you plan to import","Treat the primary sketch file's base name as reserved"],"tags":["naming-collision","sketch","validation"],"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-14T00:17:10.932Z"}