Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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

Creating a virtualhost in Windows environment is a lot of hassle. You have to create the site folder, edit httpd.conf, hosts and whatnow. In order to automate some of that I decided to try and create a batch file to automate some of that. Here what I came up with for starters:

@ECHO OFF

::location of vhosts
SET BASEURL=D:\web\vhosts\

:sitenameprompt
::ask for a sitename
set /p sitename="Enter Sitename: " %=%

echo Before IF
IF EXIST %BASEURL%%sitename% echo The specified site folder already exists! Choose another. 
IF EXIST %BASEURL%%sitename% goto sitenameprompt 

IF NOT EXIST %BASEURL%%sitename% echo Creating folder 
IF NOT EXIST %BASEURL%%sitename% MD %BASEURL%%sitename%

echo Adding virtualhost to httpd.conf
pause
(
echo ###%sitename%###
echo    <VirtualHost *:80>
echo        ServerAdmin admin@%sitename%
echo        DocumentRoot "%BASEURL%%sitename%"
echo        ServerName %sitename%.dev
echo        ServerAlias %sitename%.dev
echo        <Directory "%BASEURL%%sitename%">
echo            Options Indexes FollowSymLinks Includes ExecCGI
echo            Order allow,deny
echo            Allow from all
echo        </Directory>
echo    </VirtualHost>
) >>D:\web\xampp\apache\conf\extra\httpd-vhosts.conf

pause
echo Restarting apache
::httpd -k restart

Now when running this script it gives a syntax error at the httpd.conf part. Bear in mind I am rather new at this so its propably something obvious. So where did I screw up?

share|improve this question
up vote 0 down vote accepted

ugly signs <>|& must be escaped in batch with a caret ^, try this:

@echo off
(
echo ###%sitename%###
echo    ^<VirtualHost *:80^>
echo        ServerAdmin admin@%sitename%
echo        DocumentRoot "%BASEURL%%sitename%"
echo        ServerName %sitename%.dev
echo        ServerAlias %sitename%.dev
echo        ^<Directory "%BASEURL%%sitename%"^>
echo            Options Indexes FollowSymLinks Includes ExecCGI
echo            Order allow,deny
echo            Allow from all
echo        ^</Directory^>
echo    ^</VirtualHost^>
) >>D:\web\xampp\apache\conf\extra\httpd-vhosts.conf
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .