Virtual directories in IIS 7 allow you to map physical directories to different paths in your website structure. The AppCmd.exe
utility provides command-line access to manage IIS configurations efficiently.
Before creating a new virtual directory, it's good practice to check if one already exists. Here's the command to list virtual directories under "Default Web Site":
appcmd list vdir /app.name:"Default Web Site/"
To check for a specific virtual directory (e.g., "MyVdir"):
appcmd list vdir /app.name:"Default Web Site/" /path:/MyVdir
The basic syntax to create a virtual directory is:
appcmd add vdir /app.name:"Default Web Site/" /path:/MyVdir /physicalPath:"C:\MyPhysicalPath"
To create a virtual directory only if it doesn't exist, you can use this batch script:
@echo off
set SITE_NAME="Default Web Site"
set VDIR_PATH="/MyVdir"
set PHYS_PATH="C:\MyPhysicalPath"
appcmd list vdir /app.name:%SITE_NAME% /path:%VDIR_PATH% >nul 2>&1
if %ERRORLEVEL% equ 0 (
echo Virtual directory already exists
) else (
appcmd add vdir /app.name:%SITE_NAME% /path:%VDIR_PATH% /physicalPath:%PHYS_PATH%
echo Virtual directory created successfully
)
You can specify additional parameters when creating virtual directories:
appcmd add vdir /app.name:"Default Web Site/" /path:/MyVdir
/physicalPath:"C:\MyPhysicalPath"
/logonMethod:ClearText
/userName:MyUser
/password:MyPassword
If you encounter "ERROR ( message:Configuration error )", ensure:
- The physical path exists
- You have administrative privileges
- The application pool identity has proper permissions
When managing IIS 7, you often need to handle virtual directories under the Default Web Site. The AppCmd.exe
utility provides a powerful command-line interface for these operations.
Before creating a new virtual directory, it's good practice to check if one already exists. Here's how to list all virtual directories under the Default Web Site:
appcmd list vdir /app.name:"Default Web Site/"
To create a virtual directory named "MyVDir" that points to "C:\MyContent":
appcmd add vdir /app.name:"Default Web Site/" /path:/MyVDir /physicalPath:"C:\MyContent"
Here's a complete batch script example that checks for existence before creating:
@echo off
set vdirName=MyVDir
set physicalPath=C:\MyContent
appcmd list vdir /app.name:"Default Web Site/%vdirName%" | find "%vdirName%" >nul
if %errorlevel% equ 0 (
echo Virtual directory %vdirName% already exists
) else (
appcmd add vdir /app.name:"Default Web Site/" /path:/%vdirName% /physicalPath:"%physicalPath%"
echo Virtual directory %vdirName% created successfully
)
You can combine this with other AppCmd operations for comprehensive management:
REM Set additional properties after creation
appcmd set vdir "Default Web Site/MyVDir" /physicalPath:"C:\NewPath"
Remember to always run these commands from an elevated command prompt when working with IIS.