{"record":{"id":"7e2d8f79a17638fe","repo":"LykosAI/StabilityMatrix","slug":"could-not-move-file-to-destinationfile-because-it-already","errorCode":null,"errorMessage":"Could not move file to {destinationFile} because it already exists.","messagePattern":"Could not move file to (.+?) because it already exists\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Core/Models/FileInterfaces/FilePath.cs","lineNumber":220,"sourceCode":"    public async Task<FilePath> MoveToWithIncrementAsync(FilePath destinationFile, int maxTries = 100)\n    {\n        await Task.Yield();\n\n        var targetFile = destinationFile;\n\n        for (var i = 1; i < maxTries; i++)\n        {\n            if (!targetFile.Exists)\n            {\n                return await MoveToAsync(targetFile).ConfigureAwait(false);\n            }\n\n            targetFile = destinationFile.WithName(\n                destinationFile.NameWithoutExtension + $\" ({i})\" + destinationFile.Extension\n            );\n        }\n\n        throw new IOException($\"Could not move file to {destinationFile} because it already exists.\");\n    }\n\n    /// <summary>\n    /// Copy the file to a target path.\n    /// </summary>\n    public FilePath CopyTo(FilePath destinationFile, bool overwrite = false)\n    {\n        Info.CopyTo(destinationFile.FullPath, overwrite);\n        // Return the new path\n        return destinationFile;\n    }\n\n    /// <summary>\n    /// Copy the file to a target path asynchronously.\n    /// </summary>\n    public async Task<FilePath> CopyToAsync(FilePath destinationFile, bool overwrite = false)\n    {\n        await using var sourceStream = Info.OpenRead();","sourceCodeStart":202,"sourceCodeEnd":238,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Core/Models/FileInterfaces/FilePath.cs#L202-L238","documentation":"FilePath.MoveToWithIncrementAsync moves a file, appending ' (n)' suffixes to avoid collisions. If every candidate name up to its internal limit already exists, it gives up and throws IOException stating the destination already exists. It protects callers from silent overwrites when uniquification fails.","triggerScenarios":"Calling MoveToWithIncrementAsync(destinationFile) when the destination and all generated ' (n)' alternates already exist — e.g. a directory already containing many same-named files beyond the increment cap.","commonSituations":"Consolidating imported model images into a folder that already holds dozens of copies; repeated imports of the same checkpoint without cleanup.","solutions":["Clean up or archive older same-named files before moving","Generate the destination name with a timestamp/GUID instead of relying on increments","Catch IOException and move to a new subfolder or prompt the user","Raise the uniquification limit or use a custom naming scheme"],"exampleFix":"// before\nawait path.MoveToWithIncrementAsync(dest);\n// after\ntry { await path.MoveToWithIncrementAsync(dest); }\ncatch (IOException)\n{\n    dest = dest.WithName($\"{dest.NameWithoutExtension}_{DateTime.Now:yyyyMMddHHmmss}{dest.Extension}\");\n    await path.MoveToWithIncrementAsync(dest);\n}","handlingStrategy":"try-catch","validationCode":"if (File.Exists(destinationFile))\n    destinationFile = destinationFile.WithName($\"{destinationFile.NameWithoutExtension}_{Guid.NewGuid():N}{destinationFile.Extension}\");","typeGuard":null,"tryCatchPattern":"try { await path.MoveToWithIncrementAsync(dest); }\ncatch (IOException ex) { Logger.Warn(\"Move failed, all names taken: {Msg}\", ex.Message); /* fall back to new folder */ }","preventionTips":["Avoid repeated imports into the same directory without cleanup","Use timestamped destination names for bulk consolidations","Monitor destination directories for name-collision growth","Fall back to a new subfolder when uniquification fails"],"tags":["csharp","file-io","file-move"],"backgroundTag":"file-already-exists","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}