{"record":{"id":"47b5eb2cec04dc8c","repo":"OrchardCMS/OrchardCore","slug":"cannot-move-file-because-the-new-path-newpath-already-exists","errorCode":null,"errorMessage":"Cannot move file because the new path '{newPath}' already exists.","messagePattern":"Cannot move file because the new path '(.+?)' already exists\\.","errorType":"exception","errorClass":"FileStoreException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.FileStorage.FileSystem/FileSystemStore.cs","lineNumber":258,"sourceCode":"        }\n    }\n\n    public Task MoveFileAsync(string oldPath, string newPath)\n    {\n        try\n        {\n            var physicalOldPath = GetPhysicalPath(oldPath);\n\n            if (!File.Exists(physicalOldPath))\n            {\n                throw new FileStoreException($\"Cannot move file '{oldPath}' because it does not exist.\");\n            }\n\n            var physicalNewPath = GetPhysicalPath(newPath);\n\n            if (File.Exists(physicalNewPath) || Directory.Exists(physicalNewPath))\n            {\n                throw new FileStoreException($\"Cannot move file because the new path '{newPath}' already exists.\");\n            }\n\n            File.Move(physicalOldPath, physicalNewPath);\n\n            return Task.CompletedTask;\n        }\n        catch (FileStoreException)\n        {\n            throw;\n        }\n        catch (Exception ex)\n        {\n            throw new FileStoreException($\"Cannot move file '{oldPath}' to '{newPath}'.\", ex);\n        }\n    }\n\n    public Task CopyFileAsync(string srcPath, string dstPath)\n    {","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.FileStorage.FileSystem/FileSystemStore.cs#L240-L276","documentation":"MoveFileAsync refuses to overwrite: if a file OR directory already exists at newPath it throws FileStoreException. File.Move would otherwise throw anyway; the store pre-checks to give a clear message and to prevent clobbering directories.","triggerScenarios":"Calling MoveFileAsync(oldPath, newPath) where a file or folder already occupies newPath, e.g. renaming 'a.txt' to 'b.txt' when 'b.txt' exists, or to a path that is an existing directory.","commonSituations":"Media library rename colliding with an existing asset; two users renaming concurrently; generating the target name from user input without uniqueness checks.","solutions":["Delete or rename the existing destination first (TryDeleteFileAsync / TryDeleteDirectoryAsync).","Generate a unique destination name (append a counter or GUID).","Check GetFileInfoAsync/GetDirectoryInfoAsync on newPath before moving."],"exampleFix":"// before\nawait store.MoveFileAsync(\"a.txt\", \"b.txt\"); // b.txt exists\n// after\nif (await store.GetFileInfoAsync(\"b.txt\") is not null)\n    await store.TryDeleteFileAsync(\"b.txt\");\nawait store.MoveFileAsync(\"a.txt\", \"b.txt\");","handlingStrategy":"validation","validationCode":"if (await store.GetFileInfoAsync(newPath) is not null || await store.GetDirectoryInfoAsync(newPath) is not null)\n    newPath = GenerateUniqueName(newPath); // append counter/GUID before moving","typeGuard":null,"tryCatchPattern":"try { await store.MoveFileAsync(oldPath, newPath); }\ncatch (FileStoreException ex) when (ex.Message.Contains(\"already exists\"))\n{\n    // pick a different destination or delete the existing one\n}","preventionTips":["Generate unique destination names (GUID suffix) for user-driven renames.","Pre-check the destination with GetFileInfoAsync/GetDirectoryInfoAsync.","Never move onto a path used as a directory."],"tags":["filesystem","file-store","destination-exists"],"backgroundTag":"file-already-exists","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}