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

时间:2015-08-12 21:17:50

标签: windows batch-file file-handling

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

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

1 个答案:

答案 0 :(得分:0)

我认为这个问题与设置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
  )
)