Reputation: 45
Looking for a batch file that would copy a file into multiple folders (within the same directory that the batch file was placed), but not their subfolders.
For example:
I need K:\NewCustomers\NewPartNumber.Bat
to go into K:\NewCustomers\Customer Name\
but not any subfolder of \Customer Name\
, there being 200-300 "Customer Name" folders.
I was using:
for /R "K:\NewCustomers\" %%a in (.) do copy "K:\NewCustomers\NewPartNumber.bat" "%%a"
But this is recursive, and now that there are folders inside of these other folders, I can't run this command without putting it in every subfolder.
I tried running a for /d
loop:
for /d "K:\NewCustomers\" %%a in (.) do copy "K:\NewCustomers\NewPartNumber.bat" "%%a"
but was unsuccessful at the syntax and after a while now of looking some things up and trying different things, I'm trying to pull my hair out looking for an answer. I get this error:
K:\NewCustomers* was unexpected at this time.
Upvotes: 3
Views: 8607
Reputation: 32920
Using for /d
to loop through folders in a non-recursive fashion indeed the right way to go, but you need to use it like this:
for /d %%a in ("K:\NewCustomers\*") do copy "K:\NewCustomers\NewPartNumber.bat" "%%a"
Alternatively, you can use a for /f
loop in combination with dir
:
@echo off
pushd "K:\NewCustomers"
for /f "tokens=*" %%a in ('dir /A:D /B') do copy "NewPartNumber.bat" "%%a"
popd
Personally, I prefer the first method more, though.
Upvotes: 5