Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy items from Source to Destination if they don't already exist

Tags:

powershell

I have a pretty basic powershell copy script that copies items from a source folder to a destination folder. However this is moving way too much data, and I'd like to check if the filename already exists so that file can be ignored. I don't need this as complex as verifying created date/checksum/etc.

Currently it's along the lines of:

Copy-Item source destination -recurse
Copy-Item source2 destination2 -recurse

I'd imagine I need to add the Test-Path cmdlet, but I'm uncertain how to implement it.

like image 732
user2361820 Avatar asked Sep 18 '14 15:09

user2361820


People also ask

Will Robocopy overwrite existing files?

Robocopy normally overwrites those. :: /XO excludes existing files older than the copy in the source directory. Robocopy normally overwrites those. :: With the Changed, Older, and Newer classes excluded, Robocopy will exclude files existing in the destination directory.

Does copy-item overwrite by default?

By default when you run the PowerShell Copy-Item cmdlet, it will overwrite the file if it is already exists.

What PowerShell cmdlet is used to move a file from source to destination?

The Move-Item cmdlet moves an item, including its properties, contents, and child items, from one location to another location. The locations must be supported by the same provider. For example, it can move a file or subdirectory from one directory to another or move a registry subkey from one key to another.

How do you copy from one location to another?

Copy and paste filesRight-click and pick Copy, or press Ctrl + C . Navigate to another folder, where you want to put the copy of the file. Click the menu button and pick Paste to finish copying the file, or press Ctrl + V . There will now be a copy of the file in the original folder and the other folder.


2 Answers

You could always call ROBOCOPY from PowerShell for this.

Use the /xc (exclude changed) /xn (exclude newer) and /xo (exclude older) flags:

robocopy /xc /xn /xo source destination 

This will ONLY copy those files that are not in the destination folder.

For more option type robocopy /?

like image 87
Richard Avatar answered Sep 18 '22 13:09

Richard


$exclude = Get-ChildItem -recurse $dest
Copy-Item -Recurse $file $dest -Verbose -Exclude $exclude
like image 34
viktor Avatar answered Sep 20 '22 13:09

viktor