Tomas
Tomas

Reputation: 75

Batch to copy a file to a folder and only ONE subfolder deep

I have this batch file now that copies example.jpg from C:\Temp to the Test-folder and all its subfolders.

I would like it to copy the file only 1 subfolder deep in the Test-folder. For example copy the picture to Test\subfolder but NOT to Test\subfolder\subfolder2

@echo off
for /r "C:\Temp\Test" %%f in (.) do (
    copy "C:\Temp\example.jpg" "%%~ff" > nul
)

PAUSE

Upvotes: 1

Views: 2345

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

Don't use recursion if you want to go only 1 level deep. Try this instead:

@echo off

set src=C:\Temp\example.jpg
set dst=C:\Temp\Test

copy "%src%" "%dst%" >nul

for /d %%d in ("%dst%\*") do (
  copy "%src%" "%%~fd" >nul
)

Upvotes: 1

Magoo
Magoo

Reputation: 80023

@ECHO OFF
SETLOCAL
SET destroot=c:\temp
FOR /f "delims=" %%i IN ( ' dir /ad/b "%destroot%"' ) DO ECHO COPY "c:\temp\example.jpg" "%destroot%\%%i\"

Simply shows what the batch PROPOSES to do. Remove the ECHO keyword to activate the copy and add >nul to suppress the "copied" message

Upvotes: 0

Related Questions