[TOC]

下载一个“Bat To Exe Converter”的程序即可将.bat的文件转换为.exe的执行文件Bat To Exe Converter下载地址如下:
pageid: 224

http://pan.baidu.com/s/1sjNM8Lf

转换方法我就不介绍了,一共就几个选项,我想只要不是智障应该都知道怎样用的。
封装为.exe的程序后如下图:

基础示例

实际案例1.: 系统补丁检测并安装批处理
描述: 可以使用下面的方法来检测和打补丁,采用systeminfo命令可以列出打过的补丁。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

@ECHO off
REM By Leo
SET TITLE=Windows 补丁安装脚本 V0.070408
TITLE %TITLE%
SETLOCAL ENABLEDELAYEDEXPANSION
SET PATCHFLAG=KB
SET CAT=%PATCHFLAG%*.cat
SET PATCHLIST="%temp%\patcheslist.tmp"
SET INSTALLED=√ 已安装
SET NOTINSTALLED=× 未安装
SET DELIMS=-----------------------------------------
SET PATCH_TOTAL=0
SET PATCH_NOTINSTALLED=0
SET FLAG_INSTALLED=1*
SET FLAG_NOTINSTALLED=0*

:main
TITLE 正在搜索... -- %TITLE%
ECHO 正在搜索当前目录"%cd%"及其子目录下的补丁...
ECHO 如果想在搜索完成后立即安装未安装的补丁,请按回车。
ECHO.
REM 搜索补丁,没有搜索到则退出,否则继续。
CALL :pfind || (call :error 1& goto :eof)
ECHO.
:confirm
TITLE 请选择要安装的补丁 -- %TITLE%
ECHO 搜索到如上%PATCH_TOTAL%个补丁,其中%PATCH_NOTINSTALLED%个未安装。安装全部请输入ALL,否则请直接按回车安装尚未安装的补丁。
SET confirm=
SET /p confirm=
IF /i "%confirm%" == "ALL" (SET confirm=
) ELSE IF not defined confirm (SET confirm=%FLAG_INSTALLED:~0,1%
) ELSE GOTO confirm
ECHO %DELIMS%
ECHO.
ECHO 正在安装,请稍候。安装程序不会抢占窗口焦点,所以你可以干点别的:)
ECHO.
CALL :setup %confirm%
del %PATCHLIST% 2>nul >nul
TITLE 安装已结束 -- %TITLE%
ECHO %DELIMS%
ECHO.
ECHO 安装已结束。要使补丁生效,你可能需要手动重新启动计算机。
ECHO 按任意键退出。
SET TITLE=
ENDLOCAL
PAUSE >nul
TITLE %ComSpec%
goto :eof

REM 安装补丁
:setup
if "%1" == "" (set patchsum=%PATCH_TOTAL%) ELSE set patchsum=%PATCH_NOTINSTALLED%
set patch_counter=0
FOR /f "eol=%1 usebackq tokens=2,*" %%i in (%PATCHLIST%) DO (
set /a patch_counter+=1
TITLE !patch_counter!/%patchsum%-%%i -- %TITLE%
set nobackup=nobackup
echo %%i | find /i "%PATCHFLAG%8" 2>nul >nul && set nobackup=n
%%j /quiet /passive /norestart /!nobackup! 2>nul >nul
ECHO !patch_counter!/%patchsum% %%i √)
goto :eof

