windows 批处理/bat 一次复制文件夹和内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5062503/
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 08:05:20  来源:igfitidea点击:

batch/bat to copy folder and content at once

windowsbatch-filecopydirectory

提问by zmol

I'm writing a batch script that does a copy. I want to script it to copy an entire folder. When I want to copy a single file, I do this

我正在编写一个执行副本的批处理脚本。我想编写脚本来复制整个文件夹。当我想复制单个文件时,我这样做

copy %~dp0file.txt file.txt 

If I have a folder with this structure, is there a command to copy this entire folder with its contents all at once while preserving the exact structure.

如果我有一个具有这种结构的文件夹,是否有一个命令可以一次复制整个文件夹及其内容,同时保留确切的结构。

mainfolder/
  file1.txt
  file2.txt
  insidefolder/
     file3.txt
     file4.txt 
     file5.txt

回答by akf

if you have xcopy, you can use the /Eparam, which will copy directories and subdirectories and the files within them, including maintaining the directory structure for empty directories

如果有xcopy,则可以使用/E参数,它将复制目录和子目录以及其中的文件,包括维护空目录的目录结构

xcopy [source] [destination] /E

回答by Vertexwahn

The old way:

旧方法:

xcopy [source] [destination] /E

xcopyis deprecated. Robocopy replaces Xcopy. It comes with Windows 8, 8.1 and 10.

xcopy弃用。Robocopy 取代了 Xcopy。它带有 Windows 8、8.1 和 10。

robocopy [source] [destination] /E

robocopyhas several advantages:

robocopy有几个优点:

  • copy paths exceeding 259 characters
  • multithreaded copying
  • 复制路径超过 259 个字符
  • 多线程复制

More details here.

更多细节在这里

回答by Cody Gray

I suspect that the xcopycommand is the magic bullet you're looking for.

我怀疑该xcopy命令是您正在寻找的灵丹妙药。

It can copy files, directories, and even entire drives while preserving the original directory hierarchy. There are also a handful of additional options available, compared to the basic copycommand.

它可以复制文件、目录甚至整个驱动器,同时保留原始目录层次结构。与基本copy命令相比,还有一些额外的选项可用。

Check out the documentation here.

此处查看文档

If your batch file only needs to run on Windows Vista or later, you can use robocopyinstead, which is an even more powerful tool than xcopy, and is now built into the operating system. It's documentation is available here.

如果您的批处理文件只需要在 Windows Vista 或更高版本上运行,您可以robocopy改用它,这是一个比 更强大的工具xcopy,现在已内置到操作系统中。它的文档可以在这里找到

回答by Gayan Chinthaka Dharmarathna

For Folder Copy You can Use

对于文件夹复制,您可以使用

robocopy C:\Source D:\Destination /E

For File Copy

对于文件复制

copy D:\Sourcefile.txt D:\backup\Destinationfile.txt /Y 

Delete file in some folder last modify date more than some day

删除某个文件夹中的文件上次修改日期超过某一天

forfiles -p "D:\FolderPath" -s -m *.[Filetype eg-->.txt] -d -[Numberof dates] -c "cmd /c del @PATH"

And you can Shedule task in windows perform this task automatically in specific time.

并且您可以在特定时间自动执行 Windows 中的 Shedule 任务。

回答by a_programmer

I've been interested in the original question here and related ones.

我对这里的原始问题和相关问题很感兴趣。

For an answer, this week I did some experiments with XCOPY.

作为答案,本周我用 XCOPY 做了一些实验。

To help answer the original question, here I post the results of my experiments.

为了帮助回答最初的问题,我在这里发布了我的实验结果。

I did the experiments on Windows 7 64 bit Professional SP1 with the copy of XCOPY that came with the operating system.

我使用操作系统附带的 XCOPY 副本在 Windows 7 64 位 Professional SP1 上进行了实验。

For the experiments, I wrote some code in the scripting language Open Object Rexx and the editor macro language Kexx with the text editor KEdit.

对于实验,我使用脚本语言 Open Object Rexx 和编辑器宏语言 Kexx 和文本编辑器 KEdit 编写了一些代码。

XCOPY was called from the Rexx code. The Kexx code edited the screen output of XCOPY to focus on the crucial results.

XCOPY 是从 Rexx 代码调用的。Kexx 代码编辑了 XCOPY 的屏幕输出以关注关键结果。

The experiments all had to do with using XCOPY to copy one directory with several files and subdirectories.

所有的实验都与使用 XCOPY 复制一个包含多个文件和子目录的目录有关。

The experiments consisted of 10 cases. Each case adjusted the arguments to XCOPY and called XCOPY once. All 10 cases were attempting to do the same copying operation.

实验包括10个案例。每个案例都将参数调整为 XCOPY 并调用一次 XCOPY。所有 10 个案例都试图进行相同的复制操作。

Here are the main results:

以下是主要结果:

(1) Of the 10 cases, only three did copying. The other 7 cases right away, just from processing the arguments to XCOPY, gave error messages, e.g.,

(1) 10个案例中,只有3个是抄袭的。其他 7 个案例,仅仅通过处理 XCOPY 的参数,立即给出错误消息,例如,

Invalid path

Access denied

with no files copied.

没有复制文件。

Of the three cases that did copying, they all did the same copying, that is, gave the same results.

在进行复制的三个案例中,它们都进行了相同的复制,即给出了相同的结果。

