【问题标题】:Existing Directory Not Found by Batch Script批处理脚本未找到现有目录
【发布时间】:2015-08-12 21:17:49
【问题描述】:

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

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。任何想法或想法将不胜感激。

【问题讨论】:

  • 检查echo DEBUG: Path %CD%\%RELEASE_LABEL% does not exist [!RELEASE_PATH!]。使用括号:if not exist "!RELEASE_PATH!" (

标签: windows batch-file file-handling


【解决方案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
  )
)

【讨论】:

    猜你喜欢
    • 2013-01-30
    • 2013-04-11
    • 1970-01-01
    • 2012-11-01
    • 2013-07-27
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多