Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I rename/move items and override even if they exist (for both files, folders and links)?

Is there one command to move items (or just rename as both source and destination are in one folder) with forcing overwrite that does work for any item types (leaf, container)?

Background: I am writing a script that replaces all hardlinks and junctions with respective relative symlinks. Sample code:

mkdir C:\Temp\foo -ErrorAction SilentlyContinue
'example' > C:\Temp\foo\bar.txt
cd C:\Temp
New-Item -ItemType Junction -Name bar -Target C:\Temp\foo
New-Item -ItemType SymbolicLink -Name bar2 -Target '.\foo'

# Produces error: Rename-Item : Cannot create a file when that file already exists.
Rename-Item -Path 'C:\Temp\bar2' -newName 'bar' -force

# Unexpected behaviour: moves bar2 inside bar
Move-item -Path 'C:\Temp\bar2' -destination 'C:\Temp\bar' -force

# This works as per https://github.com/PowerShell/PowerShell/issues/621
[IO.Directory]::Delete('C:\Temp\bar')
Rename-Item -Path 'C:\Temp\bar2' -newName 'bar'
like image 608
Anton Krouglov Avatar asked Feb 27 '17 12:02

Anton Krouglov


1 Answers

I think you're looking for the UI experience of having the option to overwrite files and merge directories. These are just sophisticated error handling mechanisms to account for the same errors that you are seeing, thanks to Microsoft's thoughtful engineers.

mkdir C:\Temp\foo -ErrorAction SilentlyContinue
'example' > C:\Temp\foo\bar.txt
cd C:\Temp
New-Item -ItemType Junction -Name bar -Target C:\Temp\foo
New-Item -ItemType SymbolicLink -Name bar2 -Target '.\foo'

# Produces error: Rename-Item : Cannot create a file when that file already exists.
Rename-Item -Path 'C:\Temp\bar2' -newName 'bar' -force

This makes sense. You have two distinct objects, so they cannot have the same identifier. It would be like trying to point to two different objects

# Unexpected behaviour: moves bar2 inside bar
Move-item -Path 'C:\Temp\bar2' -destination 'C:\Temp\bar' -force

This is not unexpected. When you specify a directory for the destination, it treats it as the target directory which the moved item should be placed inside.

# This works as per https://github.com/PowerShell/PowerShell/issues/621
[IO.Directory]::Delete('C:\Temp\bar')
Rename-Item -Path 'C:\Temp\bar2' -newName 'bar'

This is essentially what the thoughtful Microsoft engineers have done for you through their UI for merging folders and overwriting files.

Note that this is the same behavior for the .NET method in System.IO as well

like image 137
Jonathon Anderson Avatar answered Oct 08 '22 04:10

Jonathon Anderson