1

Expanding upon this example: Copying a file to multiple folders in the same directory I want to copy an XML file to several different project directories under the same root folder, so I have tried this:

for /d %a in (C:\Root\Output\*\bin\x64\Release) do copy /y C:\Root\OtherProject\MyXml.xml %a\

Where the wildcard would be a different project name in my solution directory.

When I run this command, there is no error, but the XML is not copied, so my question is can you use wildcards like this in Windows command line or alternatively is there a better way to tackle this kind of operation from within command prompt?

Community
  • 1
  • 1
JNH
  • 477
  • 1
  • 5
  • 15

1 Answers1

1

Just split it:

FOR /D %1 IN (c:\root\output\*) DO (
    COPY /Y c:\root\otherproject\myxml.xml %1\bin\x64\release
)

Obviously this works if all subdirectories of c:\root\output must be included (they have a bin\x64\release subdirectory.) If it's not true then you have to include an extra check:

FOR /D %1 IN (c:\root\output\*) DO (
    IF EXIST "%1\bin\x64\release" (
        COPY /Y c:\root\otherproject\myxml.xml "%1\bin\x64\release"
    )
)

Of course you may feel this is to much code for such simple task, well then maybe it's time to switch to PowerShell, Get-ChildItem supports wildcards used the way you want:

Get-ChildItem c:\root\output\*\bin\x64\release `
    | ForEach-Object -Process `
        { Copy-Item -Force c:\root\otherproject\myxml.xml $_ }

If you love short syntax:

gci c:\root\output\*\bin\x64\release | % { cp -Force c:\root\otherproject\myxml.xml $_ }
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208