PunyTed
PunyTed

Reputation: 57

Batch to move file into sub directories excluding parent directory

I have a .otf file that I would like to copy into all the sub directories of the parent folder. This is what I have attempted.

for /R %%x in (.) do copy "file.otf" "%%x"

This works for the most part but it also leaves a copy into the parent folder. I would like to fix this such the the batch only copies into all the sub directories.

Upvotes: 1

Views: 168

Answers (1)

aschipfl
aschipfl

Reputation: 34989

Since for /R %%x in (.) is going to return the parent directory in the first iteration, you could use a flag-like variable to skip the copy command by an if condition in the first loop iteration, like this:

set "FLAG="
for /R %%x in (.) do (
    if defined FLAG (
        copy "file.otf" "%%~x"
    ) else (
        set "FLAG=#"
    )
)

According to Squashman's comment, an even easier option would be to use for /D /R to only enumerate sub-directories:

for /D /R %%x in (*) do (
    copy "file.otf" "%%~x"
)

Upvotes: 2

Related Questions