Brucevilla
Brucevilla

Reputation: 156

how to traverse specified subfolders in a windows batch file?


Here is my folder hierarchy:

[Numbers]
[Numbers/12545]
[Numbers/12545/dev]
[Numbers/12545/prod]

[Numbers/32445]
[Numbers/32445/dev]
[Numbers/32445/prod]

...

[Numbers/.....]
[Numbers/...../dev]
[Numbers/...../prod]

I want to copy some text files under the only "[Numbers/...../dev]" folders. How should i do? I tried the below code and it's not work because it coppies under the all subfolders.

for /r %NUMBER_DIRS% %%d in (.) do (
    copy %PROJECT_INPUTS%\*.txt "%%d"
)

Thanks.

Upvotes: 1

Views: 1224

Answers (2)

Endoro
Endoro

Reputation: 37569

Try this:

for /d /r "%NUMBER_DIRS%" %%d in (*DEV) do copy "%PROJECT_INPUTS%\*.txt" "%%~d\*.txt"

Upvotes: 1

Magoo
Magoo

Reputation: 79982

@ECHO OFF
SETLOCAL
FOR /f "delims=" %%i IN (
  'dir /s /b /a:d "\numbers" ^| findstr /i /e "\dev"'
  ) do ECHO COPY %PROJECT_INPUTS%\*.txt "%%i\"

This will report what the batch PROPOSES to do. Remove the ECHO keyword before the COPY to execute the copy.

Note : you may need to add /y to the copy options if you want to OVERWRITE an existing file in the destination directories.

I presume that you're copying FROM %PROJECT_INPUTS% TO many ...\dev directories.

Upvotes: 1

Related Questions