2011-12-31 165 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:\網站

我使用了類似的命令,以扁平化的目錄層次結構。

注意重複項以及如何處理它們。你想覆蓋或處理不同的方式。

相關問題