如何将Makefile中的Bash DLL拷贝命令转换为Windows批处理命令?
把Bash的find复制命令转成Windows批处理脚本
原Bash命令的作用是递归查找source目录下,所有路径包含.bin/output/子目录的.dll文件,然后把这些文件复制到当前目录的dest文件夹里:
find "source" -type f -wholename '*/.bin/output/*.dll' -exec cp {} "./dest" \;
下面是两种Windows批处理的实现方式:
方式一:用路径过滤实现精准匹配
适合需要严格检查路径中是否包含.bin/output/的场景:
@echo off set "source_dir=source" set "dest_dir=./dest" :: 先确保目标目录存在,不存在就创建 if not exist "%dest_dir%" mkdir "%dest_dir%" :: 递归遍历source下所有dll文件,过滤出路径包含.bin/output/的文件 for /r "%source_dir%" %%f in (*.dll) do ( echo "%%~pf" | findstr /i "\.bin\output\" >nul if not errorlevel 1 ( copy "%%f" "%dest_dir%" ) )
for /r会递归遍历source_dir下的所有.dll文件%%~pf提取文件的路径部分,用findstr检查是否包含.bin/output/- 匹配成功就执行
copy命令
方式二:直接指定目标子目录遍历
如果确定要找的就是所有source子目录下.bin/output里的dll,这种方式更简洁:
@echo off set "source_dir=source" set "dest_dir=./dest" if not exist "%dest_dir%" mkdir "%dest_dir%" :: 直接递归查找所有source下的.bin/output/*.dll文件并复制 for /r "%source_dir%" %%f in (.bin\output\*.dll) do ( copy "%%f" "%dest_dir%" )
for /r "%source_dir%" %%f in (.bin\output\*.dll)会自动递归查找source目录下所有嵌套的.bin/output子目录里的.dll文件- 找到后直接复制到目标目录
注意事项
- 如果
source_dir或dest_dir包含空格,要确保变量值用引号括起来 - 去掉
@echo off可以看到脚本执行的每一步命令,方便调试
内容的提问来源于stack exchange,提问作者Avraham Cohen
相关产品推荐
相关产品推荐

