Clever use of .bat batch files

1. Concept first

Sogou entry explanation:
bat file is a batch file under dos. Batch files are plain text files that contain one or more commands. It has a file extension of .bat or .cmd. Enter the name of the batch file at the command prompt, or double-click the batch file, and the system will call cmd.exe to run them one by one in the order in which the commands appear in the file. Routine or repetitive tasks can be simplified by using batch files (also known as batch programs or scripts). [1] Intruders often implement functions such as multi-tool combined intrusion, automatic intrusion, and result extraction by writing batch files.

2. Getting Started Basics

insert image description here

How to add comments : use rem or :: or @echo,
rem : has the echo function
:: : normal comment
@echo : turn off the echo function of this line

**How ​​to make the execution information stay on the interface? **Use pause
pause to make the script pause, usually to prevent cmd from flashing, add pause to facilitate viewing the
relative path and absolute path of the output content?

@echo off  
::相对路径
echo relative path >C:\>1\相对路径.txt  
::绝对路径
echo absolute path >C:\Users\zhilan.zheng\Desktop\1\绝对路径.txt 
pause


Use bat to run snipping tool?


start snippingtool

Is the program running correctly?

errorlevel The return code of the program echo %errorlevel% After each command runs, you can use this command line format to view the return code. It is
used to judge whether the command just executed successfully. The default value is 0, and the general command execution error will set errorlevel to 1

CALL understand?

The CALL command can call another batch during the execution of a batch. After another batch is executed, the original
batch will continue to be executed. The CALL command calls a batch command. Useful, such as multi-level nesting of variables, see later in the tutorial. In batch programming, command strings can be generated according to certain conditions, and the strings can be executed with call, see examples.
CALL [drive:][path]filename [batch-parameters] Call other batch programs. The filename
parameter must have a .bat or .cmd extension. CALL :label arguments
call the command section in this file, which is equivalent to a subroutine. The called command section starts with the label :label and ends with the command goto :eof.

Some special symbols:

Commonly used special symbols
1, @ command line echo mask
2, % batch variable guide
3, > redirection
4, >> redirection
5, <, >&, <& redirection
6, | command pipeline Symbol
7, ^ escape character
8, & combination command
9, && combination command
10, || combination command 11,
"" string delimiter 12
, comma
13, ; semicolon
14, () bracket
15, ! exclamation mark
16 , Other special markers that may be seen in batch processing: (omitted)
CR (0D) command line terminator
Escape (1B) ANSI escape character guide
Space (20) commonly used parameter delimiter
Tab (09) ; = not Commonly used parameter delimiter
+ COPY command file connector
* ? File wildcard
/ parameter switch guide
: batch label guide

3. Example of use [Super Practical]

dir command:

`前缀名
@echo off
title 批量添加前缀名
echo.
echo 本批处理可批量添加前缀名
echo.
echo.&set /p strtemp3= 请输入要添加前缀的文件类型:
echo.&set /p strtemp2= 请输入要添加前缀的文件名字符串(不变则直接回车):
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir /a /b *.%strtemp3%') do (
ren "%%~a" "%strtemp2%%%a")
echo.
echo OK了!
echo.
pause`

后缀名
@echo off
title 批量添加后缀名
echo.
echo 本批处理可批量添加后缀名
echo.
echo.&set /p strtemp3= 请输入要添加后缀名:
echo.&set /p strtemp2= 请输入要添加后缀名字符串(不变则直接回车):
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir /a /b *.%strtemp3%') do (
ren "%%~a" "%%a.%strtemp2%")
echo.
echo OK了!
echo.
pause
@echo off

echo 正在清除系统垃圾文件,请稍等......
del /f /s /q %systemdrive%\*.tmp
del /f /s /q %systemdrive%\*._mp
del /f /s /q %systemdrive%\*.log
del /f /s /q %systemdrive%\*.gid
del /f /s /q %systemdrive%\*.chk
del /f /s /q %systemdrive%\*.old
del /f /s /q %systemdrive%\recycled\*.*
del /f /s /q %windir%\*.bak
del /f /s /q %windir%\prefetch\*.*
rd /s /q %windir%\temp & md %windir%\temp
del /f /q %userprofile%\小甜饼s\*.*
del /f /q %userprofile%\recent\*.*
del /f /s /q "%userprofile%\Local Settings\Temporary Internet  Files\*.*"
del /f /s /q "%userprofile%\Local Settings\Temp\*.*"
del /f /s /q "%userprofile%\recent\*.*"
echo 清除系统LJ完成!
echo. & pause
::查看文件下的一类文件
@echo off
for /r "C:\1" %%v in (*.txt) do echo %%v # 匹配*.txt
pause>nul
::批量修改文件属性  
ren *.ss *.jpg
::新建一个文件并往文件里面写入hello word
@echo off  
echo hello world >nihao.c
pause

Human-computer interaction?

@echo off
echo 1.aa
echo 2.bb
:first
echo Enter your choice:
set /p opt=
if %opt%==1 goto one
if %opt%==2 goto two
echo no such choice
goto first
:one
echo your choice 1
pause:nul
exit
:two
echo your choice 2
pause:nul
exit

Guess you like

Origin blog.csdn.net/weixin_43673603/article/details/126268017