mkdir Mydir
Rename-Item .\Mydir\ MyDir
directory should be renamed
Rename-Item: Source and destination path must be different.
Name Value
---- -----
PSVersion 7.0.0
PSEdition Core
GitCommitId 7.0.0
OS Microsoft Windows 10.0.17134
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Looked into this a bit, I reckon this is a limitation from the DirectoryInfo.MoveTo() method being used to rename the directories.
if (ShouldProcess(resource, action))
{
// Now move the file
dir.MoveTo(newPath);
result = dir;
WriteItemObject(result, result.FullName, isContainer);
}
Looking over the docs, this may be by design:
The destination cannot be another disk volume or a directory with the identical name.
In my testing, this works fine when changing a file instead of a directory:
PS > rename-item testfile.txt tEsTfIlE.tXt
PS >
Doesn't look like FileInfo.MoveTo() has the same limitations
@jackdcasey You mean it's ok ? It isn't a bug ?
It's by design in terms of the API we're using to rename/move the directory. Note that we're using a MoveTo() API as there is no specific renaming API on either the DirectoryInfo class, nor the Directory static class.
A workaround here may be to do a manual check to determine if the new path or name we're moving the item to is the same name as the original, and just differs by case. If this is the case, we can have a fallback method to move it to a temporary location before moving it back under the desired new name.
But we have, for example, a mv command on Linux. There is also no special rename command there. And it works well with this case.
Sure. But this behaviour likely arose from the .NET behaviour originally on Windows. Rather than diverge the API surface, the .NET team probably elected to keep the current behaviour as-is even on Linux, to prevent the case where the same code behaves entirely differently depending on OS.
I don't know whether the underlying API could be improved on Windows as well, or if they could add a specific Rename() API for this kind of case; that question would be better directed at folks in the dotnet/runtime repository. 馃檪
@vexx32 Ok. I got it.
Thank you!
Most helpful comment
It's by design in terms of the API we're using to rename/move the directory. Note that we're using a
MoveTo()API as there is no specific renaming API on either the DirectoryInfo class, nor the Directory static class.A workaround here may be to do a manual check to determine if the new path or name we're moving the item to is the same name as the original, and just differs by case. If this is the case, we can have a fallback method to move it to a temporary location before moving it back under the desired new name.