xcopy wildcard source folder name to destination

20,890

Solution 1

for /f "delims=" %%a in ('dir /b/ad "a:\parentfolder\n*" ') do xcopy "a:\parentfolder\%%a\*" x:\parentfolder\

As you have it, XCOPY assumes that n* is a filespec, and there's no way to tell it otherwise.

Solution 2

If you first CD to the folder you want to copy it will work:

a:
cd \parentfolder
xcopy /s n*.* x:\parentfolder
Share:
20,890
Wonderer
Author by

Wonderer

Updated on July 15, 2022

Comments

  • Wonderer
    Wonderer almost 2 years

    I want to copy from a wildcard source folder to a destination folder:

    xcopy a:\parentfolder\n* x:\parentfolder
    

    Only folders starting with "n" should therefore be copied to the destination.

    Any help to get this working would be much appreciated.

  • Wonderer
    Wonderer about 10 years
    Thanks so much Magoo, though it seems there might be a little bug somewhere. I have tried your code a few different ways, but get an error: "%%a was unexpected at this time"
  • Wonderer
    Wonderer about 10 years
    Ok, I got it working! :-) It seems the problem was with the double %%a .... ? Changing it to sing le %a did the trick. Thanks Magoo !! :-)
  • Wonderer
    Wonderer about 10 years
    The final tweak was the /s switch for xcopy. Hope this is helpful for others.
  • unclemeat
    unclemeat about 10 years
    @RenéSchutte The double % is necessary when running this command from a batch file. From the command line you only need a single %, as you discovered.
  • Magoo
    Magoo over 8 years
    @user5428856 - The spces between the switches are optional with dir although I normally use them. The `` between the sourcespec and destination was definitely an error - and one which has been here for more than 18 months! Good spotting!