- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Copy files or Folder without overwriting existing files?
To copy files/folders on the remote path without overwriting the existing files/folders, you can use multiple cmdlets like Copy-Item, Robocoy, and Xcopy, etc. As Copy-Item is a standard cmdlet, we will check if it's supported parameters can prevent overwriting.
If Copy-Item doesn’t work then we will check its alternate command. Copy-Item simply overwrites the files and folders on the destination path and the copies newer files.
For example, To copy files from the source folder C:\Test1 to the destination folder C:\Test2 below command is used and it simply overwrites the file without asking.
Example
Copy-Item C:\Test1\* C:\Test2 -Recurse -Verbose
Output
PS C:\Temp> Copy-Item C:\Test1\* C:\Test2 -Recurse -Verbose VERBOSE: Performing the operation "Copy File" on target "Item: C:\Test1\File1.txt Destination: C:\Test2\File1.txt". VERBOSE: Performing the operation "Copy File" on target "Item: C:\Test1\File2.txt Destination: C:\Test2\File2.txt".
We have another -Confirm parameter, but it just confirms from the user that if files need to copy on the destination folder or not. If we choose $True for -Confirm parameter then it will ask for each file if it needs to copy or not but if you have hundreds of files on the system then this method won’t work
Copy-Item C:\Test1\* -Destination C:\Test2\ -Confirm:$true -Verbose
PS C:\Temp> Copy-Item C:\Test1\* -Destination C:\Test2\ -Confirm:$true -Verbose Confirm Are you sure you want to perform this action? Performing the operation "Copy File" on target "Item: C:\Test1\File1.txt Destination: C:\Test2\File1.txt". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
With -Force parameter is for overwriting Read-only file content. So that is also not going to help so we can use another method like filtering existing files through Get-ChildItem and skipping them but that requires writing several codes and loop instead we have a robocopy command which supports preventing overwriting of files/ folder.
Robocopy C:\Test1\ C:\Test2\ /E /XC /XN /XO
Output
PS C:\> Robocopy C:\Test1\ C:\Test2\ /E /XC /XN /XO ------------------------------------------------------------------------------- ROBOCOPY :: Robust File Copy for Windows ------------------------------------------------------------------------------- Started : Saturday, August 29, 2020 2:15:33 PM Source : C:\Test1\ Dest : C:\Test2\ Files : *.* Options : *.* /S /E /DCOPY:DA /COPY:DAT /XO /XN /XC /R:1000000 /W:30 ------------------------------------------------------------------------------ 2 C:\Test1\ *EXTRA File 8 File3.txt 100% New File 11 File2.txt ------------------------------------------------------------------------------ Total Copied Skipped Mismatch FAILED Extras Dirs : 1 0 1 0 0 0 Files : 2 1 1 0 0 1 Bytes : 22 11 11 0 0 8 Times : 0:00:00 0:00:00 0:00:00 0:00:00 Speed : 1833 Bytes/sec. Speed : 0.104 MegaBytes/min. Ended : Saturday, August 29, 2020 2:15:33 PM
Switches are explained below.
/E − Copies child items (like -recursive in Copy-Command)
/XC − Prevents overwriting the files which have the same timestamp.
/XN − Prevents overwriting of files with the newer timestamp than the source files.
/XO − Prevents overwriting of files with the older timestamp than the source files.
- Related Articles
- How to copy files from one folder to another using Python?
- How to copy certain files from one folder to another using Python?
- How to copy only updated or newer files with PowerShell?
- How to update a MongoDB document without overwriting the existing one?
- How to write files to asset folder in android?
- How to copy multiple files in PowerShell using Copy-Item?
- How to copy a file, group of files, or directory in Linux?
- Python - How to Merge all excel files in a folder
- How to copy files from host to Docker container?
- How to upload files in Laravel directly into the public folder?
- How to copy files into a directory in C#?
- How to write files to the assets folder in android using Kotlin?
- How to copy files to a new directory using Python?
- How to read multiple text files from a folder in Python?(Tkinter)
- How to copy files with the specific extension in PowerShell?
