You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Windows批处理脚本中实现插件名称的大小写敏感搜索?

问题:批处理脚本实现插件名称的大小写敏感搜索

我正在编写一款Windows批处理脚本,用于接收用户输入并在本地查找对应的插件。目前脚本运行正常,但发现插件搜索不区分大小写:例如存在名为com.test123的插件,当输入Com.test123时,脚本仍会判定插件存在并继续执行,但后续调用DITA OpenToolkit的dita命令时,会因大小写不匹配无法找到插件,导致构建失败。请问是否可以在批处理脚本中实现插件名称的大小写敏感搜索?

补充说明:输入的插件名称会用于dita命令(基于DITA OpenToolkit)构建各类输出格式(如本示例中的PDF)。尽管该问题可能源于dita进程,但希望能在批处理脚本中提前对用户进行提示。

原不区分大小写的代码

:start
echo.
echo ***********************************************************
echo.
set tmpDate=
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined tmpDate set tmpDate=%%x
set YYYYMMDD=%tmpDate:~0,4%%tmpDate:~4,2%%tmpDate:~6,2%

set DITA_DIR= %cd%\..\..\dita-ot-3.7\
set DitaSource= %cd%\..

:plugin
SET /p plugin=Enter a plugin: 
IF EXIST %DitaSource%\%plugin% (
    goto format
    ) else (
    echo Couldn't find the specified plugin.
    echo.
    goto plugin
    )

:format
SET /p format=Enter an output format (or 'r' to reset the plugin identifier): 
if %format%==pdf-qnx goto buildPDF
if %format%==r goto start
echo You must provide one of the following output formats: pdf-qnx, xhtml, eclipsehelp, webhelp-responsive
echo.

:tryagain
echo Enter the plugin identifier: %plugin%
SET /p format=Enter the output format (or 'r' to reset the plugin identifier): 
if %format%==pdf-qnx goto buildPDF
if %format%==r goto start
echo You must provide one of the following output formats: pdf-qnx, xhtml, eclipsehelp, webhelp-responsive
echo.
goto tryagain

:buildPDF
@copy %DitaSource%\buildfiles-3.7\vars.ditamap %DitaSource%\%plugin%>NUL
:: Delete the existing book.pdf file, if possible
if EXIST %DitaSource%\%plugin%\%format%\book.pdf del %DitaSource%\%plugin%\%format%\book.pdf
:: Couldn't delete the book.pdf file, likely because the file is open. Redirect to the BUILD FAILED message and try again.
if EXIST %DitaSource%\%plugin%\%format%\book.pdf goto fail
call %DITA_DIR%bin\dita -i %DitaSource%\%plugin%\book.ditamap -f %format% -o %DitaSource%\%plugin%\%format%\ --propertyfile=%DitaSource%\buildfiles-3.7\%format%.properties
del %DitaSource%\%plugin%\vars.ditamap
if EXIST %DitaSource%\%plugin%\%format%\book.pdf (
echo.
echo BUILD SUCCESSFUL
) else (
:fail
echo.
echo BUILD FAILED
)
goto start

解决方案:实现大小写敏感的插件搜索

Windows默认的IF EXIST命令不区分大小写,但我们可以通过枚举目标目录下的子目录,对目录名称做精确字符串匹配来实现逻辑上的大小写敏感检查。修改原脚本中:plugin段落的代码如下:

:plugin
SET /p plugin=Enter a plugin: 
SET "plugin_found="
:: 遍历DitaSource下的所有子目录,执行精确大小写匹配
for /d %%d in ("%DitaSource%\*") do (
    :: 先做不区分大小写的初步匹配,缩小范围
    if /i "%%~nxd"=="%plugin%" (
        :: 再做精确大小写匹配
        if "%%~nxd"=="%plugin%" (
            SET "plugin_found=1"
            goto :format
        )
    )
)
:: 未找到完全匹配的插件时提示错误
if not defined plugin_found (
    echo Couldn't find the specified plugin (case-sensitive match failed).
    echo.
    goto plugin
)

原理说明

  1. 使用for /d %%d in ("%DitaSource%\*")遍历DitaSource下的所有子目录,%%~nxd会提取出目录的完整名称(不含路径)。
  2. 先通过if /i做不区分大小写的匹配,快速筛选出可能的候选目录;再通过不带/i的if做精确的大小写对比,确保输入的插件名和实际目录名完全一致。
  3. 如果找到完全匹配的目录,标记plugin_found并跳转至后续流程;否则提示错误,让用户重新输入。

这样就能在脚本阶段提前拦截大小写不匹配的输入,避免后续调用dita命令时出现构建失败的问题。


内容的提问来源于stack exchange,提问作者Amy Higgins

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 20:15:40