ROBOCOPY - 将文件夹内容复制到单个文件夹

时间:2011-12-31 19:54:59

标签: file batch-file copy robocopy

以下是我制作的一些代码:)

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

R:\ Contracts \中充满了包含文件的文件夹。

我想将所有内容复制到R:\ Contracts \ Sites \并展平文件夹结构。

一切都很好,但文件夹结构也很好。

谢谢

5 个答案:

答案 0 :(得分:10)

没有一个命令可以为您压缩层次结构;你将不得不使用多个命令。它可以简单地通过使用FOR / R来遍历层次结构,再加上您选择的复制/移动命令(移动,复制,xcopy,robocopy)。由于目标位于源层次结构中,因此需要IF以防止目标成为源。

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

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

@echo off
set source="R:\Contracts"
set destination="R:\Contracts\Sites"

::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

答案 1 :(得分:9)

你可以使用PowerShell单线程做到这一点。 在这个例子中,我从所有子文件夹中过滤掉所有带有.txt扩展名的文件。然后将它们发送到Move-Item Cmdlet。

结合Cmdlets get-Childitem(简称GCI),-recurse,maby -filter,然后将结果传递给Move-Item Cmdlet。使用-WhatIf首先检查输出是否符合预期。

移至当前文件夹

get-Childitem -recurse -filter *.txt | Move-Item -WhatIf

移至另一个文件夹

get-Childitem -recurse -filter *.txt | Move-Item -Destination c:\temp -WhatIf

答案 2 :(得分:1)

类似于上一个Powershell选项,我执行了以下操作以展平多子目录音乐文件夹:

#Get all files and not the directories
$files = Get-ChildItem -Path R:\Contracts -Recurse | Where {$_.PSIsContainer -eq $false}

#Copy items from sources to new destination
foreach ($file in $files){
    Copy-Item -Path $file.FullName -Destination R:\Contracts\Sites\$($file.Name)
}

带有-递归开关的 Get-ChildItem 将获得所有子文件夹和文件的列表。 位置功能通过检查布尔值 PSIsContainer 属性来剥离所有目录。如果不删除目录,则将创建不包含文件的子文件夹结构。此列表存储在 $ files 变量中。

foreach 函数遍历 $ files 变量中的文件列表,并一次将一项存储在 $ file 变量中。然后, Copy-Item 函数使用 $ file.FullName 中的完整路径,然后将文件从 $ file.Name <中复制到目的地。 / strong>。

答案 3 :(得分:0)

我最近不得不解决这个问题,我想从层次结构转移到单个文件夹的许多文件彼此具有相同的名称,我希望仍然可以将层次结构扁平化而不会覆盖它们。 我所做的是编写一个移动文件的脚本,但是使用名称中的旧层次结构路径重命名该文件 例如: 源文件:

C:\文件\ somefiles \ file.txt的

C:\文件\ otherfiles \ file.txt的

目的地是C:\ newdir \ 文件创建为

C:\ NEWDIR \ somefiles-file.txt的

C:\ NEWDIR \ otherfiles-file.txt的

这里是代码,批处理文件1通过文件,批处理文件2重命名并移动它们(如果你想保留源代码,也可以复制代码:

@echo off
for /r %%f in (*.*pr) do @renameandmovefilespart2.bat "%%f" "%%~ff" "%%~xf"

renameandmovefilespart2.bat

@echo off
Setlocal EnableDelayedExpansion
rem set the whole file path
set origWhole=%1
set origPathOnly=%2
set extension=%3
rem here you can set where the directory to hold the flattened hierarchy is
set destDir=c:\destinationDir\
rem  set the directory to do a string replace
rem make this the starting directory, that you dont want in the newly renamed files
set startingDir=C:\starting\directory\
set nothing=
set slash=\
rem here you can set what the character to represent the directory indicator \ in the new files 
set reaplcementDirectoryCharacter=--
set quote="
rem cut out the starting part of the directory
call set newname=%%origWhole:!startingDir!=!nothing!%%
rem replace slashes with new character
call set newname=%%newname:!slash!=!reaplcementDirectoryCharacter!%%
rem remove quotes
call set newname=%%newname:!quote!=!nothing!%%
rem @echo shortened: %newname%
rem @echo source path: %origPathOnly% newPath: %startingDir%
rem @echo extension: %extension%
rem rename the files
ren %origWhole% %newname%
rem prepare to move the file, clean up the source path
call set origPathOnly=%%origPathOnly:!quote!=!nothing!%%
move "%origPathOnly%%newname%" "%destDir%"

答案 4 :(得分:0)

有一个名为Sweep.exe的旧PCMag实用程序可以在当前和子目录中操作相同的命令。我不会将目标作为源目录的子目录。将目的地放在别处。

http://www.rarewares.org/files/case/Sweep.zip

cd c:\contracts sweep copy *.* c:\sites

这会将c:\ contracts及其下的所有内容复制到c:\ sites

我使用类似的命令来展平目录的层次结构。

注意重复以及如何处理它们。你想覆盖或处理不同的方式。