askvity

How Do I Create a Run BAT File?

Published in Windows Scripting 4 mins read

Creating a BAT file, also known as a batch file, is a straightforward process on Windows. These simple script files allow you to automate tasks by running a sequence of commands directly in the Command Prompt. To get started, you'll need to create the file itself and then add the commands you want it to execute.

Understanding BAT Files

A BAT file is essentially a plain text file containing a series of commands that are executed by the Windows command-line interpreter (cmd.exe). When you "run" a BAT file (usually by double-clicking it), the system reads and performs the commands within it from top to bottom.

Step-by-Step Guide: Creating Your BAT File

Based on the provided steps, here is how to create the BAT file shell on your desktop:

  1. Right-Click on Your Desktop: Start by finding an empty space on your computer's desktop. Right-click anywhere in this area.
  2. Choose New > Text Document: A context menu will appear. Hover your mouse over the "New" option, and then select "Text Document" from the sub-menu. This will create a new empty text file on your desktop, typically named "New Text Document.txt".
  3. Rename the File to Include the .bat Extension: The most crucial step is to change the file extension from .txt to .bat.
    • Click on the newly created "New Text Document.txt" file to select it.
    • Right-click on the file and choose "Rename", or simply click on the file name and wait a second, then click again to enter rename mode.
    • Change the entire name, including the extension, to your desired name followed by .bat. For example, rename New Text Document.txt to my_script.bat or launch_app.bat. Remember to insert your desired name where 'name' is mentioned in the reference.
    • Press Enter.
    • You may receive a warning message asking if you are sure you want to change the file extension. Confirm by clicking "Yes".

Once you've completed these steps, you will have successfully created an empty BAT file on your desktop.

Adding Commands to Your BAT File

The BAT file you just created is currently empty. To make it "run" something, you need to open it and add the commands you want to execute.

  1. Open the BAT file for Editing: Right-click on the newly created .bat file and select "Edit". This will typically open the file in Notepad or another default text editor.
  2. Type Your Commands: Enter the commands you want the batch file to run, one command per line.

Here are a few simple command examples you could add:

  • echo Hello, World!: Displays the text "Hello, World!" in the command window.
  • pause: Pauses the execution of the script until a key is pressed. Useful for keeping the command window open to see output.
  • start chrome.exe https://www.example.com: Opens the specified website in Chrome.
  • dir: Lists the files and folders in the current directory.
  • mkdir new_folder: Creates a new directory named new_folder.

Example Content:

@echo off
echo This is my first batch script.
echo It will now show the current directory contents.
dir
pause

In this example:

  • @echo off prevents the commands themselves from being displayed as they run.
  • echo ... displays text.
  • dir lists files.
  • pause waits for user input.
  1. Save the File: After adding your commands, save the file (File > Save or Ctrl + S).

Running Your BAT File

To execute the commands within your BAT file, simply double-click the .bat file icon. A Command Prompt window will open, and the commands will run in sequence.

Common BAT File Commands

Here's a brief table of some frequently used commands you might put in a BAT file:

Command Description
echo [text] Displays text or turns command echoing on/off.
pause Pauses script execution until a key is pressed.
dir Lists files and directories.
cd [path] Changes the current directory.
start [app] Opens a file or runs a program.
del [file] Deletes one or more files.
copy [src] [dest] Copies files.

By following these steps, you can create, edit, and run your own simple automation scripts using BAT files on Windows.

Related Articles