Search code examples
batch-filefor-loopsubdirectoryxcopy

Batch: Copy files, with same partial name, from subfolders to another one


I want to copy files, who has a part of his name in common, from a known number of subfolders. I tried with this code:

@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"
    )
)

The code use two txt files that contains different data. In subFoldersList.txt:

subFolder1
subFolder2
subFolder3

In formatList.txt:

xls
xlsx
xlsm

Checking the code the loop doesn't run never and I unknown why.

Edit:

The folders skeleton that I use is this:

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

I want to obtain this result:

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

Solution

  • Based upon what you've provided thus far, maybe this is what you intended:

    @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))