REM 搜索当前目录下的补丁,返回非零值为失败。
:pfind
SET REG=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
SET listinreg="%temp%\listinreg.tmp"
reg query %reg%>%listinreg%
del %PATCHLIST% 2>nul >nul
REM 补丁是一个压缩包
FOR /r %%i in (*%PATCHFLAG%*.exe) DO CALL :pfind.sub "%%~ni" "%%~fi" "%%~sfi"
REM 补丁在压缩包展开后的目录内
FOR /r %%i in (%cat%) DO IF exist %%~dpiupdate.exe FOR /f %%j in ("%%~dpiupdate.exe") DO CALL :pfind.sub "%%~ni" "%%~fj" "%%~sfj"
IF not exist %PATCHLIST% EXIT /b 1
sort %PATCHLIST% /o %PATCHLIST%
rem 得到补丁个数
FOR /f "tokens=3 delims= " %%i in ('find /c /i "%FLAG_NOTINSTALLED%" %PATCHLIST%') DO SET PATCH_NOTINSTALLED=%%i
FOR /f "tokens=3 delims= " %%i in ('find /c /i "%FLAG_INSTALLED%" %PATCHLIST%') DO SET /a PATCH_TOTAL=%%i + %PATCH_NOTINSTALLED%
del %listinreg% 2>nul >nul
IF not defined patch_total EXIT /b 2
IF %patch_total% LSS 1 EXIT /b 3
EXIT /b 0
goto :eof
REM 看看补丁安装了没,然后写到标准输出和%PATCHLIST%内
:pfind.sub
IF "%~3" == "" GOTO :eof
CALL :getkbnum %1
SET id=!errorlevel!
IF not "!id!" == "-1" (
find /i "!id!" %listinreg% 2>nul >nul && (
set status=%FLAG_INSTALLED%& set isinstalled=%INSTALLED%
)||(set status=%FLAG_NOTINSTALLED%& set isinstalled=%NOTINSTALLED%)
ECHO !status! %PATCHFLAG%!id! %2>>%PATCHLIST%
ECHO !isinstalled! %PATCHFLAG%!id! %3
)
goto :eof

REM 返回给定字串中的KB号,返回-1表示失败。
:getkbnum
SETLOCAL ENABLEDELAYEDEXPANSION
SET str=%~1
IF not defined str EXIT /b -1
IF not defined PATCHFLAG SET PATCHFLAG=KB
echo %PATCHFLAG%>getsize.tmp
for %%i in (getsize.tmp) do SET /a offset=%%~zi-2
del getsize.tmp 2>nul >nul
SET start=0
REM 仅有%PATCHFLAG%则返回-1
IF /i "%str%" == "%PATCHFLAG%" EXIT /b -1
REM 删除%PATCHFLAG%前的字符,删除失败则返回-1
:getkbnum.findkb
IF "%~1" == "!str!" (IF "!str:~%start%,%offset%!" == "" (EXIT /b -1
) ELSE IF /i "!str:~%start%,%offset%!" == "%PATCHFLAG%" (SET str=!str:~%start%!
) ELSE (SET /a start+=1 & goto getkbnum.findkb))
REM 仅有%PATCHFLAG%则返回-1
SET str=!str:~%offset%!
IF "%str%" == "" EXIT /b -1
SET start=0
REM 保证%PATCHFLAG%后的第一个字符不是数字时,仍能返回-1
IF "!str:~%start%,1!" GTR "9" EXIT /b -1
IF "!str:~%start%,1!" LSS "0" EXIT /b -1
REM 返回%PATCHFLAG%后的数字
:getkbnum.findnum
IF "!str:~%start%,1!" GTR "9" EXIT /b !str:~0,%start%!
IF "!str:~%start%,1!" LSS "0" EXIT /b !str:~0,%start%!
SET /a start+=1
goto getkbnum.findnum
ENDLOCAL
EXIT /b -1

:error
TITLE ERROR -- %TITLE%
IF "%1" == "1" (ECHO 没有找到补丁。使用的搜索规则是:CAT:%cat%,PATCHFLAG:%PATCHFLAG%。程序已结束。)
SET TITLE=
PAUSE >nul
TITLE %ComSpec%
goto :eof

参考来源: https://blog.csdn.net/darkread/article/details/8064493

实例说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@echo off
mkdir delete
mkdir other
mkdir photo_bak
mkdir photo_ksh
mkdir photo_ksh.rar
mkdir photo_ksh_backup
mkdir photo_ksh_log
mkdir photo_rar
mkdir photo_rar_log
mkdir photo_sfzh_f
mkdir photo_sfzh_z
mkdir temp
mkdir demo

for /f "delims=" %%N in ('dir /A:D /B') ^
do (
xcopy createQuxian.cmd %%N /y
)
pause

示例.利用批处理文件进行更改系统网络IP与网关。

1
2
3
4
5
6
7
8
9
10
@echo off
set /p add=请输入IP地址最后一位(1~254):
set address=10.10.102.%add%
:: rem 以太网IP 与 网关设置
netsh interface ip set address name="以太网" source=static addr=%address% mask=255.255.255.0 gateway=10.10.102.1 gwmetric=1
:: rem 网络DNS域名解析服务器地址设置
netsh interface ip set dnsservers name="以太网" source=static address=192.168.10.254 register=primary
:: 刷新DNS解析
ipconfig /flushdns
ipconfig