How to create cmd executable file. How to create a file with cmd extension? Basic commands, syntax and examples of using batch files

The use of a graphical interface in operating systems today seems to be something taken for granted and completely natural, but this was not always the case. The first operating system, MS DOS, developed by Microsoft, did not have a GUI, and was controlled by entering text commands. Almost 40 years have passed since then, but the command line scripting language is still popular, and not only among developers.

The command line is not so convenient, but with its help you can perform operations that are not possible from the GUI. On the other hand, launching the console every time, entering commands into it one after another - all this greatly slows down the work. However, you can significantly simplify the task by creating a bat file or simply a batch file - a text file with the BAT extension containing a list of instructions processed by the CMD command interpreter. Such files are used to automate various tasks, for example, to delete temporary files on a schedule or launch programs.

How to create a file with a BAT extension

So, how to create a bat file in Windows 7/10? Very simple. To do this, you will need any text editor and knowledge of the basics of the command line. You can use Notepad, or even better, Notepad++, since the latter has syntax highlighting. Create a new file in the editor, select “Save As” from the “File” menu, give the future script a name, and select “Batch file (*bat; *cmd; *nt)” in the “File type” drop-down list.

If you want to use Notepad to create a bat file, you need to assign the extension manually, and select “All files” in the “File type” list.

As you can see, creating a file with the bat extension is not difficult; however, there are some subtleties here. Line breaks cannot be used in command files; the encoding of the bat file must be set to UTF-8; if the body of the script uses Cyrillic, the encoding must be changed by inserting the chcp 1251 command in the appropriate place.

Instead of the BAT extension, you can use CMD, the result of executing the script will be exactly the same.

Basic commands, syntax and examples of using batch files

You know how to make a bat file, now it’s time for the most interesting part, namely the syntax of the CMD interpreter language. It is clear that an empty batch file will not work, it will not even launch when you double click on it. For the script to work, at least one command must be written in it. For a visual example, let's see how to write a bat file to launch programs. Let's say that when you start working, you launch three programs every time - Chrome, Firefox and VLC. Let's simplify the task by creating a script that will launch these programs itself at intervals of five seconds.

Open an empty batch file and paste the following commands into it:

Start "" "C:/Program Files/Google/Chrome/Application/chrome.exe" timeout /t 05 start "" "C:/Program Files/Mozilla Firefox/firefox.exe" timeout /t 05 start "" "C :/Program Files/VideoLAN/VLC/vlc.exe"

Team start launches the executable file of the desired program, and the command timeout/t sets the interval between starts. Pay attention to the placement of the quotes - they contain paths that contain spaces. Also, if there are Cyrillic characters in the path, you should insert a command that changes the encoding at the beginning of the script chcp 1251, otherwise the interpreter will not be able to read the path correctly.

When you run the script, four console windows will be opened sequentially; this is normal; after executing the commands, they will all automatically close, however, you can make sure that only the first window opens. To do this, the application launch code should be changed as follows:

Start /b "" "path"

It may also happen that at some point it will be necessary to pause the execution of the script so that the user can decide whether to execute all other commands or not. There is a command for this pause. Try replacing timeout with it and see what happens.

Start /b "" "path" pause

Let's look at another example of commands for a bat file. Let's write a script that will turn off the computer in one case, and restart it in another. For these purposes we will use the command shutdown with parameters /s, /r And /t. If you wish, you can add a request to perform an action to your body file, like this:

@echo off chcp 1251 echo "Are you sure you want to turn off your computer?" pause shutdown /s /t 0

Let's explain. The first command hides the text of the commands themselves, the second - sets the Cyrillic encoding, the third - displays a message for the user, the fourth - sets a pause, the fifth - turns off, and with the key /r instead of /s reboots the computer without the traditional one minute delay. If you don’t want to stand on ceremony with requests and pauses, you can leave only the fifth command.

If instead of Russian text when executing the command you see kryakozyabra, try converting the script file to ANSI.

What else can you do with scripts? A lot of things, for example, deleting, copying or moving files. Let's say you have a certain data folder in the root of drive D, the contents of which need to be cleared in one fell swoop. Open the batch file and paste the following command into it:

Del /A /F /Q "D:/data"

Or you can do this:

