批处理脚本找不到现有目录

Existing Directory Not Found by Batch Script

经过对用户的一系列提示,我的批处理脚本组装了一个目录路径:

set RELEASE_PATH=!RELEASE_DRIVE!:\!CUSTOMER!\files\!RELEASE_LABEL!

我遇到的问题是在尝试检测此路径末尾的文件夹是否已存在时。您可能认为这相当简单,但路径评估总是失败,即使路径存在。但是,当我从命令行 运行 相同的 if 语句时,它起作用了。 -_-

if not exist !RELEASE_PATH! (
  echo DEBUG: Path %CD%\%RELEASE_LABEL% exists
  mkdir %RELEASE_LABEL%
) else (
  echo DEBUG: Path %CD%\%RELEASE_LABEL% does not exist
)

运行 批处理文件总是回显 DEBUG: Path %CD%\%RELEASE_LABEL% does not exist 的行,即使它实际上确实存在。仅供参考,我同时设置了 EnableExtentionsEnableDelayedExpansion

典型的发布路径可能是 R:\Widget_Co\files\Release_12.1。任何想法或想法将不胜感激。

我认为这个问题与 EnableDelayedExpansion 的设置有关。我能够使用 for 循环将此解决方法放在一起:

rem ## This clunky workaround with the for loop is required because a simple
rem ## `if not exist` command isn't working. It is believed that this has
rem ## something to do with EnableDelayedExpansion being set.  There is only one
rem ## element in the RELEASE_PATH variable being evaluated in this loop.

for /F %%i in ("!RELEASE_PATH!") do (
  if not exist %%i (
    mkdir %%i
  )
)