It is a type of script file used in Microsoft Windows to automate tasks via a series of commands. It is executed by the Command Prompt (cmd.exe) or PowerShell and is widely used for system administration, configuration, and automating repetitive tasks.
Commands
Print/Comments
@echo off
echo Only output of the commands are displayed
@echo on
echo Output as well as commands are displayed
echo. //prints blank space
Title of Batch file:
title title_of_batch_file
Create a folder:
mkdir folder_name
Create a file:
type nul > filename.txt
Press any key to connect...
pause
Escape reserved characters
echo greater than sign ^>
echo lesser than sign ^<
Display the text in the file
type filename.txt
How to create a batch file?
1) Open Notepad
2) Type batch script
3) Save in .bat format
4) Open command prompt
5) Type "file_name.bat" in command prompt
MERN Stack batch file
Note: In the 2nd step, instead of add your name
@echo off
title MERN Stack setup!
echo 1)Git initialization
git init
echo Git initialization is completed
echo Press any key to continue....
pause
echo 2)Backend part
mkdir backend
cd backend
(
echo {
echo "name": "backend",
echo "version": "1.0.0",
echo "description": "Backend part",
echo "main": "index.js",
echo "type": "module",
echo "dependencies": {},
echo "devDependencies": {},
echo "scripts": {
echo "start": "nodemon index.js"
echo },
echo "author": "<name>",
echo "license": "ISC"
echo }
) > package.json
start /wait cmd /c "npm i dotenv express jsonwebtoken body-parser bcrypt cookie-parser mongoose uuid cors cross-env"
start /wait cmd /c "npm i nodemon --save-dev"
echo Backend dependencies installed successfully
echo Press any key to continue....
pause
echo 3)Folder structure
echo Index file creation
type nul > index.js
echo import app from "./app.js"; >> index.js
echo. >> index.js
echo app.listen(4000, ()=^>{ >> index.js
echo console.log("Started at port: 4000"); >> index.js
echo }); >> index.js
echo Press any key to continue....
pause
echo App file creation
type nul > app.js
echo import cors from "cors"; >> app.js
echo import express from "express"; >> app.js
echo. >> app.js
echo const app=express(); >> app.js
echo. >> app.js
echo app.use(cors({ >> app.js
echo origin: 'http://localhost:3000', >> app.js
echo credentials: true, >> app.js
echo methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], >> app.js
echo allowedHeaders: ['Content-Type', 'Authorization'], >> app.js
echo })); >> app.js
echo. >> app.js
echo export default app; >> app.js
echo Press any key to continue....
pause
echo Folder creation
mkdir models
mkdir routes
mkdir controllers
mkdir config
cd config
type nul > config.env
cd ..
echo Folder creation completed successfully
cd ..
echo 4)Frontend part
start /wait cmd /c "npx create-react-app frontend"
cd frontend
start /wait cmd /c "npm i react-router-dom axios"
echo Frontend dependencies installed successfully
echo "Installation completed!!"
Stay Connected!
If you enjoyed this post, don’t forget to follow me on social media for more updates and insights:
Twitter: madhavganesan
Instagram: madhavganesan
LinkedIn: madhavganesan