Use batch files - common commands

xiaoxiao2021-03-06  106

Echo, @, call, pause, REM is the most common command of the batch file, we start learning from them. Echo indicates that the character echo off after this command indicates that all run commands after this statement does not display the command line itself @ and the echo offline, but it is the front of the other command line, indicating that the runtime is not displayed The line itself. CALL calls another batch file (if you call other batch files directly, the current file will not be executed after that file will not be able to execute the current file) PAUSE run this sentence will be paused, showing the press any key to continue ... Waiting for the user to press any After the key, continue REM indicates that the characters after this command are explained, not executed, just find it in the future.

Example: Edit a.bat file with Edit, enter the following contents After the store is c: /a.bat, you can implement it after executing the batch file: write all the files in the root directory to a.txt, start UCDOS, enter WPS Waiting for functions.

The content of the batch file is: file representation:

Echo off does not display the command line

Dir c: /*.*> a.txt writes the C disk file list to A.txt

Call C: /ucdos/ucdos.bat call UCDOS

echo Hello, "Hello"

Pause pause, wait for the button to continue

REM uses WPS annotations to use WPS

CD UCDOS enters UCDOS directory

WPS uses WPS

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 a batch of processing files in the root directory, the content 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

- Special order

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

: End echo this is the end Number: String Representation, the row in the label is not executed with this command allows the user 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]?

For example, Test.bat is as follows: @echo off choice / c: DME Defrag, MEM, END IF Errorlevel 3 Goto Defrag should first judge the highest value of error code if Errorlevel 2 Goto Mem if Errotlevel 1 Goto End

: Defrag C: / DOS / Defrag Goto End

: 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. 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-123118.html

New Post(0)