Table of contents
1 Overview
bat, batch file programming, batch program. In DOS and Windows (any) systems, a .bat file is an executable script program consisting of a series of commands, which can include calls to other programs. Each line of this file is a DOS command (most of the time it is just like the command line we execute at the DOS prompt). You can use Edit under DOS or any text file editing tool such as Windows Notepad to create and modify it. Batch file.
In one sentence, bat is a script in the Windows or DOS operating system. It is written using DOS commands, which is the type of DOS command we input after entering the DOS interface with cmd.
Here is what a bat basically looks like:
A bat is actually a program written in DOS. It is the same as other programs. It is composed of variables + operators + process control. Therefore, this article is also divided into variables, operators, and process control to introduce bat respectively.
2.Variables
set keyword to declare variables
3.Operator
+ - * /
Use () to specify priority
set /a means performing arithmetic operations
3.2. Redirection operator
Redirection, that is, passing the output results to subsequent operations or saving the operation results.
>The content of the file on the left overwrites the content of the file on the right. If the file on the right does not exist, it will be created automatically.
>>The content of the file on the left is appended to the file on the right. If the file on the right does not exist, it will be automatically created.
<The content of the file on the right overwrites the file on the left. If the file on the left does not exist, it will not be automatically created.
>>The content of the file on the right is appended to the file on the left. If the file on the left does not exist, it will not be automatically created.
3.3.Multiple named operators
Multiple named operations, namely AND, OR.
&&short circuit &
||Short circuit or
Multiple statements can be connected using AND and OR to achieve the effect of multiple statements being executed together.
3.4. Pipeline operator
Pipeline operation, that is, the output of the previous operation is used as the input of the next operation.
Operator: |
Here's an example of finding all network connections and then finding the TCP connections among them:
4.Command
4.1.Basic commands
The basic command format of bat is
Main command Subcommand Parameter Operation
Use /? to view help
4.2.Parameter passing
4.3. View script content
type bat script name
4.4. Comments
4.5. Date and time
date View date
timeView time
4.6.Startup script
There are two ways to start the bat script:
- Script name
- start command
The start command can be followed by parameters to specify whether to run in the current window or pop up a new window.
start script name, a new window will pop up to run
start /B script name, run in the current window.
4.7. Call other bat
call bat script, call other bat scripts.
4.8.Task management
4.8.1. View task list
tasklist, view the list of tasks running on the current computer
You can view the task list of the remote machine:
Supported filters:
4.8.2.Task termination
4.9.Folder
Structure view
4.10. Shutdown
4.11. Environment variables
Use the set command to view all environment variables of the current system
%Variable name% can get the variables of the current system
4.12. Directory
4.12.1. View
you
View all non-hidden files in the current directory by default
/A displays all files in the current directory, including hidden files
4.12.2.Create
4.12.3.Delete
Delete empty directories:
Delete non-empty directories:
4.12.4.Switch
cd
Create a new empty directory. There will not be any files in this directory, but it will have two paths.
. represents the current directory
..indicates the upper level directory
4.12.5. Double naming
ren, abbreviation of rename
4.13.Delete files
5. Process control
5.1. Judgment
5.2. Loop
Syntax format of loop structure
for switch in (range) do specific operations
The switch means which types of data are traversed:
/d means folder (directory)
/r means file
/f indicates file content
5.3.Jump
goto can jump to the specified label position
6.Interaction
Using goto can make bat interactive
set /p opt= Get the value from standard input and assign it to the opt variable
7. Actual combat
Obtain detailed information of a computer remotely
Trigger scripts and browse results through web pages
one:
html:
It should be noted that for the sake of security, browsers prohibit the execution of scripts. Only IE browser can directly execute scripts in hyperlinks, and a pop-up window will pop up for confirmation before execution.