Forfiles /p "D:/data" /s /m *.* /c "cmd /c Del @path"

Unlike the first, the second command deletes files recursively, that is, all files in the data folder will be deleted plus those located in subdirectories.

Here's another useful example. Let's write a script that will create a backup copy of the contents of one folder and save the data to another. The command is responsible for copying robocopy:

Robocopy C:/data D:/backup /e pause

By running such a batch file for execution, you will copy the entire contents of the data folder to the backup folder, including subdirectories, empty and with files. By the way, the robocopy command has many parameters that allow you to configure copy parameters very flexibly.

Run bat files as administrator and on a schedule, hidden bat launch

Now you know how to create batch files and have some general understanding of the CMD interpreter language. These were the basics, now it's time to get acquainted with some useful features of working with bat files. It is known that programs require administrator rights to perform some actions. Batniks may also need them. The most obvious way to run a script as an administrator is to right-click on it and select the appropriate option from the context menu.

In addition, you can make sure that a specific batch file will always be launched with elevated privileges. To do this, you need to create a regular shortcut to such a script, open its properties, click the “Advanced” button and check the “Run as administrator” checkbox in the window that opens. This method is also good because it allows you to select any icon for the shortcut, while a file with a BAT or CMD extension will always have a nondescript appearance.

Scripts, like all applications, can be launched on a schedule. Team timeout/t is not entirely appropriate here; for delayed launch, it is best to use the built-in Windows Task Scheduler. Everything is simple here. Open with the command taskschd.msc Scheduler, decide on the trigger, select the action “Run program” and specify the path to the bat file. That's all, the script will be launched at the scheduled time.

And finally, one more interesting point. When you run a bat file, a command line window appears on the screen, even if only for a fraction of a second. Is it possible to make the script run in hidden mode? It is possible, and in several ways. The simplest one is as follows. Create a shortcut for the bat file, open its properties and select “Collapsed to icon” from the “Window” menu. After this, the only visible sign of the script running will be the appearance of the CMD icon on the taskbar, but no windows will open.

If you want to completely hide the execution of the script, you can use a “crutch” - the VBS script, which will launch your batch file in hidden mode. The script text is below, save it to a file hidden.vbs, having previously replaced the path in the second line of code D:/script.bat path to your body file.

Set WshShell = CreateObject("WScript.Shell") WshShell.Run chr(34) & "D:\script.bat" & Chr(34), 0 Set WshShell = Nothing

There are also other options, for example, using the utility Hidden Start, which allows you to run executable and batch files in hidden mode, including without an invitation.

And that's all for now. Information regarding creating BAT scripts can be easily found on the Internet. It's also a good idea to check out William Stanek's Microsoft Windows Command Line tutorial. Despite the fact that more than ten years have passed since the publication of the book, the information contained in it is still relevant.

Hello, if you have looked at my blog page, it means you need help communicating with your computer. I will help with all I can. The topic of today's discussion is “How to create a txt file?” I was surprised to see this question in the comments to one of the articles, since the actions performed when creating basic formats such as txt, doc, xls have been brought to automaticity. And if such a task needs to be completed, I just do it without thinking about how exactly. I looked at the query statistics on Google. It turned out that many people ask similar questions - how to create a txt file on Windows 7, 10 and other operating systems (MacOS, MS-DOS, Linux), how to create a txt text file on the command line, in a folder, in Word, how create a txt file on your phone. After searching in my head and on the Internet, I collected everything I could find on this topic. This is how this article was born, which I decided to start with the most common operating system - .

Windows

So, let's talk about how to create a .txt file on computers with the operating system windows 10, 7 and earlier, outdated, but fully functional versions. Friends, it seems to me that it would be quite appropriate to first explain what a text file with the txt extension is, and only then move on to talking about how to create it.

Definition

A file is an original item on a computer that contains some information. It has a name and an extension. With the name, I think everything is clear and there is no need to explain. An extension is a continuation of the name, indicating to the system the format of the document and which program to open it with. Format is the specificity of the information that is in the content, that is, text, graphics, tables. A text file in txt format is a document containing only pure text, without any formatting or pictures, which can be opened by any text editor (by default, the standard Notepad program), and we will look at how to create it in detail below.

Context menu

