AheadKit

Batch File Tutorial: Automate the Windows Tasks You Do by Hand

If you back up the same folders, rename the same files or sort the same downloads every week, a batch file can do it in seconds. This tutorial walks through two real scripts, line by line, in language a beginner can follow.

Cover of Windows Batch Scripting for Everyone from AheadKit

What a batch file is, in plain terms

A batch file is a plain text file, ending in .bat, full of commands that Windows logoWindows already understands. Double-click it and the Command Prompt runs the lines from top to bottom, exactly as if you typed them, except it never gets bored, never misclicks, and finishes in seconds. Nothing to install: Notepad and the Command Prompt are already on your machine.

Before you write anything

  • Make a practice folder, and only ever experiment in it. Scripts do precisely what you wrote, including the mistakes.
  • Write scripts in Notepad or any plain text editor. When you save, choose "All files" in Notepad's save dialog and name the file with .bat at the end, or you will get a .bat.txt file that does nothing.
  • End learning scripts with the command pause. Without it, the window closes the instant the script finishes and you never see its messages.

Script 1: back up a folder with one double-click

This script copies a work folder to a backup location using Robocopy, the copying tool built into Windows for exactly this job:

@echo off
set "SOURCE=%USERPROFILE%\Documents\Work"
set "DEST=D:\Backup\Work"
robocopy "%SOURCE%" "%DEST%" /MIR /R:2 /W:5
echo.
echo Backup finished at %TIME% on %DATE%
pause
  • @echo off stops the window filling with the commands themselves, so you only see results.
  • set stores your two folder paths in named boxes. %USERPROFILE% is Windows shorthand for your own user folder, so the script works on any account.
  • robocopy copies everything that changed. /MIR mirrors the folders so the backup matches the original, /R:2 retries a locked file twice instead of giving up, and /W:5 waits five seconds between retries.
  • %TIME% and %DATE% are filled in by Windows when the line runs.

One warning worth the space: /MIR deletes files in the backup that no longer exist in the original, because a mirror is supposed to match. That is what you want from a backup, but run the script the first time with /E instead (copy everything, delete nothing) and check the result before you let it mirror.

Script 2: sort a messy Downloads folder

This one makes three folders and moves files into them by type:

@echo off
cd /d "%USERPROFILE%\Downloads"
if not exist PDFs mkdir PDFs
if not exist Images mkdir Images
if not exist Archives mkdir Archives
move *.pdf PDFs\
move *.jpg Images\
move *.png Images\
move *.zip Archives\
echo Sorted. Your Downloads folder is tidy.
pause
  • cd /d changes folder, and the /d also changes drive if needed, which plain cd quietly refuses to do.
  • if not exist ... mkdir means "make this folder only if it is not already there", so the script can run every week without errors.
  • move *.pdf PDFs\ uses the star as a wildcard: every file ending in .pdf. Add a line for any type you collect, like move *.docx Documents\.
An interior page of Windows Batch Scripting for Everyone
Inside the book: each command is introduced with a script that uses it, then taken apart line by line, the same way this tutorial treats the two scripts above.

The commands you will use in almost every script

CommandWhat it does
echoPrints a message. echo. prints a blank line.
setStores text or a path in a named variable, like set "NAME=value".
cd /dChanges the current folder, across drives too.
mkdirCreates a folder.
move and copyMove or copy files, with wildcards like *.pdf.
robocopyThe serious copying tool: mirrors folders, retries locked files, logs results.
if existLets a script decide: do this only if that file or folder is there.
forRepeats a command once per file or item. Inside a batch file its variable takes two percent signs, as in for %%F in (*.txt) do echo %%F; typed directly into the Command Prompt it takes one, %F.
remA comment line the computer ignores. Future you says thanks.
pauseWaits for a keypress before the window closes.

Three safety habits worth starting with

  • Test on copies. Point every new script at a practice folder until it has run clean three times. A script with a typo in a path does not ask if you are sure.
  • Read before you run. Any .bat file you download is just text. Open it in Notepad first; if you cannot tell what a line does, do not run that line.
  • Keep your scripts. Save them in one folder with plain names like backup-work.bat. In six months you will have a small toolbox you built yourself, and each one started as five lines like the scripts above.

← All guides

Added to cart