Bat / CMD 实操录 —— WyPo 20230214 (数组、指针、模板)
bat吧
全部回复
仅看楼主
level 9
WyPo🍺 楼主
楼2:数组
楼3:指针
楼4:模板
——
2023年02月14日 15点02分 1
level 9
WyPo🍺 楼主
@璐村惂鐢ㄦ埛_000076K馃惥 off & if not "%~1"=="" goto :__%~1
rem
rem 数组
rem 22:50 2023/2/14 WyPo
rem
rem 操作 双击脚本直接运行
rem
rem 主对象 asInArg[]
rem
:load
setlocal DISABLEDELAYEDEXPANSION
set local=1
set error=0
:include
goto :main
:main (void){void}
rem
rem 程序主入口
rem
setlocal ENABLEDELAYEDEXPANSION
set /a local+=1
rem
call "%~0" func "1122333" 4455 6677 "demo" "debug"
:~eof
for /l %%a in (1,1,%local%) do endlocal
goto :eof
:__func (char *asInArg[]){void}
setlocal ENABLEDELAYEDEXPANSION
set local=1
set asInArg[]=0
for %%a in (%2 %3 %4 %5 %6 %7 %8 %9) do (
set /a asInArg[]+=1
set asInArg[!asInArg[]!]=%%a
)
if %asInArg[]% gtr 0 (
echo.传入的参数有 %asInArg[]% 个,各存储的数据为:
for /l %%a in (1,1,%asInArg[]%) do (
echo. asInArg[%%a]=!asInArg[%%a]!
)
) else (
echo.没有任何参数传入,其中 %~1 只属于调用的函数
)
goto :~eof
:break
2023年02月14日 15点02分 2
level 9
WyPo🍺 楼主
@璐村惂鐢ㄦ埛_000076K馃惥 off & if not "%~1"=="" goto :__%~1
rem
rem 指针
rem 23:31 2023/2/14 WyPo
rem
rem 操作 双击脚本直接运行
rem
rem 主对象 vPoint
rem
:load
setlocal DISABLEDELAYEDEXPANSION
set local=1
set error=0
:include
goto :main
:main (void){void}
rem
rem 程序主入口
rem
setlocal ENABLEDELAYEDEXPANSION
set /a local+=1
rem
set 菜单[]="饮品[]","晚餐[]"
rem
for %%a in (饮品[]) do (
set %%a=
for %%b in (
"可乐=3"
"咖啡=6"
"橙汁=3"
"红茶=4"
) do (
set %%~b
for /f "tokens=1 delims==" %%c in ('echo.%%~b') do (
set %%a=!%%a!,"%%~c"
)
)
set %%a=!%%a:~1!
)
rem
for %%a in (晚餐[]) do (
set %%a=
for %%b in (
"红烧茄子=15"
"红烧脆皮猪=18"
"麻婆豆腐=10"
"焖牛腩=20"
"烧鹅=18"
"炒花甲=24"
) do (
set %%~b
for /f "tokens=1 delims==" %%c in ('echo.%%~b') do (
set %%a=!%%a!,"%%~c"
)
)
set %%a=!%%a:~1!
)
rem
rem 多维指针的实际运用
rem
set vPoint=%菜单[]%
for %%a in (%vPoint%) do for %%b in (!%%~a!) do (
echo.%%~a 当中 %%~b 的价格为 !%%~b! 元
)
echo.
set vPoint=%晚餐[]%
echo.其中,晚餐的菜色有:
for %%a in (%vPoint%) do (
echo. %%~a
)
echo.
for %%a in (饮品[]) do (
echo.%%a 当中有:
for %%b in (!%%a!) do (
echo. %%~b
)
)
echo.
set vPoint=菜单[]
for %%a in (vPoint) do (
echo.vPoint的己值:!%%a!
echo.vPoint的己址:%%a
for /f "tokens=*" %%b in ('echo.!%%a!') do (
echo.vPoint的它值:!%%b!
)
echo.vPoint的它址:!%%a!
)
:~eof
for /l %%a in (1,1,%local%) do endlocal
goto :eof
:break
2023年02月14日 15点02分 3
level 9
WyPo🍺 楼主
@璐村惂鐢ㄦ埛_000076K馃惥 off & if not "%~1"=="" goto :__%~1
rem
rem 模板对象
rem 22:04 2023/2/14 WyPo
rem
rem 操作 双击脚本直接运行
rem
rem 主对象 struct{};task_csv{}
rem 调用 call :task_csv oCmd
rem 给PID传值 set oCmd.PID=0
rem 给PID传值 set oCmd.%oCmd[2]%=0
rem
:load
setlocal DISABLEDELAYEDEXPANSION
set local=1
set error=0
:include
goto :main
:struct {}
rem
rem 模板初始化
rem
set nul=0
for %%a in (!%~1!) do (
set /a nul+=1
set "%~1[!nul!]=%%~a"
)
goto :break
:task_csv {}
rem
rem 模板定义
set %~1="映像名称","PID","会话名","会话#","内存使用","状态","用户名","CPU时间","窗口标题"
rem
rem 调用模板初始化
call :struct "%~1" "%~2"
rem
goto :break
:main (void){void}
rem
rem 程序主入口
rem
setlocal ENABLEDELAYEDEXPANSION
set /a local+=1
rem
rem 调用"task_csv"模板生成实例名为"oCmd"对象
call :task_csv oCmd
rem
rem 生成一个外部窗口用于查看其变化
set title=[WinCMD]%date% %time%
start "%title%" cmd /c (call "%~0" WinCMD "%title%")
rem
rem 取得外部窗口的所有数值,按模板排列方式进行取值
for /f "tokens=*" %%a in ('call "%~0" taskCSV 9 "%title%"') do (
set cntNum=0
for %%b in (%%a) do (
set /a cntNum+=1
rem
rem 按编号归位
for %%c in (oCmd[!cntNum!]) do set "oCmd.!%%~c!=%%~b"
)
)
rem
rem 查看"oCmd"对象的"PID"值
echo.[提示]开始监测PID对象:_%oCMD.PID%
echo.
rem 循环监测"oCmd"对象的"窗口标题"变化
:main_while[0]
for /f "tokens=*" %%a in ('call "%~0" taskCSV 2 "%oCMD.PID%" 9') do set oCmd.!oCmd[9]!=%%a
echo.对象[%oCmd.PID%]的标题变化:_%oCmd.窗口标题%
goto :main_while[0]
:main_while[0]_eof
:~eof
for /l %%a in (1,1,%local%) do endlocal
goto :eof
:__winCMD (char *sTitle){void}
rem
setlocal ENABLEDELAYEDEXPANSION
set local=1
rem
call :task_csv oCmd
for /f "tokens=*" %%a in ('call "%~0" taskCSV 9 "%~2"') do (
set cntNum=0
for %%b in (%%a) do (
set /a cntNum+=1
for %%c in (oCmd[!cntNum!]) do set "oCmd.!%%~c!=%%~b"
)
)
rem
call "%~0" delay 6
rem
:WinCMD_while[0]
set title=[MyPid:_%oCmd.PID%]%date% %time%
echo.%title%
title %title%
call "%~0" delay 3
goto :%~1_while[0]
:WinCMD_while[0]_eof
goto :~eof
:__delay (int nSecond){void}
for /f "tokens=*" %%a in ('timeout /t %~2 /nobreak') do set nul=
goto :~eof
:__taskCSV (int nInTokens, void *vFindFlag, int *nShowTokens){char *}
rem
setlocal ENABLEDELAYEDEXPANSION
set local=1
rem
set "nInTokens=%~2"
set "vFindFlag=%~3"
set "nShowTokens=0"
rem
for %%a in (%4) do set nShowTokens=%%~a
rem
for /f "tokens=*" %%a in ('tasklist /v /fo csv ^| find /i "%vFindFlag%"') do (
set cntNum=0
for %%b in (%%a) do (
set /a cntNum+=1
if !cntNum! equ %nInTokens% if "%%~b"=="%vFindFlag%" (
if %nShowTokens% equ 0 (
echo.%%a
) else (
set cntShowTokens=0
for %%c in (%%a) do (
set /a cntShowTokens+=1
if !cntShowTokens! equ %nShowTokens% echo.%%c
)
)
)
)
)
goto :~eof
:break
2023年02月14日 15点02分 4
level 9
WyPo🍺 楼主
将以上内容以"ANSI"格式保存为".bat"文件,直接双击运行
2023年02月14日 15点02分 5
level 9
WyPo🍺 楼主
格式防乱源码图
2023年02月14日 16点02分 6
level 4
大佬[你懂的]
2023年03月16日 15点03分 7
1