How to say no to all "do you want to overwrite" prompts in a batch file copy?

Batch FileCopy

Batch File Problem Overview


By default, copying from the command prompt will prompt you to overwrite files that already exist in the target location.

You can add "/Y" to say "Yes to all" replacements.

But how can you say "No to all" ?

In other words, I want to copy everything from one directory that does not already exist in the target.

The closest thing I see is the XCOPY argument to only copy things after a specific mod-datetime.

Batch File Solutions


Solution 1 - Batch File

Unless there's a scenario where you'd not want to copy existing files in the source that have changed since the last copy, why not use XCOPY with /D without specifying a date?

Solution 2 - Batch File

echo "No" | copy/-Y c:\source c:\Dest\

Solution 3 - Batch File

You can make a text file with a single long line of "n" then run your command and put < nc.txt after it. I did this to copy over 145,000 instances where "No overwrite" was what I wanted and it worked fine this way.

Or you can just hold the n key down with something, but that takes longer than using the < to pipe it in.

Solution 4 - Batch File

Here's a workaround. If you want to copy everything from A that does not already exist in B:

Copy A to a new directory C. Copy B to C, overwriting anything that overlaps with A. Copy C to B.

Solution 5 - Batch File

I use XCOPY with the following parameters for copying .NET assemblies:

/D /Y /R /H 

/D:m-d-y - Copies files changed on or after the specified date. If no date is given, copies only those files whose source time is newer than the destination time.

/Y - Suppresses prompting to confirm you want to overwrite an existing destination file.

/R - Overwrites read-only files.

/H - Copies hidden and system files also.

Solution 6 - Batch File

I know you all think /D: date is going to use date stuff, but just /D without the: does exactly what we want so...

xcopy {Source} {Destination} /E /D 

Will copy without overwriting to pickup those files that are new or maybe failed before for some reason.

Just try it, it works.

Solution 7 - Batch File

I expect xxcopy has an option for that.

Bingo:

http://www.xxcopy.com/xxcopy27.htm#tag_231

2.3   By comparison with the file in destination

    The switches in this group select files based on the
    comparison between the files in the source and those in
    the destination.  They are often used for periodic backup
    and directory synchronization purposes. These switches
    were originally created as variations of directory backup.
    They are also convenient for selecting files for deletion.

2.3.1  by Presence/Absence

    The /BB and /U switches are the two switches which select
    files by the pure presence or absence as the criteria.
    Other switches in the this group (Group 2.3) are also
    affected by the file in the destination, but for a
    particular characteristics for comparison's sake.

    /BB  Selects files that are present in source but not in destination.
    /U   Selects files that are present in both source and destination.

-Adam

Solution 8 - Batch File

this works fine

no | cp -rf c:\source c:\Dest\

Solution 9 - Batch File

echo N | copy /-y $(SolutionDir)SomeDir $(OutDir)

Solution 10 - Batch File

Try this:

robocopy "source" "destination" /e /b /copyall /xo /it

Copy that line into notepad and save as a .bat file. Run the file and it will copy everything from the source to the destination. When you run it again it will not replace files that are identical. when you change or a file changes it will replace the file at the destination.

test it out. I created a .txt file with a few works, ran the script, change the wording on the .txt file and ran the script again, it replace only the change file from the source.

/e=Copies subdirectories. Note that this option includes empty directories
/b=Copies files in Backup mode
/copyall=Copies all file information
/xo=Excludes older files. (this is what prevents it from copy the same file over and over)
/it=Includes "tweaked" files. (this will allow the copy and replace of modified files)

Solution 11 - Batch File

Thanks for this. I am using the command line utility AzCopy (v 3.1.0.93) to move ~1 million files from my local PC to my Azure blob storage. I've got some duplicates and cannot babysit the copy to answer each prompt and don't want to re-upload the same file.

The AzCopy utility offers a /Y command to suppress the confirmation prompts but ends up telling it to overwrite the destination file. Going this route I was able to get it to NOT re-upload the file. However, it does seem like a bit of a hack since it is not actually answering the prompt with "No", instead I get the error "No input is received when user needed to make a choice among several given options." but does not upload the file.

Here is the command I used: echo n | AzCopy /Source:"{file path}" /Dest:"{blob storage URL}" /DestKey:{key}

Hope this helps the next guy.

Solution 12 - Batch File

Depending on the size and number of files being copied, you could copy the destination directory over the source first with "yes to all", then do the original copy you were doing, also with "yes to all" set. That should give you the same results.

Solution 13 - Batch File

We used "robocopy" through "invoke-command" to copy a huge amount of VMs in our environment. We've discovered that "robocopy" unexpectedly exits sometimes and the whole proccess goes to down. So we've decided to use "xcopy". Now we're checking it's work and to "create" "Not for all" option we use that function (powershell):

function gen_long_no([string]$path) { $result = ""; Get-ChildItem $path -Recurse | ? { if ($_.PSIsContainer -eq $false) { $result += "n" } }; return $result }

Maybe helps somebody.

Solution 14 - Batch File

Adding the switches for subdirectories and verification work just fine. echo n | xcopy/-Y/s/e/v c:\source*.* c:\Dest\

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJosephStyonsView Question on Stackoverflow
Solution 1 - Batch FileKevView Answer on Stackoverflow
Solution 2 - Batch FilekrishnanView Answer on Stackoverflow
Solution 3 - Batch FileChrisView Answer on Stackoverflow
Solution 4 - Batch FileLiamView Answer on Stackoverflow
Solution 5 - Batch FilechitzaView Answer on Stackoverflow
Solution 6 - Batch FileEddie MiddlebrooksView Answer on Stackoverflow
Solution 7 - Batch FileAdam DavisView Answer on Stackoverflow
Solution 8 - Batch FileghostCoderView Answer on Stackoverflow
Solution 9 - Batch FileLufiView Answer on Stackoverflow
Solution 10 - Batch FileJeff TondreauView Answer on Stackoverflow
Solution 11 - Batch Fileammills01View Answer on Stackoverflow
Solution 12 - Batch FiletloachView Answer on Stackoverflow
Solution 13 - Batch FileblinkerView Answer on Stackoverflow
Solution 14 - Batch FileJohn SalterView Answer on Stackoverflow