ThisCalculator

Everyday

Batch File Basics: What They Are & How to Use Them

Updated · Standard formula, shown below

Average pace (min/km) 5.00 min/km
Average speed (km/h)
12.00 km/h
Result
Time (minutes)
50 min
You entered
Distance (km)
10 km
You entered

Batch File Basics: What They Are & How to Use Them

Batch files are plain-text scripts that run a sequence of commands in the Windows command prompt (cmd.exe). You write them in any text editor, save them with a .bat or .cmd extension, and double-click them to execute everything inside, line by line. They automate repetitive tasks like copying files, launching programs, cleaning temp folders, or setting up a development environment.

Formula

The calculator above works out your average running pace and speed from a total time and distance. The pace is the time it takes to cover one kilometer, and the speed is the distance covered in one hour.

  • Pace (min/km) = Total time in minutes ÷ Distance in kilometers
  • Speed (km/h) = Distance in kilometers ÷ (Total time in minutes ÷ 60)

In the example, t is the total time in minutes, and d is the distance in kilometers. The pace result is rounded to two decimal places, and the speed is rounded to two decimal places as well.

Worked Example

Using the default values from the calculator block above, where t = 50 minutes and d = 10 kilometers:

  1. Pace = 50 ÷ 10 = 5.00 minutes per kilometer.
  2. Speed = 10 ÷ (50 ÷ 60) = 10 ÷ 0.8333 = 12.00 kilometers per hour.

So with those defaults, the calculator returns a pace of 5.00 min/km and a speed of 12.00 km/h. That matches the output block exactly.

What Exactly Is a Batch File?

A batch file is a text file containing one command per line, executed in order by the Windows command interpreter. The filename extension .bat is the classic one; .cmd works identically on modern Windows but was originally introduced for OS/2. The file itself is not compiled — it is interpreted at runtime, which means you can edit it with Notepad and run it again immediately.

Batch files have existed since the early days of MS-DOS. They were the primary way users automated tasks before graphical interfaces became common. Today they still work on every version of Windows, from Windows 7 through Windows 11, because the command prompt remains a core component of the operating system.

How a Batch File Works

When you run a batch file, cmd.exe reads the file line by line and executes each line as if you had typed it into the command prompt yourself. There is no separate runtime or installation step. Key mechanics include:

  • Echo — controls whether commands are displayed as they run. @echo off at the top hides the commands and shows only their output.
  • Variables — set with set var=value and read with %var%. They are text strings, not typed numbers or booleans.
  • Labels and GOTO — :label marks a spot, and goto label jumps to it. This is the old way to create loops and branches.
  • Conditionals — if exist, if %var%==value, and if errorlevel let the script make decisions.
  • For loops — for %%f in (*.txt) do ... iterates over files or numbers. Inside a batch file, loop variables use two percent signs; on the command line, they use one.

A simple example that copies all .jpg files from one folder to another might look like this:

@echo off
set source=C:\Users\YourName\Pictures
set dest=D:\Backup\Pictures
copy "%source%\*.jpg" "%dest%"
echo Done.

That is the whole script. It sets two variables, copies the files, and prints a message.

Common Uses and Real Examples

Batch files are most valuable for tasks you repeat regularly. Typical uses include:

  • File backup — copying or moving files to another drive or network share.
  • Cleanup — deleting temp files, clearing browser caches, or emptying the Recycle Bin.
  • Launching multiple programs — opening your editor, terminal, and browser together for a project.
  • Environment setup — setting PATH variables or creating folders before a build.
  • Scheduled tasks — combined with Task Scheduler, a batch file can run daily or at startup.
  • Network diagnostics — running ping, tracert, and ipconfig in sequence and logging the output.

For example, a cleanup script might be:

@echo off
del /q "%TEMP%\*"
for /d %%d in ("%TEMP%\*") do rmdir /s /q "%%d"
echo Temp folder cleared.

That deletes every file in the user’s temp folder and then removes all subfolders, including their contents. Note the /q flag suppresses confirmation prompts, and /s on rmdir removes the folder tree.

How to Write and Run Your First Batch File

