Windows batch processing, windows batch processing tutorial
Note: The file name must be saved as an ASCII format and cannot contain Chinese characters.
Basic commands:
@ Echo off
Echo refers to the loop. echo off refers to the echo function. the previous @ indicates that the echooff line does not show back. You can try to remove @ and the whole line. another function of @ is to automatically restore the command echo when the batch processing file is executed.
Test:
@ Echo off
Ipconfig
Pause
Pause the execution of the batch processing program and display a message prompting you to press any key to continue execution.
Rem
Comment command
Call
Call Command
Example 1:
Write a test. bat file and place it on drive C.
@ Echo off
Echo % 1-Here % 1 is the input parameter, echo is the output display
Open cmd and enter call C: \ test. bat "dingxiaowei". "dingxiaowei" is displayed"
Example 2:
Change test. bat
Ping % 1
Ping % 2
Effect:
If
Judgment command
Three methods:
IF [NOT] string1 = string2 command
IF [NOT] EXIST filename command
IF [NOT] ERRORLEVEL number command
Example 1:
@ Echo off
If % 1 = 1 echo "a = 1"
Example 2:
Check whether a file exists
@ Echo off
If existe: 1.txt echo "exist 1.txt"
Goto
Redirect execution command
@ Echo off
Net user
If % ERRORLEVEL % = 0 goto successed
If % ERRORLEVEL % = 1 goto failed
: Successed
Echo netuser execution successful!
Goto return
: Failed
Echo netuser execution failed!
: Return
Set
SET command
FOR parameter % variable name IN (related file or command) DO Command executed
Parameter: FOR has four parameters:/d/l/r/f
Example:
Set protogen = "% ~ Dp0 .... \ Tools \ release \ protobuf \ protogx \ protogx.exe"
% ~ Dp0 \ indicates the current path
/D is only a directory
@ Echo off
For/d % I in (*) do @ echo % I
Pause
Save it in the root directory of drive C and print out all directory names under drive C.
@ Echo off
For/d % I in (???) Do @ echo % I
Pause
The current directory contains 1-3 letters in the directory name.
Echo off
For/d % I in (e: *) do echo % I
This command only scans the name of the subdirectory in the current directory, does not scan the subdirectory name contained in the subdirectory
/R Recursion
@ Echo off
For/r c: \ % I in (*. exe) do @ echo % I
Pause
Search all exe files
For/r c: \ % I in (.) do echo % I
Scans the names of all subdirectories in the current directory and recursively traverses them.
/F is used to traverse objects.
For/f % I in (a.txt) do echo % I
Will read the.txt content. If there is no/f, only the file name will be displayed
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.