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
- 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
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
- 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
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
- Source directory not found
- Source file does not exist
- Directory not found
- Failed to organize files and rollback was incomplete
- Service type is not assignable to
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)