Move-Item explodes contents of first top-level folder #13352
Comments
|
In this case, Move-Item performs a two-step operation in sequence. That is, Move-Item will "move" the item if the destination already exists, but otherwise it will "rename" the item. An easy workaround is to create a if (Test-Path src){
remove-item src -force -recurse
}
if (Test-Path dest){
remove-item dest -force -recurse
}
New-Item -ItemType directory -Path src | out-null
New-Item -ItemType directory -Path src\foo | out-null
New-Item src\foo.txt | out-null
New-Item src\foo\foo.txt | out-null
# Create a dest folder explicitly
New-Item -ItemType directory -Path dest | out-null
# Then, move items
move-item -path "src\*" -Destination "dest" |
|
Couldn't |
|
I see we need to review move-item design. I don't think it works as such alternative tools. |
|
We could follow the lead of the Unix
Additionally, @jdhitsolutions' proposal - creation of a target folder on demand (unless a file by that name already exists) - should probably be opt-in, via a switch such as |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

Steps to reproduce
Run the script below in a new folder.
It will create a simple folder structure like this...
...and then move the contents of
srcinto a new folderdest.Expected behavior
Actual behavior
We end up with a folder structure like this...
...as well as the following error.
I assume the error appears because when
foois moved, the contents are moved instead of the whole folder, sofoo.txtis moved directly underdest. When the root levelfoo.txtis then attempted to be moved, there is already afoo.txtin its destination path.Environment data
The text was updated successfully, but these errors were encountered: