Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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

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.