Search code examples
batch-filecmdfile-copying

copy file to multiple subdirectories in same directory


Expanding upon this example: Copying a file to multiple folders in the same directory

I want to copy all file .txt from C:\Newfolder\ to folder C:\Output\*\rack\*\cloth

example for destination folder:

C:\Output\name1\rack\nick1\cloth
C:\Output\name2\rack\nick2\cloth
.
.
C:\Output\name100\rack\nick100\cloth

so I have tried this:

FOR /D %%1 IN (C:\Output\*) DO (
    IF EXIST "%%1\rack" (
        COPY /Y C:\Newfolder\*.txt "%%1\rack\*\cloth"
    )
)

the problem all txt file doesn't copied to destination folder


Solution

  • If you use the same FOR /D method for each unknown subfolder name then you should get to where you want to be:

    @ECHO OFF
    FOR /D %%A IN ("C:\Output\*") DO IF EXIST "%%A\rack\" FOR /D %%B IN ("%%A\rack\*"
    ) DO IF EXIST "%%B\cloth\" COPY /Y "C:\Newfolder\*.txt" "%%B\cloth"