X Tutup
The Wayback Machine - https://web.archive.org/web/20210506162919/https://github.com/PowerShell/PowerShell/issues/13352
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move-Item explodes contents of first top-level folder #13352

Open
yohosuff opened this issue Aug 4, 2020 · 4 comments
Open

Move-Item explodes contents of first top-level folder #13352

yohosuff opened this issue Aug 4, 2020 · 4 comments

Comments

@yohosuff
Copy link

@yohosuff yohosuff commented Aug 4, 2020

Steps to reproduce

Run the script below in a new folder.

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

move-item -path "src\*" -Destination "dest"

It will create a simple folder structure like this...

image

...and then move the contents of src into a new folder dest.

Expected behavior

image

Actual behavior

We end up with a folder structure like this...

image

...as well as the following error.

image

I assume the error appears because when foo is moved, the contents are moved instead of the whole folder, so foo.txt is moved directly under dest. When the root level foo.txt is then attempted to be moved, there is already a foo.txt in its destination path.

Environment data

image

@mkht
Copy link

@mkht mkht commented Aug 5, 2020

In this case, Move-Item performs a two-step operation in sequence.
First, it RENAMES the src/foo folder to the dest folder.
Next, it tries to MOVE the src/foo.txt file to the dest folder. However, there is already a foo.txt file in the dest folder and an error occurs.

That is, Move-Item will "move" the item if the destination already exists, but otherwise it will "rename" the item.
I don't know if this behavior is intended or if it's a bug. Please wait for reply from PowerShell Pros. :-(

An easy workaround is to create a dest folder before you run Move-Item. This will allow you to achieve the behavior you expected.

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"
@jdhitsolutions
Copy link

@jdhitsolutions jdhitsolutions commented Aug 26, 2020

Couldn't Move-Item be modified to first create the destination if it doesn't exist? If it did, would that solve the original problem?

@iSazonov
Copy link
Collaborator

@iSazonov iSazonov commented Jan 17, 2021

I see we need to review move-item design. I don't think it works as such alternative tools.
/cc @mklement0 if you have an interest.

@mklement0
Copy link
Contributor

@mklement0 mklement0 commented Jan 17, 2021

We could follow the lead of the Unix mv utility:

  • If there are multiple source items, the target item must (a) exist and (b) must be a directory - otherwise an error occurs (that is, @mkht's workaround is the proper solution to avoid this problem).

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 -Force (though making -Force do many different things is problematic, I'm not aware of anything that it currently does in the file-system provider, so perhaps that is an option),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
X Tutup