我想從已知數量的子文件夾中複製文件,這些文件有一部分共同名稱。我試着用這段代碼:批處理:將具有相同部分名稱的文件從子文件夾複製到另一個文件夾
@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
**%〜dp0 **已經有一個尾隨的反斜槓,刪除你之後添加的反斜槓! – Compo
我想'對於%% 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
您還必須發佈「POPD」。 – Squashman