Home > Enterprise >  Copy-Item with overwrite?
Copy-Item with overwrite?

Time:06-29

Here is a section of code from a larger script. The goal is to recurse through a source directory, then copy all the files it finds into a destination directory, sorted into subdirectories by file extension. It works great the first time I run it. If I run it again, instead of overwriting existing files, it fails with this error on each file that already exists in the destination:

Copy-Item : Cannot overwrite the item with itself

I try, whenever possible, to write scripts that are idempotent but I havn't been able to figure this one out. I would prefer not to add a timestamp to the destination file's name; I'd hate to end up with thirty versions of the exact same file. Is there a way to do this without extra logic to check for a file's existance and delete it if it's already there?

## Parameters for source and destination directories.
$Source = "C:\Temp"
$Destination = "C:\Temp\Sorted"

# Build list of files to sort.
$Files = Get-ChildItem -Path $Source -Recurse | Where-Object { !$_.PSIsContainer }

# Copy the files in the list to destination folder, sorted in subfolders by extension.
foreach ($File in $Files) {
    $Extension = $File.Extension.Replace(".","")
    $ExtDestDir = "$Destination\$Extension"

    # Check to see if the folder exists, if not create it
    $Exists = Test-Path $ExtDestDir

    if (!$Exists) {
        # Create the directory because it doesn't exist
        New-Item -Path $ExtDestDir -ItemType "Directory" | Out-Null
    }
    
    # Copy the file
    Write-Host "Copying $File to $ExtDestDir"
    Copy-Item -Path $File.FullName -Destination $ExtDestDir -Force
}

CodePudding user response:

$Source = "C:\Temp"
$Destination = "C:\Temp\Sorted"

You are trying to copy files from a source directory to a sub directory of that source directory. The first time it works because that directory is empty. The second time it doesn't because you are enumerating files of that sub directory too and thus attempt to copy files over themselves.

If you really need to copy the files into a sub directory of the source directory, you have to exclude the destination directory from enumeration like this:

$Files = Get-ChildItem -Path $Source -Directory | 
         Where-Object { $_.FullName -ne $Destination } |
         Get-ChildItem -File -Recurse

Using a second Get-ChildItem call at the beginning, which only enumerates first-level directories, is much faster than filtering the output of the Get-ChildItem -Recurse call, which would needlessly process each file of the destination directory.

  • Related