2016-11-08 36 views
1

我想從已知數量的子文件夾中複製文件,這些文件有一部分共同名稱。我試着用這段代碼:批處理:將具有相同部分名稱的文件從子文件夾複製到另一個文件夾

@Echo Off 
rem Check if the aFolder exists and delete it if it exists 

IF EXIST "%~dp0\aFolder" (
    rd /s /q "%~dp0\aFolder" 
) 

rem Create a new aFolder 
mkdir "%~dp0\aFolder" 

rem Copy the files from the subfolders inside of bFolder to aFolder  
For /F "tokens=1" %%A in (%~dp0\subFoldersList.txt) do (
    For /F "tokens=1" %%B in (%~dp0\formatList.txt) do (
     pushd "%~dp0\bFolder\%%A" 
     For %%C in (%%AcommonPart.%%B) do xcopy %%C "%~dp0\aFolder" 
    ) 
) 

該代碼使用兩個包含不同數據的txt文件。 在subFoldersList.txt:

subFolder1 
subFolder2 
subFolder3 

在formatList.txt:

xls 
xlsx 
xlsm 

檢查循環不會永遠運行的代碼,我不明所以。

編輯:

,我使用的文件夾骨架是這樣的:

rootFolder 
|->aFolder 
||-->subFolder1 
|||--->subFolder1commomPart.xlsx (for example) 
|||--->subFolder1other1Part.xlsm (for example) 
|||--->subFolder1other2Part.xls (for example) 
||-->subFolder2 
|||--->subFolder1commomPart.xlsm (for example) 
|||--->subFolder1other1Part.xlsx (for example) 
|||--->subFolder1other2Part.xls (for example) 
||-->subFolder3 
|||--->subFolder1commomPart.xls (for example) 
|||--->subFolder1other1Part.xlsm (for example) 
|||--->subFolder1other2Part.xlsx (for example) 
|->bFolder 
|->subFoldersList.txt 
|->formatList.txt 
|->Other Stuff 

我想獲得這樣的結果:

rootFolder 
|->aFolder 
||-->subFolder1 
|||--->subFolder1commomPart.xlsx (for example) 
|||--->subFolder1other1Part.xlsm (for example) 
|||--->subFolder1other2Part.xls (for example) 
||-->subFolder2 
|||--->subFolder1commomPart.xlsm (for example) 
|||--->subFolder1other1Part.xlsx (for example) 
|||--->subFolder1other2Part.xls (for example) 
||-->subFolder3 
|||--->subFolder1commomPart.xls (for example) 
|||--->subFolder1other1Part.xlsm (for example) 
|||--->subFolder1other2Part.xlsx (for example) 
|->bFolder 
||--->subFolder1commomPart.xlsx (for example) 
||--->subFolder1commomPart.xlsm (for example) 
||--->subFolder1commomPart.xls (for example) 
|->subFoldersList.txt 
|->formatList.txt 
|->Other Stuff 
+0

**%〜dp0 **已經有一個尾隨的反斜槓,刪除你之後添加的反斜槓! – Compo

+0

我想'對於%% C in(%% AcommonPart。%% B)do xcopy %% C「%〜dp0 \ aFolder」'應該改爲'For %% C in(「commonPart。%% B」)做xcopy「%%〜C」「%〜dp0 \ aFolder \」',因爲'pushd'已經變成了子文件夾'%% A';還要注意改進的引用,'%%〜C'中的'~'和目標處的尾部反斜槓。您還應該將'tokens = 1'更改爲'delims ='。請給我們展示一些文件名稱的示例... – aschipfl

+1

您還必須發佈「POPD」。 – Squashman

回答

0

基於你這樣提供了什麼遠,也許這就是你想要的:

@ECHO OFF 

REM Make sure script directory is current 
IF /I NOT "%CD%\"=="%~dp0" PUSHD "%~dp0" 2>NUL||EXIT/B 

REM Make sure source files exist 
FOR %%A IN ("subFoldersList.txt" "formatList.txt") DO IF NOT EXIST %%A EXIT/B 

REM Delete aFolder if it exists 
IF EXIST "aFolder\" RD/S/Q "aFolder" 

REM Create aFolder 
MD "aFolder" 

REM Copy the files from the subfolders inside of bFolder to aFolder  
FOR /F "USEBACKQ DELIMS=" %%A IN ("subFoldersList.txt") DO (
    IF EXIST "bFolder\%%A\" (PUSHD "bFolder\%%A" 
     FOR /F "USEBACKQ DELIMS=" %%B IN ("%~dp0formatList.txt") DO (
      IF EXIST "commonPart.%%B" (
       IF NOT EXIST "%~dp0aFolder\commonPart.%%B" (
        COPY "commonPart.%%B" "%~dp0aFolder"))) 
     POPD)) 
+0

代碼無法使用。我試圖在輸入一個命令後暫停一下,但沒有運行第一個for循環。 – Tilan04

+0

爲了調試或提供關於某人代碼的反饋,有一些簡單的事情要做,「代碼無法工作」對於我來說並不是足夠的信息。如果它沒有到達第一個循環,那麼至少有一個txt文件不存在於腳本的旁邊。 – Compo

+0

是的......就是這樣。我重命名了文件名,並且丟失了一個字母。哈哈感謝您的幫助。 – Tilan04

相關問題