xcopy wildcard source folder name to destination
Asked Answered
V

2

11

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.

Violinist answered 22/4, 2014 at 2:44 Comment(3)
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.Teacup
Is there an error? What doesn't work?Ettore
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 /RViolinist
S
10
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.

Smug answered 22/4, 2014 at 3:1 Comment(5)
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"Violinist
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 !! :-)Violinist
The final tweak was the /s switch for xcopy. Hope this is helpful for others.Violinist
@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.Marque
@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!Smug
D
3

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

a:
cd \parentfolder
xcopy /s n*.* x:\parentfolder
Dairy answered 14/12, 2014 at 3:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.