Matrefeytontias Matrefeytontias - 1 month ago 9
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
for
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

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.

@ECHO OFF
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