How to Create a Windows Batch Script to Start/Run a Server

Published on 16 Jan 2024 Tech Automation
image
Photo by Annie Spratt on Unsplash
Disclaimer: This post was created with the assistance of Gemini and/or ChatGPT for informational purposes. While given a quick look over, readers are encouraged to verify key facts independently.

The Problem: Repetitive Morning Routine

Every morning when I start work, I need to run the same set of commands to launch my local development servers and open all the necessary tools. Manually doing this routine became tedious, so I decided to automate it using Windows batch scripts.

My Development Environment Needs

I work with three different projects, each requiring different setup commands:

  • React - Needs Node.js server
  • Django - Needs Python virtual environment and server
  • Laravel - Needs Docker containers

For each project, I need to:

  1. Run the local development server
  2. Open a web browser to the development site or admin panel
  3. Launch Visual Studio Code in the project directory

The Solution: Batch Script Automation

I created separate .bat files for each project saved on my desktop. Each script:

  • Disables command echoing for cleaner output
  • Starts the development server in a new command window
  • Opens the relevant browser tabs
  • Launches VS Code in the project directory

React Project Batch Script

@echo off
start cmd.exe /k "cd Projects/react-project && npm run start"
start chrome http://localhost:8080/
code -n Projects\react-project

Django Project Batch Script

@echo off
start cmd.exe /k "cd Projects && django_virtual_env\Scripts\activate && cd django-project && py manage.py runserver 8000"
start chrome http://127.0.0.1:8000/
start firefox http://127.0.0.1:8000/admin/
code -n Projects\django-project

Laravel Project Batch Script

@echo off
start cmd.exe /k "cd Projects && open -a Docker cd laravel-project && docker compose up -d"
start chrome http://localhost:80/
start firefox http://localhost:8080/phpmyadmin
code -n Projects\laravel-project

The Result: One-Click Project Setup

Now instead of manually running all these commands every morning, I simply double-click the appropriate batch file on my desktop. Each script handles the complete setup for its respective project, saving me time and ensuring consistency in my development environment.

Comments