The simplest and most common way to create a text file, which I use, like most Windows users, contains the following steps:


The text document is ready. You can open it with two quick clicks or through the corresponding context menu item, which is called up by right-clicking on the document.

Folder

Friends, although you can create a txt file anywhere using the method described above, let me tell you how to do this using the resources of a specific folder:

  • We go to the desired folder.
  • At the top of the window, select the “File” section and open its menu.
  • Among the proposed options, select the “Create” action and then “Text document”.

Word

As I already mentioned, a text file is opened by Notepad by default, but you can create a txt document with other text editors, as well as convert it from existing documents with the docx extension. You can get a document with a given extension from a doc or docx element in three ways:

  • The first method is to open Notepad (located in the list of standard programs in the Start menu) and copy the contents of the docx document into it.
  • Second way:
    • Right-click on something.docx and select the “Rename” action.
    • We delete the existing extension and write txt instead.
    • Press Enter to confirm.
    • We answer the system’s warning question about the danger of changing the extension in the affirmative.
  • Third way:
    • Open the existing docx document in Word or another text editor.
    • Open the “File” menu and select “Save As”.
    • In the list of suggested options, select “Other formats”, which will open a new window.
    • At the bottom of the window there are two lines “Name” - you need to set it and “Type” - here you need to install the extension.
    • By clicking on the arrow in the corner of the “Type” line, we open the list of possible extensions and set “Plain text (*.tхt)”.
    • Confirm saving the document in the specified format by clicking the “Save” button.

By the way, the third method is quite suitable not only for changing the extension of existing documents, but also for creating a new one in the required format.

Command line

You can also create a txt file using the cmd.exe application. This is not so simple and not always clear, and you also need to know the commands. But still, I’ll tell you about this briefly, maybe someone will find it useful. So, using the command line, you can create two types of txt files - empty and filled, that is, with content.

Empty

To create an empty file with a txt extension using the command line, you must first launch it. There are several ways to do this, here are the simplest:

  • Press Win and R, which will open the “Run” window, enter the command cmd.exe and press OK.
  • From the Start menu, select Command Prompt (Admin).
  • In the Start search bar, type cmd and run the found application as administrator.
  • copy con filename.txt – will create the desired document in an open directory; to make it appear in another place, along with the name, enter the full path to the desired folder.
  • echo off > C:\111.txt, where "echo" is the "create" command, "off" means no text, and C:\111.txt is the name and path of the location.

With text

Using the same commands, but with some modifications, you can create a .txt file and immediately write data to it:

  • Enter copy con filename.txt, then write the required text, press CTRL and Z, press Enter and get what you need.
  • In the second command - echo off > C:\111.txt - the required text must be written instead of “off”.

By the way, if you are looking for an answer to the question “how to create a file with a txt extension in MS-DOS?”, then check out the methods described above, as they are also relevant for this operating system.

Linux

Friends, let's talk about how to create a txt file in the Linux operating system. This can be done using the terminal, which is analogous to the command line. There are several commands, I will tell you about the shortest ones. So, just like with the command line in Windows, to create a .txt text file in Linux, you will first need to launch a terminal. To do this, press three keys simultaneously - CTRL, ALT and T. Then enter one of the following commands:

  • For zeros - > a or >a (a space is optional), where a is the name of the document that will be created in the current folder. If you need to create several similar documents at once, then instead of “> b > c > d > e > f > g” it will be more convenient to write touch b c d e f g.
  • For filled ones - echo tekst > h or the same, but without spaces (no difference) - echo tekst>h. In this case, we will get a document with the name h and the text tekst in the current folder.

MacOS

It's time to talk about how to create a txt file on devices running MacOs. There are, as always, a lot of ways. I'll tell you the simplest one:

  • We launch the terminal - the easiest way is through the Spotlight search bar (press spacebar and CTRL at the same time, write “Terminal”).
  • Enter the command $ touch some_text_file.txt, but only after going to the desired directory.

Android

Very often people ask on the Internet how to create a txt text file on Android. My answer is that this cannot be done with the resources of the phone itself. It is necessary to install special applications, which are available in bulk in the Play Market service - “Simple Text Editor” (analogous to Notepad), Text Editor. There are those in which the text can be dictated by voice, and it will be converted into the desired format (Speech text editor). Or, conversely, the application converts selected text fragments into speech and reads them in voice (Text Editor).

