1 内嵌参数变量
1.1 介绍
内嵌参数变量指:%i,其中i为大于等于0的整数。如%0, %1, %2,...
1.2 作用
可以客制化功能。
1.3 案例
批处理文件:main_Param.bat
@echo off && setlocal ENABLEDELAYEDEXPANSION
set path_mode0=%cd%
set path_mode1=%0
set path_mode2="%0"
set path_mode3="%~0"
set path_mode4="%~d0"
set path_mode5="%~p0"
set path_mode6="%~dp0"
set path_mode7="%~n0"
set path_mode8="%~x0"
set path_mode9="%~s0"
set path_mode10="%~a0"
set path_mode11="%~t0"
set path_mode12="%~z0"
set path_mode13="%~$PATH:0"
set path_mode14="%~f0"
set path_mode15="%~fs0"
set /a LineNum=-1
for %%I in (%path_mode0%,%path_mode1%, %path_mode2%, %path_mode3%, %path_mode4%, %path_mode5%, %path_mode6%, %path_mode7%, %path_mode8%,
%path_mode9%, %path_mode10%, %path_mode11%, %path_mode12%, %path_mode13%, %path_mode14%, %path_mode15%) do (
set /a LineNum=!LineNum!+1
echo Line!LineNum!:%%I)
echo !LineNum!
echo %LineNum%
pause
exit
运行结果:
Line0:C:\Users\Andy.fan\Desktop\dos-test
Line1:"C:\Users\Andy.fan\Desktop\dos-test\main_Param.bat"
Line2:""C:\Users\Andy.fan\Desktop\dos-test\main_Param.bat""
Line3:"C:\Users\Andy.fan\Desktop\dos-test\main_Param.bat"
Line4:"C:"
Line5:"\Users\Andy.fan\Desktop\dos-test\"
Line6:"C:\Users\Andy.fan\Desktop\dos-test\"
Line7:"main_Param"
Line8:".bat"
Line9:"C:\Users\Andy.fan\Desktop\dos-test\MAIN_P~1.BAT"
Line10:"--a--------"
Line11:"04/08/2023 01:44 PM"
Line12:"784"
Line13:"C:\Users\Andy.fan\Desktop\dos-test\main_Param.bat"
Line14:"C:\Users\Andy.fan\Desktop\dos-test\main_Param.bat"
Line15:"C:\Users\Andy.fan\Desktop\dos-test\MAIN_P~1.BAT"
15
15
Press any key to continue . . .
1.4 参数分析
- %0:传入脚本文件的第0个参数,该参数为脚本文件名(是否带绝对路径视具体情况而定),会自动包含双引号;见Line1
- ~:去除%0展开后的双引号,为了后期字符串拼接;对比Line2和Line3
- d:driver的第一个字母,获取驱动符或者说盘符;见Line4
- p:path的第一个字母,获取路径;见Line5
- dp:获取盘符和路径;见Line6
- n:name的第一个字母,获取文件名;见Line7
- x:extension意思,获取扩展名;见Line8
- s:short第一个字母,获取简短名字,见Line9
- a:attribute第一个字母,获取文件属性,见Line10
- t:time第一字母,获取时间最后修改时间,见Line11
- z:获取文件大小,单位为字符,见Line12
- ~$PATH: :相当于dp功能,见Line13
- f:file第一个字母,文件,见Line14