Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I try to create a virtual directory under the "Default Web Site" in IIS 7 using AppCmd.

But first I'd like to see if one already exists. How can I use AppCmd to create a virtual directory under the "Default Web Site" and how can I do an if-statement?

share|improve this question
up vote 8 down vote accepted

Try this:

@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
share|improve this answer
    
Cool! This is just what I need to get going! Thanks! Looks like ServerFault might be just as good as SO! – Riri Jul 29 '09 at 7:00
    
This does not seem to trigger an exit code of 1 for a nonexistent virtual directory for me. Using IIS 7.5. – jpmc26 Jul 23 '14 at 22:28

Try this. Mostly the same as the answer given by Christopher_G_Lewis, but relies on a parse of the list output instead of errorcode, which I don't get either.

Also makes use of the cmd.exe shell construct A || B (if A fails then do 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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.