10

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.

unclemeat
  • 5,029
  • 5
  • 28
  • 52
Wonderer
  • 113
  • 1
  • 1
  • 6
  • 1
    So what's wrong with what you've got? If there's an error, post it. Otherwise, state what's working differently than you expect. – Blorgbeard Apr 22 '14 at 02:47
  • Is there an error? What doesn't work? – Luigi Apr 22 '14 at 03:00
  • I have deleted a few files in the destination folder in order to to test whether the above command will copy the missing files across. Even though I get no error from the above xcopy, the missing files are not copied either. I have also tried the xcopy command as follows: xcopy "E:\parentfolder\N*.*" "N:\parentfolder\" /D /F /I /Y /R – Wonderer Apr 22 '14 at 03:00

2 Answers2

9
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.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • 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 Apr 22 '14 at 03:15
  • 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 Apr 22 '14 at 03:19
  • The final tweak was the /s switch for xcopy. Hope this is helpful for others. – Wonderer Apr 22 '14 at 03:27
  • 1
    @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. – unclemeat Apr 22 '14 at 03:37
  • @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! – Magoo Nov 29 '15 at 01:34
3

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

a:
cd \parentfolder
xcopy /s n*.* x:\parentfolder