我尝试使用在 IIS 7 中的“默认网站”下创建一个虚拟目录AppCmd
。
但首先我想看看是否已经存在。如何使用AppCmd
在“默认网站”下创建虚拟目录以及如何执行 if 语句?
答案1
尝试这个:
@ECHO OFF
REM --------------------------------------------------------------------------------
REM Check for and create VDir under Default Web Site
REM
REM %1 is the VDIR to create
REM %2 is the Physical path to the VDIR
REM --------------------------------------------------------------------------------
IF "%1"=="" GOTO Syntax
IF "%2"=="" GOTO Syntax
ECHO Running...
ECHO AppCmd.exe list vdir "Default Web Site/%1/"
ECHO.
AppCmd.exe list vdir "Default Web Site/%1/"
IF %errorlevel%==1 GOTO Exists
ECHO.
ECHO Running...
ECHO AppCmd.exe ADD vdir /app.name:"Default Web Site/" /path:/%1 /physicalPath:%2
ECHO.
AppCmd.exe ADD vdir /app.name:"Default Web Site/" /path:/%1 /physicalPath:%2
GOTO End
:Exists
ECHO.
ECHO VDir already exists
ECHO.
GOTO End
:SYNTAX
ECHO.
ECHO VDir Name and Physical Path Required
ECHO.
ECHO CreateVDir.CMD ^<VDirName^> C:\PhysPath
ECHO.
:END
答案2
试试这个。与 Christopher_G_Lewis 给出的答案大致相同,但依赖于列表输出的解析而不是错误代码,我也不明白。
还利用 cmd.exe shell 构造 A || B(如果 A 失败则执行 B)
@ECHO OFF
REM --------------------------------------------------------------------------------
REM Check for and create VDir under Default Web Site
REM
REM %1 is the VDIR to create
REM %2 is the Physical path to the VDIR
REM --------------------------------------------------------------------------------
IF "%1"=="" GOTO Syntax
IF "%2"=="" GOTO Syntax
AppCmd.exe list vdir "Default Web Site/%1/" | findstr /I "Default Web Site/%1/" || AppCmd.exe add vdir /app.name:"Default Web Site/" /path:/%1 /physicalPath:%2
goto :eof