windows 如何编写将一个目录复制到另一个目录并替换旧文件的批处理脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9904608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:23:53  来源:igfitidea点击:

How do I write a batch script that copies one directory to another, replaces old files?

windowsbatch-file

提问by NomenNescio

I'd like a batch script in Windows with which I can copy one directory to another. If this directory already exists, and then for each file that already exists in both with the same name and location, it should be overwritten, if it does not exists, it should just be added.

我想要一个 Windows 中的批处理脚本,我可以用它将一个目录复制到另一个目录。如果这个目录已经存在,那么对于已经存在的具有相同名称和位置的每个文件,它应该被覆盖,如果它不存在,它应该被添加。

In the end it should be a batch script to which I can pass 2 arguments, source & destination.

最后它应该是一个批处理脚本,我可以向它传递 2 个参数,源和目标。

回答by Habib

In your batch file do this

在您的批处理文件中执行此操作

set source=C:\Users\Habib\test
set destination=C:\Users\Habib\testdest\
xcopy %source% %destination% /y

If you want to copy the sub directories including empty directoriesthen do:

如果要复制包含空目录的子目录,请执行以下操作:

xcopy %source% %destination% /E /y

If you only want to copy sub directories and not empty directories then use /slike:

如果您只想复制子目录而不是空目录,请使用以下命令/s

xcopy %source% %destination% /s /y

回答by Uri

It seems that the latest function for this in windows 7 is robocopy.

似乎 Windows 7 中的最新功能是 robocopy。

Usage example:

用法示例:

robocopy <source> <destination> /e /xf <file to exclude> <another file>

/e copies subdirectories including empty ones, /xf excludes certain files from being copied.

/e 复制子目录,包括空目录,/xf 将某些文件排除在复制之外。

More options here: http://technet.microsoft.com/en-us/library/cc733145(v=ws.10).aspx

更多选项在这里:http: //technet.microsoft.com/en-us/library/cc733145(v=ws.10).aspx

回答by pdwalker

Have you considered using the "xcopy" command?

您是否考虑过使用“xcopy”命令?

The xcopy command will do all that for you.

xcopy 命令将为您完成所有这些工作。

回答by Bali C

Try this:

尝试这个:

xcopy %1 %2 /y /e

xcopy %1 %2 /y /e

The %1and %2are the source and destination arguments you pass to the batch file. i.e. C:\MyBatchFile.bat C:\CopyMe D:\ToHere

%1%2你传递给批处理文件的源和目标参数。IEC:\MyBatchFile.bat C:\CopyMe D:\ToHere

回答by adarshr

Just use xcopy /y source destination

只需使用 xcopy /y source destination