How to Create a Batch File

Maintained on

A batch file is a text file that collects multiple Command Prompt commands so they can run all at once. Double-click it and the commands you wrote run automatically, one after another.

Batch files use the extension .bat or .cmd. Both work almost the same way (.cmd is the format for Windows NT and later, and handles the exit code, ERRORLEVEL, of some commands differently). This article uses the widely used .bat.

Batch files can automate tasks such as:

  • Backing up files every day
  • Creating many folders at once
  • Launching applications or switching settings
  • Cleaning up or deleting log files
No programming knowledge required

You can create a batch file without any programming knowledge. Just write the commands you would use in Command Prompt into a text file.

How to Create a Batch File

Step 1: Open a New File in Notepad

Open Notepad. Search for “Notepad” in the Start menu, or press Win + R, type notepad, and press Enter.

Step 2: Write the Commands

Type the following into Notepad.

@echo off
echo Hello! The batch file ran.
pause

Each command is explained below. For now, just type these three lines as they are.

Step 3: Save with the .bat Extension

Choose “File” → “Save As” in Notepad. In the save dialog, make sure to:

  1. Enter the file name as "hello.bat" (wrapped in double quotes)
  2. Set Save as type to “All files (*.*)”
  3. Set the encoding to “ANSI”
Wrap the file name in double quotes

Without double quotes ("), the file may be saved as hello.bat.txt. Always write it as "hello.bat".

If the file icon in File Explorer changes to a gear, Windows recognizes it as a batch file.

Step 4: Double-Click to Run

Double-click the saved hello.bat. A Command Prompt window opens and runs it.

Hello! The batch file ran.
Press any key to continue . . .

If you see this, it worked. Press any key to close the window.

What @echo off Means

@echo off is a standard line written at the top of a batch file.

echo off — Stops the commands themselves from being displayed while the batch file runs. Normally each executed command is shown on screen; with echo off only the results appear.

@ — Hides the echo off command itself.

Here is the difference.

Without @echo off:

C:\Users\user>echo Hello
Hello
C:\Users\user>pause
Press any key to continue . . .

With @echo off:

Hello
Press any key to continue . . .

Unless you have a reason not to, start your batch files with @echo off.

About the echo command

The echo command prints text to the screen, as in echo text to display. See

for details.

What the pause Command Does

The pause command suspends the batch file and shows “Press any key to continue …”.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>pause
Press any key to continue …
C:\users\user>

When you run a batch file by double-clicking, the window closes the moment processing finishes. Putting pause at the end keeps it open so you can read the result.

When running from Command Prompt

If you run the batch file from an already open Command Prompt, the window does not close automatically, so pause is not required. It matters most when you double-click the file.

See

for details.

Writing Comments with rem

Use the rem command to write comments. Lines starting with rem are ignored.

@echo off
rem This batch file is a sample
rem Author: Taro Yamada
rem Created: 2026-04-24

echo Starting...

rem Do some processing here
echo Done.

pause

Comments make the file easier to understand when you come back to it later or when someone else uses it.

Comments with ::

You can also write comments with :: instead of rem, but it can cause errors inside IF blocks and FOR loops. rem is safer. See

for details.

Practical Example: A Backup Batch File

This batch file copies the files in a folder into a backup folder named with the current date.

@echo off
rem ========================================
rem File backup script
rem Backs up to a folder named with the date each time it runs
rem ========================================
setlocal

rem Set the source and destination
set SOURCE=C:\Users\%USERNAME%\Documents\Work
set BACKUP_ROOT=D:\Backup

rem Get the current date for the folder name (e.g. 20260424)
rem Note: the format of %DATE% depends on regional settings (yyyy/mm/dd is assumed here)
set TODAY=%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%
set DEST=%BACKUP_ROOT%\backup_%TODAY%

echo Starting backup...
echo Source: %SOURCE%
echo Destination: %DEST%
echo.

rem Create the destination folder if it does not exist
if not exist "%DEST%" mkdir "%DEST%"

rem Copy the files
xcopy "%SOURCE%\*" "%DEST%\" /E /Y /I

echo.
echo Backup complete.
echo Saved to: %DEST%
echo.

endlocal
pause

Key points of this script:

  • set — Defines a variable. Putting paths in SOURCE and DEST makes them easy to change later
  • %DATE% — An environment variable holding the current date. %DATE:~0,4% extracts part of it. Its format changes with regional settings, so adjust the offsets if your date is not in yyyy/mm/dd form
  • if not exist — Runs the command only if the folder does not exist
  • xcopy — Copies files. /E includes subfolders, /Y skips overwrite prompts, and /I treats the destination as a folder
Change the paths for your environment

Change the source (SOURCE) and destination (BACKUP_ROOT) paths to match your environment. Specifying a path that does not exist causes an error.

Running as Administrator

Some batch files perform operations that need administrator rights, such as writing to system folders or controlling services.

Run as administrator from the right-click menu

Right-click the batch file and choose “Run as administrator”. See

for details.

It helps to note in a comment that the file must be run as administrator.

@echo off
rem * Run this batch file as administrator
rem   Right-click it and choose "Run as administrator"

echo Running with administrator rights...
pause

Common Mistakes and Fixes

Garbled Text (Encoding Problems)

If a batch file containing non-ASCII text such as Japanese shows garbled characters, the file encoding is the most likely cause.

Saving as UTF-8 can cause garbled text

Recent versions of Notepad (Windows 10 version 1903 and later, and Windows 11) save as UTF-8 (without BOM) by default. Command Prompt, however, uses a legacy code page that depends on the system locale (for example, Shift-JIS on Japanese Windows), so a UTF-8 batch file containing non-ASCII text can show garbled characters or fail to run correctly. Set the encoding to “ANSI” when saving.

If you do need UTF-8, add chcp 65001 > nul on the line after @echo off. It may be unstable in some environments. See

for details on character encodings.

The Extension Ends Up as .txt

If you do not wrap the file name in double quotes when saving in Notepad, it may be saved as hello.bat.txt. If extensions are hidden in File Explorer, turn on “View” → “File name extensions” to check.

Paths Containing Spaces

Paths that contain spaces must be wrapped in double quotes.

@echo off
rem Wrong: the path with a space is not quoted
copy C:\My Documents\file.txt D:\Backup\

rem Correct: wrap the paths in double quotes
copy "C:\My Documents\file.txt" "D:\Backup\"
Always wrap paths in double quotes

Windows has many folders with spaces, such as Program Files and My Documents. Get into the habit of always wrapping paths in double quotes.

Summary

ItemDetails
How to createWrite commands in Notepad and save with the .bat extension
@echo offStandard line that hides the commands themselves
echoPrints text to the screen
pauseWaits for a key press so the window stays open
remWrites a comment
EncodingANSI (legacy code page) recommended
Paths with spacesWrap in double quotes

Practice Questions

Practice Problem

What is the effect of @echo off at the top of a batch file?

回答がサーバーに送信されることはありません
Practice Problem

Why do you wrap the file name in double quotes, as in "hello.bat", when saving a batch file in Notepad?

回答がサーバーに送信されることはありません
Practice Problem

If non-ASCII text such as Japanese is garbled in a batch file, what is the most basic fix?

回答がサーバーに送信されることはありません
#Command Prompt #Batch File #bat #Beginner #How to