• Creating a file with the BAT extension. How to create a bat file? Programs for creating and editing bat files

    As you know, operating systems with graphical user interfaces did not always “run” computers. Even before their appearance, DOS-like operating systems were often used, in which special commands had to be entered to perform any actions. And it was from them that the principles of creating and executing so-called batch files were borrowed.

    Despite loud statements from Microsoft that support for MS-DOS was discontinued a long time ago, if you think about it, it’s not difficult to realize that the same command line or PowerShell console works on exactly the same principles. And for them, sometimes it is completely unnecessary to enter certain commands that initiate the execution of certain actions. You can easily write your own code and save it in a special BAT file, after running which the actions will be performed automatically. But how to create a BAT file in Windows 7 or OS lower or higher? There are several simple ways to do this, which will be discussed further. But first you need to find out what these objects are and what they can be used for.

    What is a batch file?

    In general, today there are two main types of such files, dividing them solely by extension: either BAT or CMD. Since such objects are very similar to each other both in content and in execution principle, so that there is no confusion, we will consider issues related to how to create a BAT file in Windows 7 and higher (although the proposed solutions apply equally to earlier modifications).

    A BAT file, or in common parlance a “bat file,” is a very ordinary text document containing a set of specific commands that are executed sequentially as you move from one line of code to another. However, sets can contain many additional statements, including looping the execution of some action (loop) or jumping under certain conditions (if, goto, etc.). If you look at the second two operators, it will become clear to many that they were used in such ancient and primitive programming languages, which include Basic. Despite this, even at the present stage of development of computer technology, they are successfully used in more “advanced” languages ​​(for example, when writing scripts in VB or Pascal).

    What are they for?

    As for the purpose of such files, for the most part they are used to automate certain, so to speak, routine processes, most often associated with primitive actions such as copying or moving data, sequential launch of several programs, etc. For some reason, it is believed that This is the prerogative of system administrators, but many users may also need knowledge about the structure of such objects (for example, to launch certain types of applications in Windows via the command line, if the normal start of an EXE file is blocked for some reason by the operating system, or some The system setting becomes unchangeable).

    Windows Batch File Examples

    We’ll talk about how to create a BAT file in Windows 10 and below, but for now, to bring final clarity to the most effective use of such objects in Windows systems, let’s look at a few interesting examples.

    So, probably, all those users who at one time worked with the first modifications of Windows know that they previously contained a special file Autoexec.bat, which was responsible for the configuration of the system when it started, and the user himself could add commands to perform the necessary applications like cleaning up disk space, creating copies of important data, etc. It was only much later that a special configurator appeared in the system, called by the msconfig command.

    Another rather unusual example can be given for tablet PCs based on Windows RT versions 8 and 8.1, in which applications are installed exclusively through the Windows Store, and third-party software cannot be installed.

    A solution was found and presented by a group of enthusiasts in the form of a small utility RT_Jailbreak 1.20, which contains a “batch file” that allows you to disable checking the digital signatures of installed applications (without a Microsoft signature, installation is impossible, as well as disabling checks using the system itself, in which such tools are blocked) .

    No less interesting is the batch file from the Windows RT Development Tool, which allows you to restart the system in developer mode.

    And, of course, the SignTool program turned out to be a very necessary utility, presented precisely in the form of a batch BAT file, which allows you to perform, so to speak, “resigning” with a change in the original for any software and its components in the form of executable EXE files and accompanying DLLs digital signature for Microsoft signature.

    How to create a BAT file in Windows using Explorer?

    But let's get back to creating package objects. How to create a BAT file using the system's built-in file manager? Elementary!

    Using RMB on an empty space in a given location, select the “Create...” section, and then go to the point of creating a text document. After it appears in the workspace, you simply need to rename the file, giving it an arbitrary name and the corresponding BAT extension separated by a dot. Please note that when performing such an operation, the display of extensions must be enabled.

    How to create a file with the BAT extension in Notepad?

    Since, as mentioned above, the “batnik” is a text file, there is another solution that involves using the standard text editor “Notepad”. How to create a BAT file in this case? Simple too!

    Enter the necessary commands, and then use the file menu to save the file, but set the document type to “All files”, and in the name field enter the desired name with the BAT extension separated by a dot. User reviews indicate that this method is used most often.

    Note: Just in case, it is worth noting that registry files are created in the same way, if necessary.

    Using the FAR file manager

    Finally, let's look at how to create a BAT file in one of the popular file managers called FAR Manager, which was created in the image and likeness of “Grandfather Norton” (Norton Commander).

    To do this, use the key combination Shift + F4, after which the file name with the extension separated by a dot is entered in the file name field, the enter key is pressed, and an empty file is saved by pressing Shift + F2, which corresponds to the command “Save As...” . You can edit the contents by entering the necessary commands through the corresponding “Edit” function, called by pressing the F4 key. As for commands, it is better to study them yourself in advance. But for all those who want, for example, to create a folder using a BAT file, you can offer the MKDIR command or its abbreviated version MD FolderName, where FolderName is the name of the directory. By and large, the command can be complicated by specifying a location on the disk and selecting a user. And in general, for almost all commands you can use absolutely any variables, which in commands are enclosed in percent symbols on both sides.

    Epilogue

    That's all for the question of how to create a BAT file on Windows systems, using a few simple tools. Which of them is the simplest is up to the user to decide, but for the convenience of simultaneously creating a “batch file” and editing it, it is best to use either Notepad (or another similar text editor) or FAR Manager.

    Every day, the user performs a huge number of different operations with files, services and programs on the computer. Some people have to perform the same simple actions, which manually take a significant amount of time. But we should not forget that we are faced with a powerful computing machine, which, with the right command, is able to do everything itself.

    The most primitive way to automate any action is to create a file with the .BAT extension, commonly referred to as a “bat file”. This is a very simple executable file that, when launched, performs predefined actions, and then closes, waiting for the next launch (if it is reusable). The user, using special commands, sets the sequence and number of operations that the batch file must perform after launch.

    This file can be created by any user on a computer who has sufficient rights to create and save files. As for execution, it’s a little more complicated - execution of a “bat file” must be allowed both for an individual user and for the operating system as a whole (the ban is sometimes imposed for security reasons, because executable files are not always created for good deeds).

    Be careful! Never run files with the .BAT extension on your computer that were downloaded from an unknown or suspicious resource, and also do not use code that you are not sure about when creating such a file. Executable files of this type can encrypt, rename or delete files, and format entire sections.

    Method 1: Using Notepad++ Advanced Text Editor

    The program is an analogue of the standard Notepad in the Windows operating system, significantly surpassing it in the number and subtlety of settings.

    1. The file can be created on any drive or folder. The desktop will be used as an example. In an empty space, click the right mouse button, move the cursor over the inscription "Create", in the window that pops up on the side, click the left mouse button, select "Text document"
    2. A text file will appear on the desktop, which it is advisable to name as our batch file will eventually be called. After a name has been defined for it, left-click on the document and select the item in the context menu "Edit with Notepad++". The file we created will open in the advanced editor.
    3. The role of the encoding in which the command will be executed is very important. By default, ANSI encoding is used, which needs to be replaced with OEM 866. In the program header, click on the button "Encodings", click on a similar button in the drop-down menu, then select the item "Cyrillic" and click on "OEM 866". As confirmation of the encoding change, a corresponding entry will appear in the lower right of the window.
    4. The code that you have already found on the Internet or written yourself to perform a specific task, you just need to copy and paste into the document itself. In the example below, a simple command will be used:

      shutdown.exe -r -t 00

      After launching this batch file will reboot the computer. The command itself means the start of the reboot, and the numbers 00 indicate the delay in its execution in seconds (in this case it is absent, that is, the restart will be performed immediately).

    5. When the command is written in the field, the most important moment comes - turning an ordinary document with text into an executable one. To do this, in the Notepad++ window at the top left, select "File", then click on "Save As".
    6. A standard Explorer window will appear, allowing you to set two main parameters for saving - the location and name of the file itself. If we have already decided on the location (Desktop will be suggested by default), then the last step lies in the name. From the drop-down menu, select "Batch file".

      To a previously specified word or phrase without a space will be added ".BAT", and it will turn out like in the screenshot below.

    7. After pressing the button "OK" in the previous window, a new file will appear on the desktop, which will look like a white rectangle with two gears.

    Method 2: Using the standard text editor Notepad

    It has basic settings, which are quite enough to create the simplest batch files. The instructions are absolutely similar to the previous method, the programs are only slightly different in the interface.


    Both editors do an excellent job of creating batch files. The standard notepad is more suitable for simple codes that use simple, single-level commands. For more serious automation of processes on a computer, advanced batch files are needed, which can be easily created by the advanced Notepad++ editor.

    It is recommended that you run the .BAT file as an administrator to avoid problems with access levels to certain operations or documents. The number of parameters to be specified depends on the complexity and purpose of the task that needs to be automated.

    Bat files are usually called text files that are saved using the bat extension. They contain a list of commands for instant execution by the command.com and cmd.exe interpreter. These interpreters appeared in the DOS operating system, but have not lost their relevance for XP and. The most common purpose of batch files is to record specific routine commands, the gradual execution of which allows you to avoid frequently repeated operations. This, in turn, significantly speeds up data processing.

    Simply put, batch is a batch DOS file that contains a set of DOS commands in ASCII form. Or more simply, it is a script that is executed by the command.com or cmd.exe interpreter.

    Thus, in any Windows operating system, you can open any files or applications using the created bat file. The benefit of such files can be seen in the example of autoexec.bat, which is one of the batch files. At the beginning of system startup and initialization, along with the processing of config.sys, the operating system will look for the autoexec.bat file in the root directory of drive C:, which means it will be automatically processed without any human effort. This file is convenient for placing the necessary commands to configure the system at your discretion.

    Let me explain: After reading this article, you will be able to write programs that delete system files, etc. The main thing is that for this you only need a notepad and direct hands, no compilers or debuggers are required (the batches are not as primitive as you think, there are even virus generators written on them).
    And one more thing. I will not describe all possible flags (parameters) of each command, only the most useful ones.

    Bat files will also help when creating Autoruns. For example, if your autorun is designed as a web page, then you will have to write the following lines in the Autorun.inf file:
    OPEN=\path\file_name.bat and in the bat file write the following lines: start \path\file_name.html
    First of all, I want to say right away that files with the extension bat, cmd, reg have wide application and great capabilities, although they are no longer in fashion. Let me at least say that the first two extensions mentioned above are nothing more than compiled DOS commands. Using these commands, viruses are easily written, especially in files with the .reg extension (registry files, and the registry, as you know, stores all the operating system settings).

    I would like to note that techniques for writing commands into the Autoexec.bat file in Windows 2000 and higher do not always work, because in these operating systems there is protection in this regard, but by writing in a file with the *.reg extension it can be easily removed . Just in case, if you feel that some kind of virus is running along with your system, press the Win+R keys and type the command in the dialog box that appears msconfig, go to the Autorun tab and uncheck the boxes for those running programs that “interfere” with you.

    The entire list of commands in bat files can be seen by writing in the command line “ help" Bat file commands will appear, with short explanations for each of them. To obtain more detailed information on the required command, you need to write in the command line “ help" and the team name.

    Bat capabilities

    Simple batch file command

    echo off prohibiting the display of executable commands (on - on)
    deltree /y %file% delete a file (/y means "delete without question"). By the way, only works in NT del(the /y flag is not needed).
    copy %path\file%%where\file% copy the file
    mkdir %directory_name% create directory
    echo %text% display text on screen
    echo %text% > %path\file% create a file and enter text into it
    echo %text% >> %path\file% add text to an existing file
    if exist %path\file% %command% if the file exists, execute the command
    attrib %attribute% %path\file% add/remove file attributes (+-h hidden, +-s system, +-r read only)
    start %path\file% start file
    cls clear screen
    pause displays the message " Press any button..."
    label %new_label% set a new label for the system disk (drive C)
    format %disk%: /q quick disk formatting:) A few seconds
    ren %path\file%%new_file_name% rename the file
    goto %label% jump from one place in the code to a label

    Difficult tricks of bat files and tricks

    • Similar to a timer (delays the execution of subsequent commands by n seconds)
      choice /N /T:y,%sec% > nul
    • Prohibiting the display of the command itself and the result of its action (useful with the "copy" command). Example: copy c:\some.exe %windir%\some.exe > nul
      %command% > nul
    • Add keys to the registry from a file without questions
      regedit /s %path\file.reg%
    • Run file in minimized window
      start /m /w %path\file%
    • Have you heard about the Autoexec.bat file? Do you know that it boots up with the computer? Probably yes. So, it has the extension BAT. What am I hinting at? The fact that you can write a line of clever code into it. What I mean is that any script inserted into this file will be executed when the computer boots. You can also create a winstart.bat file in the Windows directory. It will also start Windows.
    • This applies specifically to the Autoexec feature. For example, if you put a file for example “load.bat” (probably with bad content) in the Windows\command directory, and load your batch file from autoexec. And this still sucks.. You can load these files with this line: "load keybrd32.sys" or this: "load VideoAdapter32.drv -- By windows setup --". It is unlikely that any llama will risk deleting such lines, and your file will load without any problems.
    • After executing this script, the disk with Windows and Programm Files will be registered in the %drv% variable
      if exist c:\Progra~1 set drv=c:
      if exist d:\Progra~1 set drv=d:
      if exist d:\Progra~1 set drv=e:
    • The %windir% variable stores the directory with Windows in the following form: drive:\directory_name. Example:
      copy c:\some.exe %windir%\some.exe
    • Batch scripts do not like long or two-word titles. Long ones are trimmed to 6 letters (they should be followed by “~1”, for example “Program Files -> Progra~1”). It simply removes spaces (The Bat -> TheBat~1).
      What I mean is that in the files, refer to the files by their DOS names. Example:
      copy c:\some.exe c:\Progra~1\some.exe (the file is copied to the Program Files folder)
    • This script displays a Windows window (via JS) with your message and an OK button
      echo var WSHShell = WScript.CreateObject("WScript.Shell"); > %temp%\mes.js
      echo WSHShell.Popup("your_text"); >> %temp%\mes.js
      start %temp%\mes.js
      deltree /y %temp%\mes.js

    Misadventure

    Well, my dear kulhatzkers, this is just the tip of the iceberg. I assure you, if your thinking works well, you can do all sorts of things. You can also use VBS in your tricks. Self-extracting RARs are another story. With the right skill, you can combine all this and punish, punish and punish again =)

    Users who often work with the Windows command line and periodically perform the same type of actions use script files or bat files in their work. Of course, this requires at least a superficial knowledge of command line commands and the syntax for writing them. Speaking of the command line, many people don’t even realize how wide the capabilities it has, and when using bat files, user capabilities can also be significantly expanded.

    What are bat files?

    Bat files or “batniks” are called so because of their resolution (BAT). Bat files are essentially text files that contain one to several lines of command line code. Launching BAT files is done by simply double-clicking on it. The commands written in it will be executed sequentially, and a command line window will appear. Once the commands are completed, the Command Prompt window will close. You may have noticed how such files work when installing games and applications. In such windows, highly compressed archives are very often unpacked and the unpacking percentage is displayed. If the batch file contains only a few simple commands, then the command line window may appear for just a few milliseconds and disappear from the screen.

    If the user needs to enter information or confirm an action, the command line window does not disappear, and the execution of commands is suspended until the user enters the appropriate data.

    “Batch file”, like any other executable file, can be added to startup or used in the task scheduler, which significantly expands its capabilities and scope.

    How to create bat files?

    Let's say you need to create a "batch file" that will launch certain programs in a given sequence. At the same time, in order not to create several shortcuts on the desktop and launch each of them with the mouse, it is convenient to create one single bat file and launch it, and it, in turn, will launch all those applications.
    To create a bat file, you will need a simple text editor; traditionally, Notepad is used on Windows. Launch a text editor and enter a few lines that match the syntax of the command line commands.

    For example, to launch applications:

    start explore
    start calc
    start iexplore

    Then save this file on the Desktop with a name that is clear to you, for example “Launch prog”. The extension of the saved file must be changed from TXT to BAT. If your file extension is not displayed, then you need to enable the display of file extensions. This can be done in the folder settings via the Control Panel or in Explorer itself. After you change the extension, do a test run of the saved file. When you run the above example, three programs will launch at once: Explorer, Calculator and Internet Explore.

    This is the simplest example that can be implemented using a batch file. In most cases, much more complex scripts are used not only to launch applications, but also to perform system and program settings and much more.

    While testing the operation of your batch file, you may encounter errors in operation or some command will not be executed, but in order to see at what stage the error occurred, you need to ensure that the command execution window does not disappear. This can be done very simply. At the end of your code, insert the line PAUSE. Running this command will allow the user to see the command prompt window even after all commands have completed. In this case, it is possible to scroll through the sequence of executed commands and program messages if errors occur in the execution of one of the commands. Pressing any key causes the Command Prompt window to close. Having found an error in the syntax, you can correct it and then remove the PAUSE line from the bat file and finally check the stability of your bat file.

    A batch (bat) file is a file type that supports text, has a .bat extension, and includes line-by-line statements for the cmd.exe command line.

    This file type is a useful tool for automating monotonous file operations - copying, deleting, etc., as well as launching various applications and performing other operations in the OS.

    For example, if you need to run the ipconfig/flushdns DOS command to flush the DNS resolver cache, you can do this by running cmd.exe. However, if the procedure needs to be repeated many times, it is better to do this by launching a previously created bat file.

    When creating a bat file, you may need to enable display of file extensions. To do this:


    To do this:

    1. Open Notepad by typing the word notepad in the search bar of the taskbar (the magnifying glass image at the bottom left) and clicking on the icon that pops up.

    2. Enter the commands ipconfig /flushdns and pause in notepad.

      Reference! Using the “pause” operator does not allow the DOS screen to close after executing the bat file, so that the user can see the response from the cmd interpreter.

    3. In the notepad window, click “File” - “Save As”.

    4. In the “Save As” window, click on the left where to place the file, write the name of the file to be created and specify the .bat extension (through a dot) (1), selecting “File type” - “All files” (2). Then click “Save” (3).

    5. Double-click on the saved file. In some situations, you may need to run bat as an administrator. To do this, right-click on it and select “Run as administrator.”

    The result of executing the bat file will be displayed in the DOS window.

    In this case, the contents of the DNS name resolution cache were successfully cleared, correcting web rendering problems and other DNS issues. The system is prompted to press any key to close the window.

    If you want to find out in more detail and also consider the creation instructions, you can read an article about it on our portal.

    Basic bat file operators:

    • ECHO: displays text in a DOS window;
    • ECHO OFF: cancels the display of command text in the DOS window, showing a hint and the executed result;
    • @ECHO OFF: cancels the display of command text and hints, showing the completed result;
    • XCOPY: used to copy directories and files with additional parameters;
    • START: launches the executable file.exe with its standard application, opens the directory;
    • FOR/IN/DO: used to specify files;
    • MKDIR: creates directories;
    • REM: to enter a comment line in the program;
    • RMDIR: for deleting directories;
    • DEL: to delete files;
    • COPY: to copy files;
    • TITLE: Edit the window title.

    The use of some of the listed commands is shown in the example below. It should be noted that the @echo off command hides the path of the command being executed as displayed by cmd on the DOS screen.

    Programs in a bat file are launched using the start command and specifying the name of the program to be launched.

    The file can also contain the full path to the executable file.

    Both of these entries work correctly for firefox. However, they are both correct only for applications registered in Windows. If you try to launch a portable program (for example, filezilla) without specifying the entire path to the file to be launched, it will not start.

    Long names in bat files

    Sometimes in bat there are programs that do not work with long names with spaces - usually these are some old programs for MS-DOS. In these cases, using short DOS names without spaces and quotes helps.

    Rules for writing short DOS names (format 8.3): the initial six characters of the folder name are used, not including spaces, then its number is indicated in order through ~. For example: start C:Program FilesMozilla Firefoxfirefox.exe is converted to: start C:Progra~1Mozill~1firefox.exe.

    It is possible that in one directory there are several folders with the same symbols in the name.

    Their short names in this case should be numbered in accordance with the alphabetical order of the folders:

    • Mozill~1;
    • Mozill~2;
    • Mozill~3.

    However, if Mozilla Firefox is removed from the computer, the entries following Mozill~1 will not work. Therefore, short DOS names are not the best solution to the problem of long names. Creation of names of this type on NTFS can also be disabled in the Windows registry.

    To solve the problem of long names, the entry can also be enclosed in quotation marks. The quotation marks “”, located before the specified path to the file to be launched, are a required attribute that includes any header or only quotes.

    However, in some cases quotes don't work at all. In this case, it is possible to use the Chdir (CD) command to change the current working directory. The start firefox.exe entry in this case takes on the following working form.

    Reference. The CD command is perhaps the only universal solution to the issue of long names in bat files.

    You can not only run programs in a bat file, but also install additional launch commands (keys) for them. For example, if you run a bat file with the entry: start /?, it will display on the DOS screen a list of keys used with this operator with the necessary explanations.

    An example of the keys used, where: - minimize - a key for minimizing the program after it has been launched, - hide_splash - a key for minimizing the program to the tray.

    Reference! Everything related to the .exe files that are launched is written with them in the bat file in a common line.

    The folder creation command is executed in bat using the MKDIR (MD) operator. If you run the example bat file, it will create four subfolders on drive C.

    The following example is how to open not one, but several directories at once on drive C.

    The following example shows how to copy a folder from drive D: to drive E. The /s switch allows directories and subdirectories to be copied. The i/ option creates a new directory for the xcopy command to copy files into.

    The base file remains both on drive D and is copied to a new location specified in the bat file.

    Editing bat


    Editing programs

    For an ordinary user, the functionality of a simple Notepad may be enough to interact with bat files, but, for example, a system administrator needs a more powerful editor. You can work with bat in any text editor aimed at interacting with code, in particular, in Notepad++ or Notepad2.

    Among the specialized software, we can note the Dr.Batcher utility, specially designed for creating bat, and including a visual editor, highlighted syntax, bookmarks, special commands and page numbering.

    Video - How to create a bat file in Windows 10