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

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.

This question is related to batch-file copy

The answer is


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.


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


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


this works fine

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

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.


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.


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


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)

this works fine

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

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


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.


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.


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.

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.


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.


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.


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.

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

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)

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.


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


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


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.


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


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.


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


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.


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.

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.


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

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.


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.