What is a BAT file and how (and for what) you can create one yourself in a few steps

Though it’s changing into much less frequent, it’s seemingly that on some event you will have come throughout a .bat file in your onerous drive (In case your computing expertise dates again to Home windows 95/98 or earlier, the chance will likely be 100%). The file C:autoexec.bat, for instance, was answerable for regulating the beginning sequence of MS-DOS earlier than it ran Home windows.

‘Bat’ is brief for ‘batch file’ or ‘batch file’, and mainly consists of a textual content file that executes scripts, with the target, often, of automate duties that require numerous directions (from mass renaming of information to putting in a program); Thus, when resorting to a * .bat, it will likely be sufficient to put in writing it as soon as and we will save time and keep away from typographical errors.



Instance of a fundamental Home windows 3.11 Autoexec.bat: the script permits you to configure and cargo drivers, or specify the OS working directories.

Certainly, its usefulness hardly differs from that of a PowerShell or Bash script (within the case that you simply use WSL / Linux), solely that within the case of BATs, the one accountable for executing it will likely be CMD.EXE, often known as the ‘Command Immediate’, the final seen hint within the present Home windows of the outdated MS-DOS.

The way to create and run a .bat file

A .bat file is straightforward to create: it may be carried out from inside Notepad itself —Or some other easy textual content editor— merely altering its extension from .txt to .bat on the time of saving.

The ten most dangerous text commands for Windows, Linux and Mac

We can even use this program to edit the file, as a result of, though we should be cautious to take action right-click on the file and choose ‘Edit’, in any other case we might be immediately executing the script (one other strategy to execute it could be to invoke it the place the Command Immediate).

You’ll be able to see under a check .bat file, subsequent to its content material (in Notepad, proper) and to display output of its execution (in Command Immediate, left):

Example Bat 1

Let’s clarify it a bit: the instruction ‘echo’ reveals messages on the display (‘@echo off’ clears any earlier messages) and ‘pause’ paralyzes the execution of the sequence command ready for the person to press a key.

A helpful .bat file instance: organizing a folder by file extensions

Now we’re going to attempt to create a considerably extra sophisticated batch file, however rather more helpful. In it we are going to see how the conditional statements (execute X provided that Y file exists) and the way they are often insert feedback within the .bat with the ‘rem’ assertion. Subsequent, the system instructions will likely be used ‘mkdir’ (create folder) and ‘transfer’ (transfer file) … you’ll be able to see here the rest of the available commands.

However what’s the objective of this batch file? Easy: create a subfolder for every kind of file current in a folder, grouping the information by separate folders in accordance with their file extension. Assume how helpful it could be to make use of it in folders like ‘Downloads’ or ‘Desktop’:

@echo off

rem For every file current within the listing …

for %%a in (“.*”) do (

rem We examine that the file in query has an extension and that it’s not the .bat that we’re executing …

if “%%~xa” NEQ “” if “%%~dpxa” NEQ “%~dpx0” (

rem We examine that the folder akin to its extension doesn’t exist already and, in that case, we create it …

if not exist “%%~xa” mkdir “%%~xa”

rem And if not, we proceed to maneuver the file to the aforementioned folder.

transfer “%% a” “%% ~ dpa %% ~ xa “))

Be the first to comment

Leave a Reply

Your email address will not be published.


*