Batch command learning (reference textbook)

xiaoxiao2021-03-06  21

Batch Command Learning (Reference Teaching Material) Batch file is a form of text file, which contains one or more commands. Its file extension is .bat or .cmd. At the command prompt, the name of the approval file is submitted, or double-click the batch file, the system will call CMD.exe to run them one by one according to the order of each command in this file.

1. Simple Batch Interior Command Introduction 1. Echo Command

Open the echo or close the request back function, or display the message. If there is no parameters, the echo command will display the current echo setting.

grammar

Echo [{on off}] [Message] Sample: @echo off / echo hello world

In practical applications we will combine this command and redirect symbol (also known as pipe symbols, usually >>) to implement files to enter some commands to specific formats. This will be embodied in later examples.

2. @ 令

Indicates that @ later commands, in the invasion process (for example, using batch to format the enemy's hard drive) naturally not let the other party see the command you use.

Sample: @echo off @echo now initializing the program ... ========== WGSCD =============== @format x: / Q / u / autooet (format is not available / y this parameter, gratifying that Microsoft has left an Autoset to us, the effects and / Y are the same.)

3. goto command

The specified jump to the label. After finding the label, the program will process the command starting from the next row.

grammar:

Goto Label (Label is a parameter, specifies the row in the batch program to be turned to the batch program.) Sample: if {% 1} == {} goto noparms if {% 2} == {} goto nopArms (if IF,% here 1,% 2 You don't understand, first jump over, there will be a detailed explanation.

The name of the label can be random, but it is best to make a meaningful letter, let me add one: used to indicate that this letter is a label, and the goto command is based on this: to find the next step to jump there. It is best to have some explanation that you will understand your intentions.

4. REM command

Comment command, quite with / * in the C language -------- * /, which does not be executed, just a role of a comment, easy to read and modify it later.

Rem message sample: @Rem Here is the description.?

????? 5. Pause command

When you run the PAUSE command, the following message is displayed:

Press any key to continche.. Sample: @echo off: begin copy a: *. * D: / back echo please put a new disk INTO DRIVER A PAUSE GOTO BEGIN In this example, all files on the disk in the drive A All copied to D: / back. Display Note Tips When another disk is placed in the drive A, the PAUSE command will hang the program so that you can replace the disk and press any key to continue processing.

6. Call command

Another batch program is called from a batch program and does not terminate the parent batch program. The call command accepts the label used as calling the target. If you use Call outside the script or batch file, it will not work on the command line.

grammar

Call [[Drive:] [PATH] FileName [BatchParameters]] [: label [arguments]]]]]

parameter

[Drive:} [PATH] FileName

Specifies the location and name of the batch program to be called. The filename parameter must have a .bat or .cmd extension.

7. START command

Call external programs, all DOS commands and command line programs can be called by the start command.

Intrusion common parameters:

MIN Start Window Minimize Separate Start 16 Bit Windows Program in Separated Space Time The High Pretty Category Start Application RealTime Launches the application and wait for the application to end Parameters These for transfer to command / Program parameters

When the execution application is a 32-bit GUI application, the cmd.exe does not equal the application to terminate the command prompt. If executed within the command script, the new behavior will not happen.

8. Choice command

Choice Use this command to allow users to enter a character to run different commands. When using, you should add / c: parameters, C: After you should write the prompt that the characters can be entered, there is no space. Its return code is 1234 ...

Such as: Choice / C: DME DEFRAG, MEM, END

Will display

DEFRAG, MEM, END [D, M, E]? Sample: sample.bat is as follows: @echo off choice / c: DME DEFRAG, MEM, END IF Errorlevel 3 Goto Defrag IF Errorlevel 2 Goto Mem if Errotlevel 1 goto end

: Defrag C: / DOS / Defrag Goto end: Mem Mem Mem Mem Mem Mem Goto End: End Echo Good Bye

After this file runs, the DEFRAG, MEM, END [D, M, E]? The user can select DME, then the IF statement will make a judgment, and D represents the block segment executing the label DEFRAG, and M represents the program that is labeled MEM. Segment, e represents a block that executes the label End, and each block is finally jumped to the END Number with Goto End, and then the program will display good Bye and the file ends.

Batch files can also be used as a parameter like a C language, which requires only one parameter representation%.

% Indicates the parameters, the parameter refers to the string after the file name after running the batch file. Variables can represent file names from% 0 to% 9,% 0, and character strings are represented by% 1 to% 9.

For example, the C: root directory next batch file name is f.bat, content is format% 1

If you do C: /> f A:, it actually executes Format A: Another example is the name of the root directory, the name of the process of processing files is Type% 1 TYPE% 2

So running C: /> t a.txt b.txt will display the contents of A.TXT and B.TXT files sequentially

If Goto Choice for is a relatively advanced command in a batch file. If you are very skilled, you are an expert in batch files.

The if indicates whether it is determined whether or not the specified condition is determined to determine the execution of different commands. There are three formats: 1, if "==" String "The command parameter to be executed is equal to the specified string, the condition is established, run the command, otherwise run the next sentence. (Note is two equal numbers) such as if "% 1" == "a" format A:

2, if the command to be executed, if there is a specified file, the condition is established, run the command, otherwise run the next sentence. If exist config.sys edit config.sys

3, if the command to be executed if the command to be executed If the return code is equal to the specified number, the condition is established, run the command, otherwise run the next sentence. If the IF Errorlevel 2 Goto X2 DOS program is running, a number will return to DOS, called error code Errorlevel or the return code GOTO batch file runs here will jump to the reference numeral specified by Goto, generally in conjunction with IF. Such as:

Goto end

: Endecho this is the end

Number use: string representation, the label is not executed

The for loop command, as long as the condition meets, it will execute the same command multiple times.

Format for [%% F] in (Collection) DO [Command] As long as the parameter f is within the specified collection, the condition is established, execute the command

If there is a line in a batch file: for %% C in (* .bat * .txt) Do Type %% C meaning is if it is the file ending with BAT or TXT, the contents of the file are displayed.

Autoexec.bat

DOS will automatically run the autoexec.bat in the startup, usually we load each required program, such as: Path, SmartDRV (Disk Acceleration), Mouse (Mouse Start), MSCDEX, Doskey (Keyboard Management), Set (Set Environment Variable), etc..

If this file does not have this file in the root directory, the computer will allow the user to enter the date and time.

For example, a typical autoexec.bat content is as follows:

@echo off does not display the command line

Prompt $ P $ g Set a directory prompt before prompt

PATH C: / DOS; C: /; C: / Windows; C: / Ucdos; C: / Tools Settings Path

LH C: /DOS/doskey.com Load Keyboard Management

LH c: / mouse/mouse.com loading mouse management

LH C: /DOS/smartdrv.exe loading disk accelerated management

LH C: / DOS / MSCDEX / S / D: MSCD000 / M: 12 / V Loading CD-ROM Drive

Set Temp = C: / TEMP Settings Temporary Directory

转载请注明原文地址:https://www.9cbs.com/read-46018.html

New Post(0)