(2) If want to copy a directory X and all the files and directories in directory X, in the hierarchical file system tree rooted at directory X, then apparently XCOPY -- and this appears to be much of the original question -- just will NOT do that.

(2) 如果想复制一个目录 X 和目录 X 中的所有文件和目录,在以目录 X 为根的分层文件系统树中,那么显然 XCOPY——这似乎是原始问题的大部分——只是会不要那样做。

One consequence is that if using XCOPY to copy directory X and its contents, then CAN copy the contents but CANNOT copy the directory X itself; thus, lose the time-date stamp on directory X, its archive bit, data on ownership, attributes, etc.

一个后果是,如果使用 XCOPY 复制目录 X 及其内容,则可以复制内容但不能复制目录 X 本身;因此,会丢失目录 X 上的时间戳、其存档位、所有权数据、属性等。

Of course if directory X is a subdirectory of directory Y, an XCOPY of Y will copy all of the contents of directory Y WITH directory X. So in this way can get a copy of directory X. However, the copy of directory X will have its time-date stamp of the time of the run of XCOPY and NOT the time-date stamp of the original directory X.

当然如果目录 X 是目录 Y 的子目录,则 Y 的 XCOPY 会将目录 Y 的所有内容与目录 X 一起复制。因此,通过这种方式可以获得目录 X 的副本。但是,目录 X 的副本将具有它的时间-日期戳是运行 XCOPY 的时间,而不是原始目录 X 的时间-日期戳。

This change in time-date stamps can be awkward for a copy of a directory with a lot of downloaded Web pages: The HTML file of the Web page will have its original time-date stamp, but the corresponding subdirectory for files used by the HTML file will have the time-date stamp of the run of XCOPY. So, when sorting the copy on time date stamps, all the subdirectories, the HTML files and the corresponding subdirectories, e.g.,

对于包含大量下载网页的目录副本而言,时间日期戳的这种更改可能会很尴尬:网页的 HTML 文件将具有其原始时间日期戳,但 HTML 使用的文件的相应子目录文件将具有运行 XCOPY 的时间日期戳。因此,当按时间日期戳对副本进行排序时,所有子目录、HTML 文件和相应的子目录,例如,

x.htm

x_files

can appear far apart in the sort on time-date.

可以在时间-日期的排序中显得相距很远。

Hierarchical file systems go way back, IIRC to Multics at MIT in 1969, and since then lots of people have recognized the two cases, given a directory X, (i) copy directory X and all its contents and (ii) copy all the contents of X but not directory X itself. Well, if only from the experiments, XCOPY does only (ii).

分层文件系统可以追溯到 1969 年 MIT 的 IIRC 到 Multics,从那时起,很多人都认识到了两种情况,给定目录 X,(i)复制目录 X 及其所有内容和(ii)复制所有内容X 但不是目录 X 本身。好吧,如果仅从实验来看,XCOPY 只做(ii)。

So, the results of the 10 cases are below. For each case, in the results the first three lines have the first three arguments to XCOPY. So, the first line has the tree name of the directory to be copied, the 'source'; the second line has the tree name of the directory to get the copies, the 'destination', and the third line has the options for XCOPY. The remaining 1-2 lines have the results of the run of XCOPY.

所以,10个案例的结果如下。对于每种情况,在结果中的前三行都有 XCOPY 的前三个参数。因此,第一行是要复制的目录的树名,即“源”;第二行是获取副本的目录树名、“目的地”,第三行是 XCOPY 选项。剩下的 1-2 行是运行 XCOPY 的结果。

One big point about the options is that options /X and /O result in result

关于选项的一大要点是选项 /X 和 /O 导致结果

Access denied

To see this, compare case 8 with the other cases that were the same, did not have /X and /O, but did copy.

为了看到这一点,将案例 8 与其他相同的案例进行比较,没有 /X 和 /O,但有复制。

These experiments have me better understand XCOPY and contribute an answer to the original question.

这些实验让我更好地理解 XCOPY 并为原始问题提供了答案。

======= case 1 ==================
"k:\software\dir_time-date\"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_1\"
options = /E /F /G /H /K /O /R /V /X /Y
Result:  Invalid path
Result:  0 File(s) copied
======= case 2 ==================
"k:\software\dir_time-date\*"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_2\"
options = /E /F /G /H /K /O /R /V /X /Y
Result:  Access denied
Result:  0 File(s) copied
======= case 3 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_3\"
options = /E /F /G /H /K /O /R /V /X /Y
Result:  Access denied
Result:  0 File(s) copied
======= case 4 ==================
"k:\software\dir_time-date\"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_4\"
options = /E /F /G /H /K /R /V /Y
Result:  Invalid path
Result:  0 File(s) copied
======= case 5 ==================
"k:\software\dir_time-date\"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_5\"
options = /E /F /G /H /K /O /R /S /X /Y
Result:  Invalid path
Result:  0 File(s) copied
======= case 6 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_6\"
options = /E /F /G /H /I /K /O /R /S /X /Y
Result:  Access denied
Result:  0 File(s) copied
======= case 7 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_7"
options = /E /F /G /H /I /K /R /S /Y
Result:  20 File(s) copied
======= case 8 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_8"
options = /E /F /G /H /I /K /O /R /S /X /Y
Result:  Access denied
Result:  0 File(s) copied
======= case 9 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_9"
options = /I /S
Result:  20 File(s) copied
======= case 10 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_10"
options = /E /I /S
Result:  20 File(s) copied