Server Fault is a question and answer site for system and network administrators. 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

I'm working on Windows XP.

I need the batch file to do the following:

  1. Create a directory $DOMAINNAME on D:/Webserver/domains/%DOMAINNAME/www/
  2. Open the file - D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
  3. Append the following to that file:

    <VirtualHost *:1337>
        DocumentRoot "D:/Webserver/domains/$DOMAINNAME/www"
        ServerName "$DOMAINNAME"
        ServerAlias "$DOMAINNAME"
        ErrorLog "logs/$DOMAINNAME-error.log"
        CustomLog "logs/$DOMAINNAME-access.log" common
    <Directory "D:/Webserver/domains/$DOMAINNAME">
    Allow From all
    AllowOverride All
  4. Open the file - C:/WINDOWS/sysstem32/drivers/etc/hosts

  5. Append this to that file: $DOMAINNAME

  6. Close the files.

I do this manually but it's tiring. Please help.

share|improve this question
As an aside, my antivirus goes mental every time I modify the hosts file. Your antivirus may also similarly object to a script modifying the file and may even veto it. – Ben Pilbrow Jun 8 '11 at 20:53
Are you asking for someone here to write this script for you? Probably not going to get any takers. Have you tried writing this yourself and not had success? Describe what you've done and what isn't working and you'll probably get better answers. – mfinni Jun 8 '11 at 20:54
@Ben Pilbrow, I think that the antivirus can add an exception. If not, how to make the first paragraphs? – Kirill Firsov Jun 8 '11 at 20:55
As @mfinni says, you have to take a stab at this before you'll get help. If it helps, this appears straightforward. We have confidence in your ability to pull it off! – uSlackr Jun 8 '11 at 21:04
I was wrong - you did get two good answers and I voted for both of them. Good for them - but you really have to give things a first shot yourself. Simple scripting like this is so easy, that once you've described in detail what you need it to do, you've written 80% of it. – mfinni Jun 9 '11 at 2:13
up vote 4 down vote accepted

Here you go:

mkdir D:\Webserver\domains\%1
mkdir D:\Webserver\domains\%1\www
echo ^<VirtualHost %1:1337^> >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo DocumentRoot "D:/Webserver/domains/%1/www" >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo ServerName "%1" >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo ServerAlias "%1" >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo ErrorLog "logs/%1-error.log" >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo CustomLog "logs/%1-access.log" common >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo ^<Directory "D:/Webserver/domains/%1"^> >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo Allow From all >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo AllowOverride All >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo ^</Directory^> >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo ^</VirtualHost^> >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo %1 >> C:/WINDOWS/system32/drivers/etc/hosts

Just save it as a .bat file and run it using the domain name you want as a parameter, like this:

script.bat MyDomain

I've also fixed an error in your configuration: you can't have multiple default sites listening on the same port, so the <VirtualHost> directive needs to contain the domain name.

share|improve this answer

You would be better served using PowerShell, which will run on XP. But if you're stuck with .bat:


cd D:\Webserver\domains\
mkdir %1
mkdir %1\www
cd D:\Webserver\apache2\conf\extra\

echo DocumentRoot "D:/Webserver/domains/%1/www"  >> httpd-vhosts.conf
echo ServerName "%1" >> httpd-vhosts.conf
echo ServerAlias "%1" >> httpd-vhosts.conf
echo ErrorLog "logs/%1-error.log" >> httpd-vhosts.conf
echo CustomLog "logs/%1-access.log" common >> httpd-vhosts.conf
echo Allow From all >> httpd-vhosts.conf
echo AllowOverride All >> httpd-vhosts.conf

cd c:\Windows\System32\Drivers\etc

echo "  %1" >> hosts

This is quick and dirty, and includes zero error-catching (what happens when a mkdir fails? This script doesn't care). And keep in mind that AV software gets really, really paranoid about modifications to the hosts file, so it may not be possible to run.

share|improve this answer

Your Answer


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.