ROBOCOPY - 将文件夹内容复制到单个文件夹 [英] ROBOCOPY - Copy folders content to a single folder

查看:60
本文介绍了ROBOCOPY - 将文件夹内容复制到单个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我编写的一些代码 :)

Here is some code I made :)

@echo off
set source="R:Contracts"
set destination="R:ContractsSites"
ROBOCOPY %source% %destination% *.srt *.pdf *.mp4 *.jpg /COPYALL /R:0 /S
for /r %source in (*) do @copy "%destination" .

R:Contracts 充满了其中包含文件的文件夹.

R:Contracts is full of folders which have files in them.

我想将所有内容复制到 R:ContractsSites 并展平文件夹结构.

I want to copy all to R:ContractsSites and flatten the folder structure.

一切都很好复制,还有文件夹结构.

Everything copies well but also the folder structure.

谢谢

推荐答案

没有一个命令会为您扁平化层次结构;您将不得不使用多个命令.只需使用 FOR/R 遍历层次结构,再加上您选择的复制/移动命令(移动、复制、xcopy、robocopy),即可完成.因为您的目标位于源层次结构中,所以您需要一个 IF 来防止目标成为源.

No single command will flatten the hierarchy for you; you will have to use multiple commands. It can be done simply by using FOR /R to walk the hierarchy, coupled with your copy/move command of choice (move, copy, xcopy, robocopy). Because your destination is within the source hierarchy, you need an IF to prevent the destination from being a source.

在继续之前,您应该停下来想想如果相同的文件名出现在多个源文件夹中会发生什么.您的目标文件夹中只能有一个版本.你能保证没有重复的名字吗?如果不是,应该保留哪个文件?您如何构造命令以保留所需的文件?这种复杂性可能是为什么没有编写命令来简单地扁平化层次结构的原因.

Before proceeding you should stop and think about what happens if the same file name appears in multiple source folders. You can only have one version in your destination folder. Can you guarantee no duplicate names exist? If not, which file should be kept? How can you structure the command to keep the file you want? This complication is probably why no command was ever written to simply flatten a hierarchy.

这是与 FOR/R 解决方案集成的 ROBOCOPY 命令.

Here is your ROBOCOPY command integrated with the FOR /R solution.

@echo off
set source="R:Contracts"
set destination="R:ContractsSites"

::Not sure if this is needed
::It guarantees you have a canonical path (standard form)
for %%F in (%destination%) do set destination="%%~fF"

for /r %source% %%F in (.) do if "%%~fF" neq %destination% ROBOCOPY "%%F" %destination% *.srt *.pdf *.mp4 *.jpg /COPYALL /R:0

这篇关于ROBOCOPY - 将文件夹内容复制到单个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