S on the command line. CMD commands: list, description and application. Network CMD Commands
Hi all. In this article, we will talk about topics such as:
Command line (CMD) is a separate program that allows the user to directly interact with the operating system. Command line is based on an environment in which applications and utilities are executed using a text-based interface, and the execution result is displayed on the screen.
Command line Windows uses a command interpreter cmd.exe, which allows you to download applications and directs the flow of data between them, in other words, translates user commands into a form understandable to the system. Command line console integrated into all versions of Windows OS. At first glance command interface scares the user who is spoiled by the graphical interface of the same Windows, but as a rule command interface, is much faster and has a lot of additional features that cannot be implemented in a graphical interface.
Launch methods:
- Start / All Programs / Accessories / Command Prompt.
- Start / Run / enter cmd.exe in the line
- Run from the system folder: C:\WINDOWS\system32\cmd.exe
CMD commands.
Below I will give you a sign with a complete list command line commands, and after the tablet we will look in more detail at the main CMD commands.
Team | Description |
---|---|
ASSOC | Display or change associations based on file name extensions. |
AT | Execute commands and launch programs according to a schedule. |
ATTRIB | Display and change file attributes. |
BREAK | Enable/disable the CTRL+C key combination processing mode. |
CACLS | Display/edit access control lists (ACLs) for files. |
CALL | Calling one batch file from another. |
CD | |
CHCP | Display or set the active code page. |
CHDIR | Display the name or change the current folder. |
CHKDSK | Checking the disk and displaying statistics. |
CHKNTFS | Show or change whether disk check is performed during boot. |
CLS | Cleaning the screen. |
CMD | Launching another Windows command line interpreter. |
COLOR | Set the default text and background color. |
COMP | Comparing the contents of two files or two sets of files. |
COMPACT | Display/change file compression on NTFS partitions. |
CONVERT | Convert FAT disk volumes to NTFS. The currently active drive cannot be converted. |
COPY | Copy one or more files to another location. |
DATE | Display or set the current date. |
DEL | |
DIR | List files and subfolders from a specified folder. |
DISKCOMP | Comparing the contents of two floppy disks. |
DISKCOPY | Copying the contents of one floppy disk to another. |
DOSKEY | Editing and re-calling command lines; creating macros. |
ECHO | Display messages and switch the display mode of commands on the screen. |
ENDLOCAL | End of local environment changes for batch file. |
ERASE | Deleting one or more files. |
EXIT | Exiting the CMD.EXE (command line interpreter) program. |
F.C. | Compare two files or two sets of files and display the differences between them. |
FIND | Search for a text string in one or more files. |
FINDSTR | Searching for strings in files. |
FOR | Run the specified command for each of the files in the set. |
FORMAT | Formatting a disk for use with Windows. |
FTYPE | Display or change the file types used when matching by file name extensions. |
GOTO | Transfer control to the marked line of the batch file. |
GRAFTABL | Allows Windows to display extended characters in graphics mode. |
HELP | Displays help information about Windows commands. |
IF | An operator for conditionally executing commands in a batch file. |
LABEL | Create, change and delete volume labels for disks. |
M.D. | Creating a folder. |
MKDIR | Creating a folder. |
MODE | Configuring system devices. |
MORE | Sequential output of data in parts the size of one screen. |
MOVE | Move one or more files from one folder to another. |
PATH | Display or set the search path for executable files. |
PAUSE | Pauses the batch file and displays a message. |
POPD | Restores the previous value of the current active folder saved using the PUSHD command. |
Printing the contents of text files. | |
PROMPT | Changing the prompt in the Windows command line. |
PUSHD | Saves the current active folder and moves to another folder. |
R.D. | Deleting a folder. |
RECOVER | Recovering readable information from a bad or damaged disk. |
R.E.M. | Place comments in batch files and CONFIG.SYS file. |
REN | |
RENAME | Renaming files and folders. |
REPLACE | File replacement. |
RMDIR | Deleting a folder. |
SET | Listing, setting and removing Windows environment variables. |
SETLOCAL | Start local environment changes for the batch file. |
SHIFT | Changing the contents (shift) of substituted parameters for a batch file. |
SORT | Sorting input. |
START | Run a program or command in a separate window. |
SUBST | Matches the given path to a drive name. |
TIME | Displaying and setting the system time. |
TITLE | Sets the window title for the current session of the CMD.EXE command line interpreter. |
TREE | Graphic display of the folder structure of a specified drive or specified folder. |
TYPE | Displaying the contents of text files. |
VER | Displays information about the Windows version. |
VERIFY | Setting the mode for checking the correctness of writing files to disk. |
VOL | Displays the volume label and serial number for the disk. |
XCOPY | Copying files and folder trees. |
Command line commands.
Now let’s talk in more detail about the most frequently used commands, but first I’ll tell you a little about the keys that can be used together with commands for a more functional action. For example, let's take the RD command (remove directory), if it is applied to an empty folder, it will be deleted, but if there are files in the folder, then nothing will happen. So, to delete a folder along with the files in it, you need to use the key together with the RD command /s.
That is, for example, there is a directory kat and to find any files in it, in order to delete the directory along with the files, you need to register RD/s cat. A list of keys to any command can be found by typing the command name followed by a slash and the question: command_name/?.
Basic commands for working with directories:
- MD - Directory creation. Syntax: (MD directory name, it is possible to specify the location path).
- RD - Removing a directory. Syntax: (RD directory name, it is possible to specify the location path). Note, RD without keys only allows you to delete an empty directory.
- CD - Change the current directory. Syntax: (CD directory name, it is possible to specify the location path). CD\ - Go to the root directory. CD.. - Go to the parent directory.
- DIR - View directory as a list. Syntax: (DIR directory name, it is possible to specify the location path).
- TREE - Display directories in graphical representation. Syntax: (TREE directory name, it is possible to specify the location path).
- MOVE - Move\Rename directory. Syntax: .
- XCOPY - Copy directory structures. Syntax: (XCOPY what_we copy where_we copy, it is possible to specify the location path).
Commands for working with files:
- COPY CON - Create a file. Syntax: (COPY CON file_name_with_extension, it is possible to specify the location path).
- TYPE — Displays the contents of the file on the screen. Syntax: (TYPE file_name_with_extension, it is possible to specify the location path).
- DEL - Delete a file. Syntax: (DEL file_name_with_extension, it is possible to specify the location path).
- COPY - Copying a file\Combining files. Syntax(copy): (COPY what_we copy where_we copy, it is possible to specify the location path). Syntax(merging): (COPY file_name + file_name + file_name... merge_file_name, it is possible to specify the location path).
- EDIT - Create\Edit a file. Syntax: (EDIT file name, it is possible to specify the location path).
- MOVE - Move\Rename a file. Syntax: (MOVE what_we move where_we move, it is possible to specify the location path).
- REN - Rename files. Syntax: (REN what_renaming what_renaming, it is possible to specify the location path).
- FC - File comparison. (FC file_name file_name file_name..., it is possible to specify the location path).
System commands:
- CLS - Screen cleaning.
- DATE - View and display changes to the current date.
- VER — Displays information about the current version of the Operating System.
- VOL - Displays information about the volume-logical partition of the disk.
- SYSTEMINFO - Display information about system configurations.
- EXIT - Exits the command line.
These were the most used command line commands.
This is where I end this article, I hope you have fully understood the topics: CMD Commands, Command Line Commands, Command Prompt.
If you ever need to get a list of all files, then you will find the most unpleasant news. The Windows Explorer interface doesn't allow you to do this in a simple and clear way. And the task seems to be incredibly simple, and all the necessary files seem to be in front of your eyes, but... However, this can be done very simply using the command line (cmd). Additionally, the command line will allow you to filter only the necessary files, for example only mp3s. In this case we will use the special command "dir". Most likely, experienced users are familiar with this command, so the article will not be interesting to them. But for most ordinary users it will be very useful.
Note: you can also use various catalogers and organizers, but they require the installation of additional applications.
If you think that this will never be useful to you in your life, then you are mistaken. Almost every user implicitly uses file lists every day. This includes searching for the required document among all your documents. This is music. These are photographs. These are various file exports in your applications. Trite, same playlist. It’s just that you’ve never encountered such simple tasks as “send a list of files, for example, to technical support”, “collectively select the music you need from the entire list” (and this is usually done on paper, and there are a huge number of “fashionable devices”, but they will never replace such simple things as paper and pencil), “upload a list into a new application” (not all applications have high-quality import), “using the same console applications for technical needs” (for example, various batch processing of files; of course, the average user will never do this, but...) and much, much more.
Display a list of files in a folder and its subfolders on the command line screen
- Open a command prompt in the desired folder ()
- Type "dir" (without quotes) and press Enter. You will receive a list of files (see picture)
- If you need to list files in all subfolders, then instead of "dir" use "dir /s" (type without quotes)
The resulting lists can be quite long, especially if the list is created for some large directories. Such lists will not fit on the screen, and it will not be very convenient to work with them. Fortunately, you can very easily save the entire resulting list into a separate text file.
Create a text file with a list of files on the command line
- Open a command prompt in the desired folder.
- Type "dir > listmyfolder.txt" (without quotes) and press Enter. The entire list will be saved in the file "listmyfolder.txt".
- If you need to also save files from all subdirectories, then use the command "dir /s > listmyfolder.txt" (without quotes)
The file "listmyfolder.txt" will be created in the same folder you are in. A small clarification about the file, you can call it whatever you like, give it absolutely any extension. There are no special restrictions, except for those that the Windows system requires for file names. You can also use full paths to save the list in another directory. For example, you want to save a file on the "F:" drive in the "list" directory. To do this, you will need to use the following command: "dir > F:\list\listmyfolder.txt" (without quotes). As you can see, there is nothing complicated.
Create a text file with a list of files of a specific extension on the command line
You don't always need to get all the files in a directory. For example, you want to get only a list of photos or only music. Agree that manually changing the complete list of files is a boring and routine task. To make life easier for everyone, the command line supports a special character *, with which you can create so-called regular expressions (sometimes called a mask). Don’t be afraid of scary words; you won’t come across any complex constructions in the “/d+?[\.2345]?d+[\.]?mp3/$” variant. Next we will give simple examples of how you can get lists of all your mp3 and jpg files, i.e. music and photos.
Getting all mp3 files in a folder and all subfolders from the command line
- dir /s *.mp3 > listmp3.txt
The file "listmp3.txt" will contain a list of all your MP3 files.
Getting all JPEG photos in a folder and all subfolders from the command line
The command now looks like this:
- dir /s *.jpg > listjpg.txt
The file "listjpg.txt" will contain a list of all your photos in JPEG format.
Getting a simple list format from the command line
The above commands will generate lists that will additionally include information about the files, namely the size and creation date. But such information is not always necessary. Sometimes we just need a simple list of files. To generate such a list, you need to add the special parameter "/b" to the command. As a result, the command will look like this:
- dir /s /b *.jpg > listjpg.txt
If you're interested, there are many other useful commands. To find out the full list of commands, you need to enter the command "help". Of course, a short description of the command is unlikely to help you imagine how the command can be used. That's why there is a special parameter "/?". If you enter it immediately after the command, a small manual on using the command and its parameters will appear in the console window.
Warning: If you are not very versed in what exactly the command does, then we strongly recommend that you first read about its use, at least in a search engine. Careless and incorrect use of commands can lead to unpleasant consequences. Of course, you can use disk images before tests, but you must agree that this is very expensive.
- How to open Command Prompt in Windows 7/Vista as Administrator?
Technical Tips
In Windows 7, there are operations that are impossible or difficult to perform through a regular graphical interface, but they can actually be carried out through the Command Line interface using the CMD.EXE interpreter. Let's look at the basic commands that users can use when using this tool.
Using commands in the Command Prompt, you launch various utilities and perform certain operations. Often the main command expression is used together with a number of attributes, which are written separated by a slash ( / ). It is these attributes that initiate the execution of specific operations.
We do not set ourselves the goal of describing absolutely all the commands used when using the CMD.EXE tool. To do this, I would have to write more than one article. We will try to fit information about the most useful and popular command expressions on one page, dividing them into groups.
Launching system utilities
First of all, let's look at the expressions that are responsible for launching important system utilities.
CHKDSK– launches the Check Disk utility, which performs . This command expression can be entered with additional attributes, which, in turn, trigger the execution of certain operations:
- /f– disk recovery in case of detection of logical errors;
- /r– restoration of drive sectors in case of detection of physical damage;
- /x– disconnecting the specified hard drive;
- /scan– proactive scanning;
- C:, D:, E: …— specifying logical drives for scanning;
- /? – call up help about the operation of the Check Disk utility.
SFC– launch the utility. This command expression is most often used with the attribute /scannow. It runs a tool that checks OS files for compliance with standards. In case of damage, if you have an installation disk, it is possible to restore the integrity of system objects.
Working with files and folders
The next group of expressions is designed to work with files and folders.
APPEND– opening files in a user-specified folder as if they were in the required directory. A prerequisite is to indicate the path to the folder to which the action will be applied. The recording is made according to the following template:
append [;] [[computer drive:]path[;...]]
When using this command, you can apply the following attributes:
- /e– recording a complete list of files;
- /? – launch help.
ATTRIB– the command is intended to change the attributes of files or folders. Just as in the previous case, a prerequisite is to enter, along with a command expression, the full path to the object being processed. The following keys are used to set attributes:
- h– hidden;
- s– systemic;
- r– read only;
- a– archival.
In order to apply or disable an attribute, the sign is placed in front of the key accordingly «+» or «-» .
COPY– used to copy files and directories from one directory to another. When using the command, be sure to indicate the full path of the copy object and the folder into which it will be performed. The following attributes can be used with this command expression:
- /v– checking the correctness of copying;
- /z– copying objects from the network;
- /y– rewriting the final object if the names match without confirmation;
- /? – activation of help.
DEL– deleting files from the specified directory. The command expression allows for the use of a number of attributes:
- /p– enabling a request to confirm deletion before manipulating each object;
- /q– disable the request when deleting;
- /s– deleting objects in directories and subdirectories;
- /a:— deleting objects with the specified attributes, which are assigned using the same keys as when using the command ATTRIB.
R.D.– is analogous to the previous command expression, but deletes not files, but folders in the specified directory. When used, the same attributes can be applied.
DIR– displays a list of all subdirectories and files that are located in the specified directory. The following attributes are used along with the main expression:
- /q– obtaining information about the owner of the file;
- /s– displaying a list of files from the specified directory;
- /w– displaying a list in several columns;
- /o– sorting the list of displayed objects ( e– by extension; n- by name; d– by date; s- by size);
- /d– display a list in several columns with sorting by these columns;
- /b– display only file names;
- /a– displays objects with certain attributes, which are specified using the same keys as when using the ATTRIB command.
REN– used to rename directories and files. The arguments to this command include the path to the object and its new name. For example, to rename the file file.txt, which is located in the folder "Folder" located in the root directory of the disk D, in the file file2.txt, you need to enter the following expression:
REN D:\folder\file.txt file2.txt
M.D.– is intended for creating a new folder. In the command syntax, you must specify the drive on which the new directory will be located, and the directory where it will be located if it is nested. For example, to create a directory folderN, which is located in the directory folder on disk E, you should enter the following expression:
md E:\folder\folderN
Working with text files
The next block of commands is designed to work with text.
TYPE– displays the contents of text files on the screen. The required arguments to this command are the full path to the object whose text should be viewed. For example, to view the contents of the file file.txt located in the folder "Folder" on disk D, you need to enter the following command expression:
TYPE D:\folder\file.txt
PRINT– prints the contents of a text file. The syntax of this command is similar to the previous one, but instead of displaying text on the screen, it is printed.
FIND– searches for a text string in files. Along with this command, the path to the object in which the search is performed must be indicated, as well as the name of the searched string, enclosed in quotation marks. In addition, the following attributes apply with this expression:
- /c– displays the total number of lines containing the searched expression;
- /v– output lines that do not contain the search expression;
- /I– case insensitive search.
Working with accounts
Using the command line, you can view and manage information about system users.
FINGER– displaying information about users registered in the operating system. The required argument of this command is the name of the user about whom you want to obtain data. Alternatively, you can use the attribute /i. In this case, the information will be displayed in a list format.
TSCON– attaches a user session to a terminal session. When using this command, you must specify the session ID or its name, as well as the password of the user to whom it belongs. The password should be specified after the attribute /PASSWORD.
Working with Processes
The next block of commands is designed to control processes on the computer.
QPROCESS– providing data about running processes on a PC. The information displayed will include the name of the process, the name of the user who launched it, the session name, ID and PID.
TASKKILL– used to terminate processes. The required argument is the name of the element that needs to be stopped. It is indicated after the attribute /IM. You can also terminate not by name, but by process ID. In this case the attribute is used /PID.
Networking
Using the command line, it is possible to manage various network activities.
GETMAC– starts displaying the MAC address of the network card connected to the computer. If there are several adapters, all their addresses are displayed.
NETSH– initiates the launch of the utility of the same name, which is used to display information about network parameters and change them. This command, due to its very broad functionality, has a huge number of attributes, each of which is responsible for performing a specific task. To obtain detailed information about them, you can use Help using the following command expression:
NETSTAT– displaying statistical information about network connections.
Other commands
There are also a number of other command expressions used when using CMD.EXE that cannot be separated into separate groups.
TIME– viewing and setting the PC system time. When you enter this command expression, the current time is displayed on the screen, which can be changed to any other time in the very bottom line.
DATE– the syntax command is completely similar to the previous one, but is used not to display and change the time, but to launch these procedures in relation to the date.
SHUTDOWN– turns off the computer. This expression can be used both locally and remotely.
BREAK– disable or start the button combination processing mode Ctrl+C.
ECHO– displays text messages and is used to switch their display modes.
This is not a complete list of all commands that are used when using the CMD.EXE interface. Nevertheless, we tried to reveal the names, as well as briefly describe the syntax and main functions of the most popular ones, for convenience, dividing them into groups by purpose.
The command line is a software tool for the user to enter commands and receive the results of their execution on the screen. In modern operating systems of the Windows family, the command line is provided by a standard application cmd.exe, also called the command processor, command interpreter, and console. The Command Line application provides the user with a text-based interface for entering commands and obtaining the results of their execution. In fact, the command line is a software emulator of the classic console of the first computer systems, which is a terminal with a keyboard used by the operator as a means of communicating with the computer. As in the days of the first computers, the command line supports the standard input device, the keyboard, and the standard output device, the display. The user enters commands from the keyboard and receives the results of their execution on the display screen.
Launch the command line.
To launch the command line, you can use one of the following methods:
Start - Run (or Win+R keys) enter cmd and press the Enter key;
Start - All Programs - Accessories - Command Prompt";
Start - Search - Command Line . Also, to launch the command line, you can use a pre-prepared shortcut that links to the executable file %SystemRoot%\system32\cmd.exe(usually C:\Windows\system32\cmd.exe). Executing some commands requires elevated privileges, therefore, the command line application must be launched using the right-click “Run as administrator” context menu item.
Command line setup.
The standard command prompt window is a window with white characters on a black background. You can change font settings, character color and background, clipboard usage, and other command line properties to suit your preferences. Settings can be made by changing the properties of the shortcut that launches the command line application, or by editing registry settings related to the command processor.Working on the command line involves entering data from the keyboard, and sometimes quite a lot of it. This volume can be significantly reduced using some techniques:
Using the clipboard.
Text selected in the application window can be copied to the clipboard, as well as pasted from the clipboard into the input field. With standard command line settings, to select text, use the “Mark” item in the right-click context menu. If desired, in the properties of the shortcut used to launch, you can enable mouse selection mode:
In mouse selection mode, the context menu is not used, and text is selected using the left mouse button. Copying and pasting is performed by clicking the right mouse button. When “Allow keyboard shortcuts with CONTROL” is enabled, you can use standard keyboard shortcuts:
CTRL+C (CTRL+Insert) – copy the selected text.
CTRL+V (Shift+Insert) – paste selected text.
Using command history and hotkeys.
To recall previously entered commands, use the arrow keys Up Arrow - one command back and Down Arrow - one command forward. In addition, you can use function keys:
F1- character-by-character call of the last entered command. Each press of F1 results in sequential substitution of one character from the previous command into the input field.
F2- copying to character. After pressing F2, the screen displays a request for the character to which the previous command will be copied. For example, if the previous command was ping yandex.ru, and a dot is specified as a symbol, then the command input line will contain ping yandex, if a space is given, then - ping.
F3- call the previous command.
F4- delete up to the character. Deletes text from the current cursor position to the specified character.
F5 And F8- calling a buffer of previously entered commands. Output by pressing the F5 key stops when the first entered command of the current session is displayed.
F7- display of previously entered commands in a separate window in the form of a list. To execute the desired command, select it using the arrow keys and press ENTER
F9- execute the command whose number is requested. The command number in the history list can be obtained by using F7.
Windows 10/Windows Server 2016 introduces features not found in previous command line implementations:
Changing the transparency of the console window when pressing combinations CTRL+Shift+- or CTRL+Shift++ .
Enable/disable full screen mode when clicked CTRL+Enter.
Expanded text selection and editing capabilities:
Shift+Home– select text from the current cursor position to the beginning of the line.
Shift+End– select text from the current cursor position to the end of the line.
Quickly move through the screen buffer and search by context:
CTRL+Home– go to the beginning of the screen buffer
CTRL+End– go to the end of the screen buffer.
CTRL+Up Arrow- move 1 line up.
CTRL+Down Arrow- move 1 line down.
CTRL+F- open the text search dialog in the screen buffer.
The command prompt window in Windows 10 can be closed using the standard keyboard shortcut - ALT+F4.
To disable new CMD features, you need to check the “Use previous version of the console (restart required)” checkbox in the application properties.
File path completion
In the Windows command line, you can perform file and directory name substitution when you press a key Tab. For example, to go to a directory with a long name Program Files type the initial part of the directory name
CD Pro and press Tab. The command should appear in the input line:
CD "Program Files"
As you can see, the missing part of the directory name is substituted, and in cases where it contains a space character, double quotes are also added.
If there are multiple matching initial parts of file or directory names, each time you press the key Tab The next name in alphabetical order will be substituted. To return to the previous one, use the combination Shift+Tab
Redirect console standard input/output data.
As already mentioned, the standard command line input device is the keyboard, and the display is used as the output device. However, there is a possibility redirects I/O to other devices using redirection operators:
ping –n 5 localhost > nul- ping the loopback interface 5 times with output redirected to a dummy device nul. The output of the command execution results is suppressed. A similar technique is used to organize a delay in command files, since the ping of the loopback interface is performed almost instantly, and the interval between pings is one second, the execution time of this command is determined by the value of the parameter -n
ping –n 100 yandex.ru > C:\ping-ya.txt- ping the yandex.ru node 100 times, writing the results of the command to the file C:\ping-ya.txt. If the file does not exist, it will be created, and if it exists, its contents will be overwritten.
ping –n 100 yandex.ru >> C:\ping-ya.txt- the same as in the previous example, but if the file does not exist, then it will be created, and if it exists, then the results will be written to the end of the file.
cmd - launch the CMD command processor and enter data from the 1.txt file. If you put the line in the file ping –n 100 yandex.ru, then the command discussed above will be executed.
Often, the output of one command needs to be passed as input to another, i.e. combine commands into a sequential chain:
ping -n 100 microsoft.com | find "Interval exceeded"- result of command execution ping -n 100 microsoft.com passed as input to the string search command ( find) containing the text "Interval exceeded".
ping -n 100 microsoft.com | find "Interval exceeded" > C:\ping-ya.txt- the same as in the previous example, but with redirection of the output results of the command to a text file.
Each open file or device has its own descriptor (handle) which is a non-negative number whose value is used by the process that spawned the I/O thread. By default, for all processes, including the command interpreter cmd.exe :
0 (STDIN) – standard input descriptor (keyboard input).
1 (STDOUT) – standard output descriptor (output to screen).
2 (STDERR) – descriptor for displaying diagnostic messages (error messages on the screen).
Descriptors can be used in cases where you need to redirect (change) data sources and sinks in standard input/output streams. For example:
ping.exe –n 100 yandex.ru 2> C:\pinglog.txt- standard program message flow ping.exe will be printed to the screen and errors (stdout with handle = 2) will be written to a file C:\pinglog.txt. In the real case for the program ping.exe The above construction is not significant since it displays both diagnostics and results on the screen.
To specify redirection to existing handles, use an ampersand (&) followed by the number of the desired handle (for example, &1):
ping –n 100 yandex.ru >log.txt 2>&1- standard error messages (handle=2) are redirected to standard output (handle = 1) and the whole thing is redirected to a file log.txt current directory.
ping –n 100 yandex.ru >log.txt 1>&2- standard output (handle = 1) is redirected to error message output (handle = 2) and all this is written to a text file.
If the handle is not defined, then the input redirection operator defaults to will be zero (0), and the output redirection operator > will be one.
Chaining multiple commands
In the Windows command line, it is possible to execute several commands sequentially, depending on the results of their execution. What are command concatenation symbols used for - & (ampersand) and | (vertical bar)
& - A single ampersand is used to separate multiple commands on the same command line. For example:
echo ping ya.ru- display a line on the screen ping ya.ru
echo &ping ya.ru- sequential command execution echo without parameters and command ping ya.ru
&& - conditional execution of the second command. It will be executed if the exit code (the value of which is passed to the standard variable ERRORLEVEL) of the first command is zero, i.e. the command completed successfully.
team1 && team2- executed team1, A team2 executed only if the first one was executed successfully. For example:
ping ya.ru -err & ping –n 2 ya.ru- when using a single ampersand, the first command ping ya.ru –err -err, and the second, ping –n 2 ya.ru will ping the node twice ya.ru
ping ya.ru -err && ping –n 2 ya.ru- when using a double ampersand, the first command ping ya.ru –err will display a message about an invalid parameter -err and, accordingly, will generate a termination code not equal to zero (error), which will lead to the fact that the second command ( ping ya.ru -n 2) will not be executed.
Double vertical bar || - conditional execution of the second command. If the first command returns with a non-zero return code (failure), then the command following the double vertical bar is executed.
team1 || team2- If team1 executed unsuccessfully, then it is started for execution team2
ping –n 1 ya.ru && ping –n2 ya.ru || ping –n 3 ya.ru- the 1st command will be executed first ping ya.ru –n 1- single node ping ya.ru, then the command will be executed ping ya.ru -n 2- double ping of the node ya.ru. Third team ping ya.ru -n 3, ping the node three times ya.ru will not be executed.
ping –n 1 –err ya.ru && ping ya.ru -n 2 || ping ya.ru -n 3- first team ping ya.ru –n 1 –err will fail due to an invalid parameter -err, second team ping –n 2 ya.ru- double ping of the node ya.ru will fail due to the double ampersand condition, and will result in the third command being executed ping –n 3 ya.ru, ping the node three times ya.ru.
In some cases, it may be necessary to prohibit the processing of service characters and treat them as plain text. For example, if you enter on the command line
ECHO
Then instead of text ping -n 1 ya.ru & ping -n 2 ya.ru the part up to the ampersand character will be displayed ping -n 1 ya.ru and then the command after the ampersand is executed - ping -n 2 ya.ru. The problem is solved by using a special escape character ^ , which allows you to use service characters as text:
ECHO ping -n 1 ya.ru ^& ping -n 2 ya.ru- text display ping -n 1 ya.ru & ping -n 2 ya.ru
Conditional command processing logic implemented using constructs && And || affects only the nearest command, that is, when entering a command
Team COPY /? will run in any case, regardless of the result of the command TYPE C:\plan.txt. However, several commands can be grouped using parentheses. For example, there are 2 command lines:
TYPE C:\plan.txt && DIR & COPY /?
TYPE C:\plan.txt && (DIR & COPY /?)
In the first of them, the conditional processing symbol && acts only on the DIR command, in the second - on two commands simultaneously: DIR and COPY. As a visual experiment, try running the second command under both file present and file absent conditions C:\plan.txt. To create an empty file, you can use copying from a dummy device nul:
copy nul C:\plan.txt
To delete a file use the command erase c:\plan.txt or del C:\plan.txt
Batch files
Batch files (scripts) are ordinary text files with a pre-prepared set of commands for execution by the command processor cmd.exe. As a standard, such files have the extension .bat or .cmd. Lines of command files can contain specific commands of the command processor itself, for example - FOR, ECHO, REM, etc. or the names of executable modules – reg.exe, sc.exe, auditpol.exe., which can be used without the extension – reg, sc, auditpol. Example of a simple batch file:
REM Creates a text file listing the Windows directory
dir C:\Windows > %TEMP%\winlist.txt
REM delay for 5 seconds
ping -n 5 localhost > nul
REM File opens in WordPad editor
write %TEMP%\winlist.txt
REM After Wordpad finishes running, the text file is deleted.
erase C:\winlist.txt
Lines starting with REM are comments. As an example, commands are used to work with the file system and a graphical environment application is launched - the Wordpad text editor (write.exe) passing it a command line parameter (file name). The command file language is quite primitive and does not fully meet the requirements of today, however, it is the simplest means of automating routine actions and is used by the majority of system administrators and literate users. Working with batch files is a separate topic, described in more detail on the page
Programming in Cmd (introduction for beginners)
This is just an introduction to cmd commands, don’t judge strictly, the article was written for Novikov, I think it will be useful for many others =)
Probably many people don’t even suspect that cmd is a universal shell,
which allows you not only to administer the system, but also to program!
Of course, the level of the language is not as high as, for example, with, but still...
To begin with, let's write a small program that will output a line written to a variable.
We go to the C:\ drive and create something like program_1.cmd there (*.bat is possible)
open it with notepad and enter the following lines there:
rem program_1
cls
@echo off
color 0A
set string=Hello World!!!
echo %string%
rem - used to describe comments
cls - if anyone else doesn’t know, this is screen clearing
@echo off - turn off the display of commands on the screen. Pay attention to @
before ECHO! It prevents commands from being echoed on a separate line.
color 0A - set the background and text color
set string=Hello World!!! - set is used to describe variables
echo %string% - displays a variable, pay attention to %%, this
denotes that it is indeed a variable and not a string!
We've sorted out the code, now let's look at the outcome of the program! We launch it for execution via cmd:
program_1.cmd
The execution result will be, you guessed it, Hellow World!!! Bright green letters on a black background
Now let’s complicate the problem a little, add a condition to this code!
rem program_2
cls
@echo off
color 0A
set a=1
set timer=10
if %a%==1 (shutdown /r -t %timer% -c "You got screwed"
) else (echo a!=1)
I think you've already guessed what the output will be. We compare the value written to the variable “a” with 1, if the condition is true, then the computer will reboot in 10 seconds, otherwise the output will read that a!=1. The example is banal and simple,
Well, at least it’s understandable for many.
Now let's move on to cycles. Let's look at the most basic example:
rem program_3
cls
@echo off
color 0A
for /l %B in (0,1,10) do echo %B
This program will display numbers in a column, from 0 to 10 in increments of 1 =) everything is as simple as...
And now a little more complicated, for can be used for more significant purposes,
for example, to enumerate files, for example you can write in cmd:
for %B in (C:*.cmd) do (echo %B)
In return you will receive all your programs with the extension *.cmd
You can also use a for loop to iterate through directories, for example
for /d %B in (C:*) do echo %B
Will show all directories on the C:\ drive (Please note, only directories, not subdirectories!)
Now let’s write an essential program that will select emails from the database and write them to another file.
(for /f "tokens=1-5" %A in (users.txt) do @echo %D) > emails.txt
For example, our base is “ideal”, like:
Vasya Pupkin heck [email protected] 81231111111
Dima Ivanov lamer [email protected] 81232222222
Genya Petrov lamer [email protected] 81233333333
Egor Sidorov lamer [email protected] 81234444444
Anton Popovich lamer [email protected] 81235555555
Kostya Terentev lamer [email protected] 81236666666
Vanya Onisenko lamer [email protected] 81237777777
Misha Loshenko lamer [email protected] 81238888888
Vlad Sorochinskiy lamer [email protected] 81239999999
Using a loop, we go through all the words and write 4(%D) to the file emails.txt =)
What else can you do with cmd?
I'll describe my example. I'm creating websites that use a mysql database. I work with the database through the console, which is more familiar to me. So as not to worry, I wrote a small program mysql.cmd that will connect to it:
PHP code:
v:
cd usrlocalmysql4bin
mysql.exe -u root –p pass
v – virtual disk on which Denver is located
root – username
pass – user password
All we have to do is launch it and enjoy working with the database
PS Well, that's all! The article turned out to be short, but I still hope that it will be useful to someone =)