FAR Manager

Among the most popular search queries on Google is “how to create a txt file in far.” I’ll tell you about this in a nutshell - I’ll help people. To create the required document in FAR Manager, perform the following steps:

  • Press two keys at the same time, one of which is SHIFT and the other F4.
  • An empty creation prompt appears.
  • We write any name and put the required extension - 12345.txt and get what we wanted.

Friends, so I shared everything I wanted. It turned out to be quite a lot of information. I hope it is useful to someone. If something is unclear, write in the comments, we will figure it out together. Moreover, I myself enjoy this process, since in search of comprehensive answers to your questions I learn a lot of new and useful things for myself. Goodbye.

Launch Command Prompt. Click on the Start menu and find the search bar. Enter “command prompt” or “cmd” into it. Double-click Command Prompt in the list of results to launch the utility. By default, the Command Line looks like this: C:\users\Username> .

Create a new folder. Use the mkdir command to create a new folder. To create a folder, you need to enter "mkdir->folder name". In the example above, a new wikihow folder was created using the command: mkdir wikihow.

Change the current active directory. To change to a different folder, use the "cd" command, or change directory. To do this, enter the following: cd -> folder name. In our example, you need to enter cd wikihow. As shown in the picture above, the new line will look like this: C:\users\Ivan\wikihow> .

Check the contents of the folder. To check the contents of the current folder, use the dir command. Simply type dir and press Enter, and Command Prompt will display a list of the folder's contents.

Clear your screen data. To do this, use the cls command. Just type cls and press ↵Enter to clear content from the screen. As shown in the example above, only the command line will remain on the screen.

Create a new file. To create a new file, enter the NUL > command. Type NUL > file name and press Enter to create a new empty file. In the example above, NUL> newfile was entered.

Create another file. Now repeat step 6 to create another file. Name this file newFile1. To do this, you need to enter the command NUL> newFile1.

Check the contents of the folder. Now check the contents of the folder using the dir command. As shown in the example above, the wikihow folder now contains two new files: newFile and newFile1.

Delete files. To delete files, use the del command. Enter del -> file name to delete a specific file. Type del newFile to delete the file named newFile. Now check the contents of the wikihow folder and make sure that the newFile file has been deleted. Clear data from the screen with the cls command.

Navigate further up the directory tree. To perform the next step (delete a folder), you first need to leave the current active directory. To do this, use the change folder version of the command. Type cd.. to change to the parent directory without entering its name. Enter: cd.. as shown in the example above. Notice that the line now says C:\users\Brian>, which means you are no longer in the wikihow folder.

Delete the empty folder. To delete a folder, use the rmdir command. While you are in the folder, it cannot be deleted (see step 10). If the folder is empty (there are no files in it), you can delete it by simply entering the command rmdir -> folder name. In our example, the wikihow folder still contains newFile1, so the rmdir command will not work. As shown in the example above, if the folder is not empty, you will receive an error message.

To do this, in Windows XP, click the “Start” button, then “Run” and enter “cmd” there. In Windows 7 - the “Start” button, enter “cmd” in the search field, right-click on the result that appears and select run from. When prompted to launch, click “Yes”.

Now you need to find the directory where the problematic file is located. Initially you are in the system directory “C:Windowssystem32”. To view the contents of the directory, type “dir /p” (“p” is responsible for page-by-page) and the computer will give you a list of files and subdirectories; use the Enter button to go to the next page. In Windows 7, "p" is optional, since this OS has the ability to scroll the contents of the command line. To display only directories, use the key “/ad” (“dir /ad”), only files – use the key “/b” (“dir /b”).

To change to another directory use the command “cd”< путь к каталогу >”(for example, by typing “cd C: Windows”, you will be taken to the Windows directory; from the initial one you can also get there with the command “cd ..”, which serves to go back one level). If you need to change the disk, enter "<буква диска>:" (for example "D:").

Now that you have found the directory and the file you need in it, you just need to enter the name of this file. The file will be opened using a program that, according to the established parameters, should open it. In the future, it is not necessary to go to the directory with the file every time; it is enough just to remember the full path to it (see the image for the step).

Share with friends or save for yourself:

Loading...