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
Author by
Wonderer
Updated on July 15, 2022Comments
-
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 about 10 yearsThanks 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 about 10 yearsOk, 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 about 10 yearsThe final tweak was the /s switch for xcopy. Hope this is helpful for others.
-
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 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!