bat script knowledge summary

A commonly used basic commands

1.1 @

Its role is to execute the command window does not show it behind the line itself

1.2 echo

It is actually a switch command, that it has only two states: on and off. Then there is the echo on and echo off two commands. Direct implementation of echo echo command displays the current status (off or on) performing echo closes echo off

1.3 ::

It is a comment commands, and rem equivalent command in a batch script.

1.4 pause

Its role is to allow the process to pause the current program, and displays one line of information: Press any key to continue ..

1.5: sum goto

goto is a jump command,: is a label. When the program runs to goto, will automatically jump to: define the portion to perform
as:

……
goto end
……
:end 

1.6 %

The percent sign is not really strict orders, it is only in the batch parameters only, Without it batch function is reduced to 51% of

1.7 if

determining if the command is a command showing, according to each of the results obtained

Input judgment

Such as:

if "%1"=="" goto usage 
if not "%1"=="" goto usage

There is judgment

Such as:

if exist C:\Progra~1\Tencent\AD\*.gif del C:\Progra~1\Tencent\AD\*.gif 
if not exist C:\Progra~1\Tencent\AD\*.gif exit

Results to determine

Such as:

masm %1.asm
if errorlevel 1 pause & edit %1.asm
link %1.obj 

“if not errorlevel 1”And “if errorlevel 0”the effect is equivalent to

Generally execution result of a command value errorlevel returned only two, "success" 0 indicates "failure" as used with a said, in fact, errorlevel return value can be between 0 to 255

Compare all the parameters are as follows:

EQU- equals
NEQ- not equal to
LSS- less than
LEQ- equal to or less than
GTR- greater than
GEQ- greater than or equal to

1.8 call

call command to call another batch script from a batch script

Script calls can be flexible use, recycling use, repeated use

Scripts can be invoked using the parameters
call a script with parameters are:

start.bat:
……
CALL 10.BAT 0
……

10.bat:
……
ECHO %IPA%.%1 >HFIND.TMP
……
CALL ipc.bat IPCFind.txt

ipc.bat:
for /f "tokens=1,2,3 delims= " %%i in (%1) do call HACK.bat %%i %%j %%k 

The 10.bat Lane 1 was replaced with the parameters 0%. In the start.bat, ipc.bat followed by the parameters ipcfind.txt (a file, you can do parameters), when the effect of the implementation is to use three variables in each row ipc.bat corresponding substitution ipc. in bat %% i, %% j and %% k.

1.9 find

This is a search command to search for a specific string in the file

@echo off
netstat -a -n > a.txt
type a.txt | find "7626" && echo "Congratulations! You have infected GLACIER!"
del a.txt
pause & exit 

First with the netstat command to check whether there is ice default port 7626 at the event, and save the results to a.txt in. Then type the command to list the contents of a.txt, then the contents listed in the search string "7626", found any prompting in the ice, otherwise quit.

If you do not use the type command to list the contents of a.txt, but the use of the find command directly to find "7626" in a.txt in, you have to give an absolute path of this a.txt

1.10 for

Command parameters FOR %% variable name IN (relevant documents or command) DO executed

parameter

There are 4 parameters FOR / d / l / r / f

I explain their role in the following example

%% variable name: variable name can be uppercase or lowercase az AZ, they are case-sensitive, each read the values ​​FOR'll give him;

IN: format of the command, as it wants to write;

(Relevant documents or command): FOR should read something and then assigned to a variable, see example below
do: format of the command, according to just write!

Command execution: The value of each variable on what to do at this writing.

You can enter for the CMD /? To see the system provides help! Control what

/d Only directory

for /d %%i in (*) do @echo %%i 

He saved the root directory on the C execution, it will put all the names in the directory C drive directory print

/r Recursion

for /r c: %%i in (*.exe) do @echo %%i 

Save this BAT to the D drive and then do just where I would'll see, he subdirectory C root directory, and each directory of all EXE files are listed, where c: is the directory .

/L Iterative numerical range

