How to Write a Batch Script on Windows

Trending 3 months ago

If you person a task you do repeatedly, penning a elemental Batch record tin prevention you a ton of time.

The Windows TerminalHannah Stryker / How-To Geek

Key Takeaways

  • Batch files are a database of commands executed erstwhile double-clicked. They activity connected modern versions of Windows and are created utilizing a plain matter editor.
  • Simple batch files tin beryllium created by typing commands statement by line. Adding comments and hiding commands pinch "ECHO OFF" tin amended readability.
  • Batch files tin beryllium utilized for much analyzable tasks, specified arsenic redeeming bid outputs to a matter record aliases moving non-interactively to execute aggregate tasks. They tin besides see conditional statements for greater control.

Do you cognize really to usage nan Command Prompt? If you do, you tin constitute a batch file. In its simplest form, a batch record (or batch script) is simply a database of respective commands that are executed erstwhile you double-click nan file. Batch files spell each nan measurement backmost to DOS, but still activity connected modern versions of Windows.

How to Make a Basic Batch File

A batch record is simply a matter record saved pinch nan .bat record extension. You tin constitute 1 utilizing Notepad aliases a much precocious plain matter editor for illustration Notepad++ aliases Visual Studio Code (VSCode), but don't usage a connection processor for illustration Microsoft Word. Word processors usually insert further worldly related to formatting that will forestall your batch book from executing correctly.

PowerShell scripts and Bash scripts whitethorn beryllium much powerful, but batch files tin still beryllium plentifulness useful if you request to run basal Windows commands. They still activity connected some Windows 10 and Windows 11, and will for nan foreseeable future.

Let's create a elemental batch file. First, unfastened Notepad. Type nan pursuing lines into it:

ECHO Hello World
A basal Batch book successful Notepad.

Next, prevention nan record by clicking File > Save. Give it immoderate sanction you like, but switch nan default .txt record hold pinch nan .bat extension.

For example, you mightiness want to sanction it hello_world.bat .

Saving nan batch book pinch nan sanction

You now person a batch record pinch nan .bat file extension. Double-click it to tally it. This peculiar batch record sets ECHO disconnected (which cleans up nan output by hiding nan commands from being printed astatine nan prompt, prints nan matter "Hello World" to nan screen, and past waits for you to property a cardinal earlier it ends.

If you didn't adhd PAUSE to nan file, nan batch record would simply tally its commands and past automatically close. In this case, it would people "Hello World" to nan model and past instantly adjacent nan Command Prompt window. When you want to quickly tally commands without seeing nan output, you tin omit this. If you're moving respective commands, you could spot nan PAUSE bid successful betwixt them.

The Batch book running.

Writing a More Complex Batch File

It's fundamentally elemental to create a batch file. The only point you request to alteration is what you type into Notepad. To tally respective commands, you type each 1 connected its ain statement and nan batch record will tally each 1 successful order.

For example, let's opportunity we want to constitute a batch record that runs respective network diagnostic commands. We mightiness want to tally ipconfig /all to position web information, ping to spot if Google's servers are responding, and tracert to tally a traceroute to and spot if location are immoderate problems connected nan way.

In nan astir basal form, we could simply spot each those commands successful a batch file, 1 aft nan other, for illustration so:

ipconfig /all

When we tally this file, we'd conscionable spot nan output of each bid correct aft nan other. But this isn't needfully nan perfect measurement to constitute a batch file.

The output from nan much precocious batch script.

For example, you mightiness want to adhd remark lines. Any statement that originates pinch a :: is simply a remark statement and won't beryllium executed. That makes them a useful measurement to explicate what's happening successful nan record for anyone you mightiness springiness it to — aliases for your early self, who mightiness hide why you put a definite bid successful there.

You mightiness besides want to adhd nan "ECHO OFF" bid to nan opening of nan file. This is typically added to nan commencement of astir batch files. When you do this, nan commands themselves won't beryllium printed to nan Command Prompt, but nan results will be. For example, you'll spot nan web relationship specifications but not nan "ipconfig /all" line. Most group don't attraction to spot nan commands, truthful this tin cleanable up nan output.

So here's what that mightiness look like:

:: This batch record checks for web relationship problems.
:: View web relationship details
ipconfig /all
:: Check if is reachable
:: Run a traceroute to check nan way to
Command Prompt showing nan output from nan batch book example.

There are different directions you could spell pinch a batch record for illustration this. For example, you mightiness want to person your batch book tally nan supra commands and past dump nan output to a matter record you tin position later. To do so, you'd usage nan >> usability aft each bid to append its output to nan matter file. As we're going to publication nan output from nan matter record anyway, we tin omit nan PAUSE command.

:: This batch record checks for web relationship problems
:: and saves the output to a .txt file.
:: View web relationship details
ipconfig /all >> results.txt
:: Check if is reachable
ping >> results.txt
:: Run a traceroute to check nan way to
tracert >> results.txt

After you tally nan supra script, you'd find a record named results.txt successful nan aforesaid files arsenic nan batch record pinch nan output of nan commands. The Command Prompt model will automatically adjacent erstwhile nan batch record is done running.

The matter record output from nan erstwhile batch script.

The illustration we're utilizing supra relies connected really printing accusation to nan Command Prompt truthful nan personification tin publication it. However, galore batch files are designed to beryllium tally non-interactively. For example, you could person a batch record that deletes aggregate files aliases directories whenever you double-click it. You'd conscionable request to usage nan del bid to delete files aliases nan deltree bid to delete directories. Remember, you're conscionable utilizing nan aforesaid commands you'd tally successful a Command Prompt window.

Fundamentally, that's nan constituent of astir batch files — conscionable moving a fewer commands 1 aft another. However, batch files tin really beryllium importantly much analyzable than this. For example, you tin usage "IF" statements on pinch nan "GOTO" bid to cheque nan worth of thing and past skip to different lines depending connected nan result. This is much for illustration penning an existent mini programme than a speedy and soiled script. That's 1 logic why .bat files are sometimes called "batch programs." If you want to do thing much complex, you'll find plentifulness of guides to doing circumstantial things pinch batch programming online. But now, you cognize nan basics of really to propulsion a elemental 1 together.

Source Tutorials