2011-12-31 166 views
7

下面是一些代码,我做了:)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:\合同\充满这在他们的文件夹。

我想将所有复制到R:\ Contracts \ Sites \并压扁文件夹结构。

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

谢谢

+0

可能重复[如何从文件夹树中删除所有与Robocopy文件夹的文件?](http://stackoverflow.com/questions/1502170/how-to-copy-files-from-folder-tree-dropping -all-the-folders-with-robocopy) – 2011-12-31 19:59:53

回答

9

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

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

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

@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 
+0

你好dbenham,首先,非常感谢你。我尝试了上面的代码,但它似乎没有工作。我制作了一个批处理文件并对其进行了测试。我已经通过代码,并没有看到任何直接的错误。请告诉我。谢谢 – Arthor 2012-01-02 01:01:37

+0

你好,我删除了这一行“for %% F in(%destination%)do set destination =”%〜fF“”,它似乎工作。但是我不确定是否需要。如果谢谢谢谢DBENHAM – Arthor 2012-01-02 01:05:44

+0

糟糕 - 我在该行末尾附近丢失了一个%。现在已经修复了。目的是确保路径以完全合格的标准格式表示,以便随后的IF语句正常工作。 – dbenham 2012-01-02 03:22:52

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%" 
7

您可以使用PowerShell一个班轮执行此操作。 在这个例子中,我将从所有子文件夹中滤除所有扩展名为.txt的文件。然后将它们发送到Move-Item Cmdlet。

合并Cmdlet 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 
+0

辉煌。简单,易于理解。 – sonjz 2016-05-26 01:36:36

0

有一个称为Sweep.exe老PCMAG实用工具,可以在当前和子目录运行相同的命令。我不会把目标作为源目录的子目录。把目的地放在别处。

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

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

这将复制从C:\合同,下为C:\网站

我使用了类似的命令,以扁平化的目录层次结构。

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

相关问题