Writing a batch file requires no special software. The steps are:

  1. Open Notepad (or any plain-text editor).
  2. Type your commands, one per line.
  3. Save the file with a name ending in .bat, for example myscript.bat.
  4. In the Save dialog, set “Save as type� to “All Files� so Notepad does not add .txt to the name.
  5. Double-click the file in File Explorer to run it, or open a command prompt and type its name.

If the script does nothing visible, it may be because the window closes instantly on error. To see the output, open a command prompt first (type cmd in the Start menu), then drag the .bat file into the window and press Enter. The window stays open and shows any error messages.

Common Problems and How to Fix Them

Batch files fail in predictable ways. The most frequent issues are:

  • Paths with spaces — always wrap paths in double quotes: "C:\Program Files\App". Without quotes, the space splits the path into two arguments.
  • Variables inside parentheses — within a for loop or if block, %var% is expanded once at parse time, not each iteration. Use !var! with setlocal enabledelayedexpansion to read the current value.
  • Echoing blank lines — echo. prints an empty line. A bare echo shows whether echo is on or off.
  • Admin rights — some commands (like net stop or writing to Program Files) require an elevated prompt. Right-click the file and choose “Run as administratorâ€�.
  • Line endings — batch files saved with Unix line endings (LF only) can behave erratically. Save as Windows CRLF, which Notepad does by default.

A classic mistake is using if %var%==value when the variable is empty, which produces if ==value and a syntax error. Wrap both sides in quotes: if "%var%"=="value".

Batch Files vs. PowerShell vs. Modern Alternatives

Batch files are the oldest scripting tool on Windows, but they are not the only one. PowerShell is the modern replacement, offering object-oriented pipelines, real data types, and access to the .NET framework. For example, copying files in PowerShell uses Copy-Item, and you can filter with Where-Object instead of using for loops and if statements.

Python or other scripting languages are also common for cross-platform automation. However, batch files still have advantages: they require no installation, work on every Windows machine, and are instantly readable by anyone familiar with the command prompt. For simple file operations and legacy system administration, they remain perfectly adequate. The choice depends on the complexity of the task — if you need to parse JSON or call web APIs, PowerShell or Python is better; if you just need to copy a folder on a schedule, a batch file is fine.

Safety and Best Practices

Batch files can delete or overwrite data without confirmation, so caution is essential. Follow these habits:

  • Test on sample files or a disposable folder before running on real data.
  • Use echo statements to log what the script is doing.
  • Avoid del /s /q on broad paths like C:\ unless you are certain of the consequences.
  • Back up important files before running a new script.
  • Never run a batch file you downloaded from an untrusted source without reading it first. A batch file can execute any command with your user permissions.

Also be aware that antivirus software sometimes flags batch files because they can be used for malicious purposes. That does not mean your file is dangerous — it just means you should know what it does before running it.

Frequently Asked Questions

Can I convert a batch file to an executable (.exe)?
Yes, there are tools that wrap a .bat file into an .exe, but they are not official Microsoft products. The resulting file still runs cmd.exe in the background. For most purposes, keeping the .bat extension is simpler and more transparent.

Why does my batch file close immediately?
That happens when the script finishes (or errors out) and the console window closes. To keep it open, add pause at the end of the file, or run it from an already-open command prompt window.

What is the difference between .bat and .cmd?
Functionally, they are the same on modern Windows. The .cmd extension was originally for OS/2 and is treated slightly differently by the command processor in some edge cases, but for everyday scripts either works.

Can batch files run on macOS or Linux?
No. Batch files depend on Windows command-line tools. On macOS or Linux, you would write a shell script (.sh) instead. Some tools like Wine can run cmd.exe, but that is not a practical solution.

How do I pass arguments to a batch file?
Inside the script, %1 is the first argument, %2 the second, and so on. %0 is the script name itself. For example, myscript.bat file1.txt makes %1 equal to file1.txt.

Is there a way to pause a batch file until a key is pressed?
Yes, the pause command waits for any key. If you want to wait for a specific key, use choice /c ab and check errorlevel to see which one was pressed.

Related

Formula used
in t | Time (minutes) | min | 50 | 0
in d | Distance (km) | km | 10 | 0
out pace | Average pace (min/km) | min/km | t / d | 2
out speed | Average speed (km/h) | km/h | d / (t / 60) | 2

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.