🇯🇵 日本語 | 🇺🇸 English | 🇪🇸 Español | 🇵🇹 Português | 🇹🇭 ไทย | 🇨🇳 中文

Introduction to Creating Batch Files (.bat): Automate Tasks with a Single Click

In the previous article, you learned 10 basic Command Prompt commands. You saw how to check contents with dir, navigate with cd, and duplicate files with copy. You've probably realized that while each command is simple, they are powerful tools.

However, manually typing the same combination of commands into the black screen every time is a bit tedious, isn't it? Have you ever thought, "I wish I could run this whole series of tasks with just one button..."? The technology that grants this wish is a traditional automation technique that has long been a part of Windows: the batch file (.bat).

In this article, aimed at beginners who have become a little more comfortable with the Command Prompt, we will carefully explain everything from the basics of creating a batch file to concrete examples of automation scripts that will dramatically ease your daily web development work. By the time you finish reading this article, you'll be able to freely create your very own "helper tools"!


1. What is a Batch File? - A "Script" for Your Commands

In a nutshell, a batch file is a "text file that lists a series of commands you want to execute in the Command Prompt, in order." By giving the file the .bat extension, Windows recognizes it as an "executable program."

When you double-click this file, Windows automatically launches the Command Prompt and executes the commands written in the file one line at a time, from top to bottom. In other words, it's like a "procedure manual" or a "recipe" that performs the sequential command operations you usually type by hand.

It's perfectly suited for automating routine tasks, such as creating a website project folder with the same structure every time or periodically backing up files.


2. Creating Your First Batch File - Hello, World!

First, let's experience the basic workflow of creating a batch file. We'll create a simple script that only displays a message on the screen.

Step 1: Write the Commands in a Text Editor

First, open a text editor like "Notepad" and type the following three lines.

@echo off
echo Hello, Batch File World!
pause

Step 2: Save with the ".bat" Extension

This is the most critical point. When saving the file, please follow these steps:

  1. From the "File" menu, choose "Save As...".
  2. Change the "Save as type" dropdown to "All Files (*.*)".
  3. For the filename, be sure to include the .bat extension, such as "hello.bat".

Save it in an easy-to-find location, like your Desktop.

Step 3: Double-Click to Run

Try double-clicking the hello.bat file you saved. A black Command Prompt window will open, and if your message is displayed, you've succeeded!

Hello, Batch File World!
Press any key to continue . . .

Pressing any key will close the window. This is the entire flow from creating to running a batch file.


3. Writing More Useful Batch Files

Now that you understand the basic flow, let's introduce a few techniques to make your scripts clearer and more flexible.

Add Notes with Comments (`rem`)

Lines beginning with rem (Remark) are treated as comments and are ignored during execution. They are used to make it clear what the process is doing, both for your future self and for others who might read your script.

rem This is a comment. This line will not be executed.
echo This line will be executed.

Using Variables (`set` and `%variableName%`)

If you need to use the same value multiple times, it's convenient to store it in a variable. You define a variable with the set command and access its value using %variableName%.

@echo off
set USER_NAME=Taro
echo Hello, %USER_NAME%!
pause

Practical Automation Script Examples

Now, let's combine what we've learned to create more practical batch files that are useful in a real-world web development setting.

Example 1: Instantly Create a Web Project Folder Structure

This script automates the manual task of creating folders like css, js, and images every time you start a new project.

@echo off
rem --- Batch script to create initial folders for a web project ---

rem Specify the project name
set PROJECT_NAME=MyNewWebsite

echo Creating the %PROJECT_NAME% folder...

mkdir "%PROJECT_NAME%"
mkdir "%PROJECT_NAME%\css"
mkdir "%PROJECT_NAME%\js"
mkdir "%PROJECT_NAME%\images"

echo.
echo The following folder structure was created:
tree /F "%PROJECT_NAME%"
echo.
echo Done!
pause

Keep this batch file on your desktop. When a new project comes along, just edit the PROJECT_NAME, double-click, and the skeleton of your project will be created in an instant.


Example 2: Create a Dated Folder to Back Up Files

This is a script to back up important work files into a folder named after the current date. By using a special variable like %date%, you can automatically retrieve the date the script is run.

@echo off
rem --- Back up a file to a folder with today's date ---

rem Get date in YYYY-MM-DD format (works with US date format MM/DD/YYYY)
set TODAY=%date:~10,4%-%date:~4,2%-%date:~7,2%

rem Specify the backup destination folder path
set BACKUP_DIR="D:\backups\%TODAY%"

echo Backing up to %BACKUP_DIR%...
mkdir %BACKUP_DIR%

rem Specify the path of the file to copy
copy "C:\path\to\important_file.docx" %BACKUP_DIR%

echo Backup complete.
explorer %BACKUP_DIR%
pause

In this example, we've also added a nice touch at the end: using the explorer command to open the newly created backup folder in File Explorer.


Summary

In this article, we learned how to create "batch files," a traditional automation technique in Windows, along with their basic syntax and practical script examples. I hope you've been able to see how compiling commands into a "script" file, rather than typing them one by one into the black screen, can dramatically improve your workflow efficiency.

While batch files are more limited in functionality compared to PowerShell, their simplicity makes them a very powerful tool even today for automating small, routine tasks. I encourage you to look around for "tedious daily clicking tasks" in your own work and think about whether you can automate them with a batch file.

Now that you've experienced the world of Command Prompt and batch files, perhaps a deeper question has formed in your mind: "So, what exactly is the difference between this and the modern PowerShell, and how big is it?" In our next article, we will thoroughly compare and explain those differences!

The Difference with PowerShell: Why You Should Graduate from cmd