Matrefeytontias Matrefeytontias - 1 year ago 74
Linux Question

Work around Linux wildcards with Batch

I am basically trying to implement this line in Windows Batch :

cp */win/lib/*.dll ./bin
. Linux is kind enough to let me have these fancy wildcards working, but Windows won't take any of it. What's the best way to make that work ? A
loop seems a bit overkill, but neither of
copy, xcopy, robocopy
seems to implement that kind of fancy work.

Thanks by advance !

EDIT : for context, I'm writing a Makefile for mingw32-make, and I want to copy a bunch of dependencies to the executable's folder.

Answer Source

A FOR loop may feel like overkill, but we're talking about batch here, so why not simply be happy that there is a way to do what you want at all... :) I know of no other possibility to do this (apart from using external tools). Note that backslashes in the directory mask need to be doubled for regex use in FINDSTR, which is done in line 4. The caret ^ for the negating character class needs to be escaped twice since it is interpreted twice.

Edit: Correction to not include .dlls in subdirectories of *\win\lib.

SET dirmask=\win\lib
SET extension=.dll
SET regex=%dirmask:\=\\%\\[^^^^\\]*\%extension%$
FOR /F "tokens=*" %%G IN ('DIR /S /B ^| FINDSTR /R "%regex%"') DO COPY %%G .\bin
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download