LykosAI/StabilityMatrix · error · IOException

Could not move file to

Error message

Could not move file to {destinationFile} because it already exists.

What it means

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.

Solutions

  1. Clean up or archive older same-named files before moving
  2. Generate the destination name with a timestamp/GUID instead of relying on increments
  3. Catch IOException and move to a new subfolder or prompt the user
  4. Raise the uniquification limit or use a custom naming scheme

Example fix

// before
await path.MoveToWithIncrementAsync(dest);
// after
try { await path.MoveToWithIncrementAsync(dest); }
catch (IOException)
{
    dest = dest.WithName($"{dest.NameWithoutExtension}_{DateTime.Now:yyyyMMddHHmmss}{dest.Extension}");
    await path.MoveToWithIncrementAsync(dest);
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (File.Exists(destinationFile))
    destinationFile = destinationFile.WithName($"{destinationFile.NameWithoutExtension}_{Guid.NewGuid():N}{destinationFile.Extension}");

Try / catch

try { await path.MoveToWithIncrementAsync(dest); }
catch (IOException ex) { Logger.Warn("Move failed, all names taken: {Msg}", ex.Message); /* fall back to new folder */ }

Prevention

When it happens

Trigger: 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.

Common situations: Consolidating imported model images into a folder that already holds dozens of copies; repeated imports of the same checkpoint without cleanup.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12). Data as JSON: /api/errors/7e2d8f79a17638fe. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Core/Models/FileInterfaces/FilePath.cs:220

    public async Task<FilePath> MoveToWithIncrementAsync(FilePath destinationFile, int maxTries = 100)
    {
        await Task.Yield();

        var targetFile = destinationFile;

        for (var i = 1; i < maxTries; i++)
        {
            if (!targetFile.Exists)
            {
                return await MoveToAsync(targetFile).ConfigureAwait(false);
            }

            targetFile = destinationFile.WithName(
                destinationFile.NameWithoutExtension + $" ({i})" + destinationFile.Extension
            );
        }

        throw new IOException($"Could not move file to {destinationFile} because it already exists.");
    }

    /// <summary>
    /// Copy the file to a target path.
    /// </summary>
    public FilePath CopyTo(FilePath destinationFile, bool overwrite = false)
    {
        Info.CopyTo(destinationFile.FullPath, overwrite);
        // Return the new path
        return destinationFile;
    }

    /// <summary>
    /// Copy the file to a target path asynchronously.
    /// </summary>
    public async Task<FilePath> CopyToAsync(FilePath destinationFile, bool overwrite = false)
    {
        await using var sourceStream = Info.OpenRead();

View on GitHub (pinned to af93d6ef57)