Help formats: for / L %% Variable in (Start #, Step #, End #) do Command

for /l %%i in (1,1,5) do @echo %%i 

He would print such five digits from 12345

for /l %%i in (1,1,5) do start cmd

After the execution was not shocked, how much 5 CMD window

/f

Containing / F is for is very useful, the most used in the batch

FOR /F ["options"] %%i IN (file) DO command
FOR /F ["options"] %%i IN ("string") DO command
FOR /F ["options"] %%i IN (command) DO command

file on behalf of one or more files

string represents a string

command represents the command

If the file a.txt the following contents:

第1行第1列第1行第2列 第1行第3列
第2行第1列 第2行第2列 第2行第3列
第3行第1列 第3行第2列 第3行第3列

You want to display the contents of a.txt, what command it? Course type, type a.txt

for /f %%i in (a.txt) do echo %%i

Or start execution brackets, because it contains the parameters / f, so that for a.txt will first open, then Read all a.txt inside, it as a set, and in each row as an element, so that there occurs collection

{ "Row 1, column 1, line 1, column 2, column 3, line 1", the first element //
"row 2, column 1, line 2, column 2, line 2 of 3 '/ / second element
"row 3, column 1, line 3, column 2, line 3, column 3"} // third element

Set only three elements, each element similarly replaced with %% i sequentially, and then performs the latter do command.

Specific process:

"Row 1, column 1, line 1, column 2, line 1 of 3" as used %% i instead of performing the later do echo %% i, display "row 1, column 1, line 2, column 1 1, line 3 ",
with %% i instead of" row 2, column 1, line 2, column 2, line 2 column 3 ", perform echo %% i, display" row 2, column 1, line 2 column 2 column 3, line 2 ",
in turn, until each element in place last.

delims

delims used to tell for each line should be what means as a separator, the default separator is a space and tab key

for /f "delims= " %%i in (a.txt) do echo %%i

The results show that:

Row 1, column 1
row 1, column 2
, line 3, column 1

Why is this happen. With this parameter because there delims, = followed by a space, meaning that each element then separated by spaces, the default is the first element to take only after segmentation.
Implementation process are:

The first element "row 1, column 1, line 1, column 2, column 3, line 1" is divided into three elements: "row 1, column 1" "row 1, column 2" "Line 1 column 3 ", which takes only the first default, i.e.," row 1, column 1 ", and then execute the command later do, and so on.

But this is still a limited, if we want a second column elements of each row, so what?

tokens

Its role is when you each row is divided into smaller elements by delims, to take control of it to which one or a few.

Or the above example, the following command:

for /f "tokens=2 delims= " %%i in (a.txt) do echo %%i

Results of the:

Row 1, column 2
, line 2, column 2
, line 3, column 2

If you want to display the third column, it would be replaced by tokens = 3.

Meanwhile support wildcard * tokens, and to limit the scope.

To display the second and third columns, is replaced by tokens = 2,3 or tokens = 2-3, if there was more: tokens = 2-10 and the like.

At this time the command is:

for /f "tokens=2,3 delims= " %%i in (a.txt) do echo %%i %%j

How more than a %% j?

This is because the back of your tokens to take two each row, second column replaced with %% i, the third column is replaced with %% j.

And it must be arranged alphabetically, %% j can be replaced %% k, j i is the back because
the execution result is:

Row 1, column 2, column 3, line 1
, line 2, column 2, column 3, line 2
, line 3, column 2, line 3, column 3

For wildcard *, it is to put all of the remainder of the line or this line as an element of.
such as:

for /f "tokens=* delims= " %%i in (a.txt) do echo %%i

Implementation of the results:

Row 1, column 1, line 1, column 2, line 1, column 3
row 2, column 1, line 2, column 2, line 2, column 3
, line 3, column 1, line 3, column 2 3 line column 3

In fact, just for /f %%i in (a.txt) do echo %%ithe results are the same.
Another example:

for /f "tokens=2,* delims= " %%i in (a.txt) do echo %%i %%j

Implementation of the results:

Row 1, column 2, column 3, line 1
, line 2, column 2, column 3, line 2
, line 3, column 2, line 3, column 3
in place of the second row with %% i, replaced by %% j All remaining

2 Bat script compression and decompression

2.1 Compression

WinRAR.exe a -o+ -r -s -m1 -df ......\bak\backup.rar ..\base_db.sql

M represents movement command (after compression to delete the source file)
command represents a compressed added
command represents the creation of a self-extracting file s

Practical switch:

-rIncluding subfolders
-aiignore file attributes,
-clfile name lowercase
-cufile name uppercase
-edignore empty folder
-epignore the path information
-ep1to omit compressed root folder
-ep2contains the most complete path information (except drive)
-dfmeans to delete the source files after archiving
-drDelete files to the Recycle Bin
-oris automatically rename the file with the same name

2.2 Unzip

WinRAR x Unzip the archive path to the destination folder path

Unzip reserved path

WinRAR e Unzip the archive path to the destination folder path

Ignore decompression path

Ignore the path of meaning, that is, when decompressing compressed original package inside the folder, after decompression, can only get all the files and folders no more.

WinRAR.exe e backup.rar

In the compression and decompression process WinRAR.exe should be a full path compression software, I do not know the full path WinRAR.exe server, and a different server, WinRAR.exe whole Road King may not be the same. In order to ensure that no error path, can be copied to a WinRAR.exe the corresponding directory, compressed, will not use error WinRAR.exe replication.

3 Knowledge Summary

3.1 Call and start

Range of different calls

call the batch is mainly used for internal calls, such as call: call pend, and some dos commands such as call set test = 2, but also can call other executable files, but you can not start an internal call, but it can perform basic All external programs, you can also execute shell, such as opening the folder start "" "% WINDIR%", install a network printer start "" "\ IP \ printer" and so on.

Different ways to call

call is a call in the strict sense, and in another call when the batch is executed in the same form in the process, and the start is executed, so when another execution is carried out in a batch process in a different form, that the implementation of the new open process, although you can start adding b parameters, but the results are completely different.

If we use the call set test = 2 and start test results / b set = 2 appears to perform the same, but we found the latter two processes, but also in the form to be executed twice exit to exit, so when we use start to perform best in a batch after batch being called is also a plus exit, otherwise it can not be invoked to exit the dos batch form, but when using the call to call if there is exit the batch to be called in directly and end of the original batch program is called, this is a very serious problem. It recommends using goto in batch to be called in: eof instead of exit.

Different results call

call call not only to pass a parameter or variable batch is called, and is called batch parameters or variables can also be set to return, but only the start parameters or variables passed to the batch to be called, but can not return parameters or variables, which is actually an extension of the second point. In addition, we must also note that: Use call when calling other batch, when the batch is called in, if we use the goto command, it is recommended to use the original batch different tag name to jump, as this may Jump to the original batch and can not guarantee the full implementation of all the statements is called batch.

For the start, profiles two words: "different processes can not pass by value", "same one-way process of traditional values, I pass his son, son Chuan non-I."
  For the call, "the same process, variable interoperability." the most important feature is that you can call call tag in this installment, inherited his father's batch of environment variables, with sub-batch environment variable return, start can not.

3.2errorlevel

Errorlevel expressed on an execution state, the successful return 0, else return 1

if %errorlevel%==0Success is indicated

if %errorlevel%==1Represents execution failed

After general use and implementation of sql statement, bat script after executing sql statement, if it fails, and sometimes did not show failed, mistaken successful, will lead to error behind, plus errorlevel after the judgment, may well be treated

3.3 prompts the user

set /pPrompts the user
set /ato perform mathematical calculations

Such as set / pa = POP Wait Input: window is then prompts "Enter wait POP:" input after press Enter

@echo off
set /p b=输入一个数字:
echo %b%
set /a a=2*%b%
echo %b%+%b%=%a%
pause>nul

Set / p mainly used for user input in the sql statement, sometimes requires the user to enter IP, password, port number, etc., are common in practical work

Guess you like

Origin www.cnblogs.com/leton/p/11895960.html