Close
    Search Search

    How to create a BAT file

    What is a BAT file

    How to create a BAT file

    A file BAT, also called Batch, is a type of file that contains a series of commands arranged on lines in an orderly manner. In summary, it is therefore a script that allows you to perform operations on computers equipped with operating systems from Microsoft.

    In fact, the commands used in a file BAT they are only compatible with operating systems Windows, as they are basic commands for MS-DOS. The latter, in case you didn't know, was a Microsoft operating system on which Windows versions prior to Windows 95 were running. Subsequently, the structure of Microsoft operating systems changed, limiting the environment DOS to a simple command console.



    Having said that, let's move on to the practical side of the matter. You need to know the easiest way to create a BAT file is to use the Block notes Windows, which is the most suitable tool for this type of file, as it does not produce any "dirty" code that can generate anomalies during the execution of the various commands.

    Now that you know what a BAT file is, it's time to see what are some of the more common commands you can use to create scripts to run on your Windows PC.

    The commands in a BAT file

    How to create a BAT file

    I commands declared within a file BAT they can be of different types, according to the operation you want to perform on the computer. Since the BAT file is a script, it can contain various commands, which allow, also through user interaction or confirmation, to perform various automatic procedures on the PC.


    The commands of a BAT file are the same that you can use in the Command Prompt di Windows. This tool is, in fact, the DOS environment emulated by the operating system that interprets the commands MS-DOS. My advice is to always use a file with the extension . BAT, in order to be sure it is compatible with all versions of Windows. Using the extension instead .CMD, it is guaranteed to be compatible only with Windows NT or Windows 2000 operating systems and with later versions of the Microsoft platform.


    Some of the most common commands may involve moving or copying files and folders, formatting a hard drive, running a file, or turning Windows features on and off, just to give you a few examples. In the next few lines, I'll list some commands you can use when compiling your BAT file.

    • cd - is a command that allows you to access a folder on your computer. It requires the indication of the path of the folder (for example cd C: Users [username] Desktop).
    • class - allows you to clear the current prompt screen of displayed text messages.
    • copy (e xcopy) - copies the indicated files, contained in a folder, to another destination.
    • of - delete user specified files.
    • dir - allows you to show a list of files and folders that are present in a directory.
    • threw out - one of the most used commands. Allows you to view text messages on the screen. This is a useful command to provide instructions to the user in case the script requires you to manually perform an input. It also allows you to view the command strings executed by the batch file in their entirety.
    • find - is a search command to locate a string within a file.
    • for - allows you to perform commands repetitively.
    • format - allows you to delete data from a specified disk drive, through a format procedure.
    • if e if not - they are conditional commands that perform operations, respectively, when a condition is satisfied or not.
    • mkdir (o md) - is a command that creates a new folder.
    • move - similar to the command copy, but completely moves files from one directory to another, leaving no copies in the source folder.
    • pause - when creating a script that requires, at some point, input from the user, the command pause it is essential to ensure that subsequent commands are not executed.
    • rmdir (o rd) - deletes a user specified folder.
    • rem - is intended to record comments between script lines, which will be ignored when executing the BAT file.
    • rename (o ren) - allows you to rename files.
    • set - if variables are specified in the script, they can be defined via the command set.
    • sort - is a command that allows you to sort the data.

    In case you want to know other commands that you can use in your BAT file, you can consult the list that is present inside the Command Prompt Windows, by typing the words help on its screen and pressing the key Submit sulla tastiera of the computer.



    You don't know how to start the Command Prompt? Use the key combination Windows + R and type the term in the appropriate box cmd. Then press the button Submit, to start the panel Command Prompt of Windows.

    Compile a BAT file

    How to create a BAT file

    Now that you know the commands you can use in a BAT file to build your script, it's time to get down to business!

    Although you can use any text editor to compile your script, I recommend that you then pour all the code into the Block notes Windows, as it is the ideal, free and default tool of this operating system to create BAT files, without errors being generated when saving scripts.

    Before moving on to the actual creation of the script, I will show you some practical examples of compiling a BAT file, which will help you understand what you have learned so far.

    If you want to create a BAT file through which to display a text message, you will need to use the commands threw out e pause. For example, you can use a code similar to the one shown below.

    @ Echo off

    echo This is an example message from configurehow.com

    pause

    In the example above, the command was used @ Echo off, which allows you not to view the commands found inside the BAT file, but only the result of their execution.


    For this reason, when a command with threw out, the Command Prompt will return the message that is on the same line.

    Finally, the command pause will interrupt the execution of any other subsequent command, if any, waiting for user input, who will have to type a key on the keyboard.


    Just to give you another example, I'll explain how to create a BAT file that allows the execution of a command to shut down the PC.

    shutdown –s –f –t 60

    The above command, shutdown, allows you to perform a shutdown operation of the current computer session. Beside this, you can find the parameters -S, –F e –T that extend its functions: using the attribute -S, the PC must be turned off; the command –F requires the forced closure of all running programs; finally, -t is an attribute that creates a countdown before the command is actually executed (in this case, the 60 expresses the seconds to wait before stopping).

    When you launch a batch file containing this script, Windows will display a screen indicating that your computer will shut down shortly.

    In case you want to abort the command described above, you will need to create another BAT file with a specific command or act from Command Prompt. The command you will use will be shutdown -a, whose attribute -A indicates the request to abort any command shutdown running.

    Create a BAT file

    How to create a BAT file

    At this point, I just have to explain how to create a BAT file containing the script you created. The first thing you need to do is, as mentioned above, open the Block notes Windows, which you can find in the folder Windows Accessories from the menu Home.

    Once this is done, move on to compiling the script or paste the one you created into another text editor. Don't know how to do it? In this case, I recommend that you consult my guide on how to copy and paste.

    Now, click on the items File> Save with nome, found in the menu bar at the top, and select the destination folder for the file. Then type the name you want to give to the file, using the text field File name, making sure to add the wording at the end . Beat (Eg filename.bat). At this point, press the button Save, to confirm.

    Doing so will create a file with the extension.BAT which will contain the code you compiled. In case you want to modify the file, you will simply have to click with the right mouse button on it and select, from the context menu, the item Modification, in order to open it directly with the Block notes Windows. Easy, right?

    How to create a BAT file

    add a comment of How to create a BAT file
    Comment sent successfully! We will review it in the next few hours.