批处理编程案例
计算机信息展示
monitor.bat 内容
@echo off
rem echo. > log.txt
echo Log File >> log.txt
echo. >> log.txt
echo User : %username% >> log.txt
Date /t >> log.txt
Time /t >> log.txt
echo. >> log.txt
echo Process Ran by %username% >> log.txt
echo. >> log.txt
tasklist >> log.txt
echo. >> log.txt
echo Network Activites >> log.txt
netstat -s >> log.txt
ipconfig /all >> log.txt
exit
log.html 内容
<html>
<head><title>Log File - 筱团</title></head>
<body>
<br>
<center><h1><u> Log File </u></h1>
<i>This Log file is created by <b>筱团</b> for monitoring System Activities!</i>
</center>
<br>
<center>
<ul>
<a href="log.txt">Click here to view the Log File</a>
</ul>
</center>
</body>
</html>
效果展示
交互作用
interaction.bat 内容
@echo off
echo 1.show ip address
echo 2.show network link
echo 3.show direcotory
:main
echo Enter your option
set /p opt=
if %opt%==1 goto one
if %opt%==2 goto two
if %opt%==3 goto three
echo Invalid option
goto main
:one
ipconfig /all
pause>nul
exit
:two
network -an
pause>nul
exit
:three
dir
pause>nul
exit
效果展示
自创:密码文件夹
password.bat 内容
@echo off
md password
cd password
for /L %%a in (1,1,3) do (
md %%a
cd %%a
for /L %%b in (1,1,3) do (
md %%b
cd %%b
for /L %%c in (1,1,3) do (
md %%c
)
cd ..
)
cd ..
)
原本我想创建 0-9 的 10 层嵌套密码文件夹,一开始我的思路是回溯,然后我把 C 语言的方法都写好了,转到 bat 时发现 batch 的函数根本没有 return,也就不能执行回溯;于是我干脆改成最简单的 9 层 for 循环,但当我一开始运行,就报错 The filename or extension is too long.
和 The system cannot find the path specified.
,然后我算了一下,发现如果命令执行,总共要创建的文件夹的数量为:10^10 + 10^9 +...10^2 + 10,尼玛这就是天文数字啊,之后我删掉错误溢出的文件,显示正在删除文件,共 2207 个项目,预计耗时 5 分钟... 算了我还是改成 1-3 的 3 层嵌套密码文件夹吧,完。
效果展示
自创:日记保存
password.bat 内容
@echo off
type impromptu.md >> C:\Users\14805\Desktop\工具箱\diary.md
for /L %%a in (0,1,6) do (echo. >> C:\Users\14805\Desktop\工具箱\diary.md)
echo 选 1 打开 diary 选 2 浏览 diary
set /p opt=
if %opt%==1 goto one
if %opt%==2 goto two
:one
start C:\Users\14805\Desktop\工具箱\diary.md
exit
:two
type C:\Users\14805\Desktop\工具箱\diary.md
exit
效果展示
自创:日记保存(更新版)
confer.bat
@echo off
for %%i in ("C:\Users\14805\Desktop\impromptu.md") do (
set log=%%~ti)
set year=%log:~0,4%
set year_f=%year:~0,2%
set year_b=%year:~2,2%
set month=%log:~5,2%
set day=%log:~8,2%
set hour=%log:~11,2%
set minute=%log:~14,2%
set /a we=%year_b%+%year_b%/4+%year_f%/4-%year_f%*2+(26*(%month%+1)/10)+%day%-1
if %we% GTR 7 (goto prime) else (goto continue)
:continue
if %we%==1 (set week=周一)
if %we%==2 (set week=周二)
if %we%==3 (set week=周三)
if %we%==4 (set week=周四)
if %we%==5 (set week=周五)
if %we%==6 (set week=周六)
if %we%==7 (set week=周日)
echo %year%年%month%月%day%日 %week% %hour%点%minute%分 >> C:\Users\14805\Desktop\Toolbox\XOPP\identity\diary.md
for /f "delims=" %%a in (C:\Users\14805\Desktop\impromptu.md) do (
set content=%%a
goto next
)
exit
:prime
set /a we=%we%-7
if %we% GTR 7 (goto prime) else (goto continue)
exit
:next
type C:\Users\14805\Desktop\impromptu.md >> C:\Users\14805\Desktop\Toolbox\XOPP\identity\diary.md
for /L %%a in (0,1,6) do (echo. >> C:\Users\14805\Desktop\Toolbox\XOPP\identity\diary.md)
echo. > C:\Users\14805\Desktop\impromptu.md
echo %content% >> C:\Users\14805\Desktop\impromptu.md
echo. >> C:\Users\14805\Desktop\impromptu.md
exit
up.bat
@echo off
for %%i in ("C:\Users\14805\Desktop\impromptu.md") do (
set log=%%~ti)
set year=%log:~0,4%
set month=%log:~5,2%
set day=%log:~8,2%
set now_year=%date:~3,4%
set now_month=%date:~8,2%
set now_day=%date:~11,2%
set /a verify=year+month+day
set /a verify_now=now_year+now_month+now_day
if %verify% NEQ %verify_now% (C:\Users\14805\Desktop\Toolbox\XOPP\identity\confer.bat)
效果展示
补充:在原有的功能上进行了改进。不用手动修改日期,每次上传日记都会自动记录时间,并清空内容(只留下第一行的每周计划),但每天只能上传一次(判定条件是相同天只能上传一次),因此只需要第二天开机(或当天关机)时运行 Windows Terminal 即可,并且对以往的 .\up.bat
命令也进行了改进,变成了简洁的 up
,这是因为在 Path 中记录了日记文件夹的位置,并且创建了快捷方式
喜欢划水摸鱼的废人