1

所以我想检查一个环境变量是否存在于一个windows批处理文件中,如果它确实存在,它是否指向一个有效的路径。

我将两个变量传递给一个函数,第一个是表示环境变量的字符串,第二个是设置它的默认路径。我想检查以确保它尚未设置。如果它已经设置,我只想检查路径是否良好,如果不是则报告。这就是我所拥有的:

CALL:SET_PATH MY_ENV "C:\My\Path"

:SET_PATH
IF NOT DEFINED %1 (
  IF EXIST %2 (
    ECHO ______________________________________________
    ECHO %1=%2
    SETX %1 %2
  ) ELSE (
    ECHO %1 environment variable was not set, the path did not exist:
    ECHO %2
  )
) ELSE (
  IF NOT EXIST %1 ( <-- How do I evaluate this as an environment variable instead of a string so I can get the path?
    ECHO %1 environment variable points to a path that does not exist:
    ECHO %1 <-- Same thing here
  )
)
GOTO:EOF

提前感谢您的帮助。

4

1 回答 1

1
@ECHO OFF &SETLOCAL 
CALL:SET_PATH MY_ENV "C:\My\Path"
GOTO:EOF

:SET_PATH
IF NOT DEFINED %1 (
  IF EXIST %2 (
    ECHO ______________________________________________
    ECHO %1=%2
    SETX %1 %2
  ) ELSE (
    ECHO %1 environment variable was not set, the path did not exist:
    ECHO %2
  )
) ELSE (
  setlocal enabledelayedexpansion
  IF NOT EXIST "!%~1!" (
    ECHO %1 environment variable points to a path that does not exist:
    ECHO !%1!
  )
  endlocal
)
GOTO:EOF
于 2013-10-05T00:52:39.643 回答