-
参数
-
说明
- 要么是执行
bat
, 要么是执行call
调用新的bat
或label
; - 即在新的环境中, 有参数的传递;
- 要么是执行
-
参数引用
%1 - %9
, 不能有%10
,这里是%1
. 如何访问10
.就需要其他机制;%*
表示参数; 参数从0
开始,0
表示自身, 不算做参数; 即*
表示1-n
;
-
参数简单处理
%~1
:无;有双引号就去掉; 即"var" => var
%~f1
:f:fully qulified File path
, 完整路径;%~d1
:d:Drive letter only
: 获取盘符;%~p1
:p:Path only
; 获取文件路径不包括文件名;%~n1
:n:file Name only
: 获取文件名, 路径后缀名等都不要;%~x1
:x:name eXtension only
: 获取文件后缀名, 即xxx.bat => .bat
;%~s1
:s:fully qulified path that contains Short names only
: 全路径;%~a1
:a:file Attributes
, 类似linux
的rwx
之类的;%~t1
:t:date and Time of file
, 最近修改时间;%~z1
:z:the siZe of file
, 文件大小;%~$PATH:1
: 路径搜索,先后顺序; 没有找到返回空;%~tz1
组合;任意组合;%~dp$PATH:1
-
-
案例
-
arg
大于10
个call :function 11 12 13 14 15 16 17 18 19 20 21 exit /b 0 :function setlocal EnableExtensions for %%i in (%*) do echo %%i endlocal && exit /b 0
11 12 13 14 15 16 17 18 19 20 21 [Finished in 264ms]
-
shift [/n]
shift /3,0 <= n <=8
- 原理是
0
保持不变;shift /n
,等级于for(int i = n ; i < argc ; ++i) {argv[i] = argv[i+1];}
- 即将参数从
n
开始, 后面的所有往前移动一位;第n
则被丢弃; - 移动多次就可以看到更后面;
call :function 11 12 13 14 15 16 17 18 19 20 21 exit /b 0 :function setlocal EnableDelayedExpansion shift if "%1" neq "" ( call :function %1 %2 %3 %4 %5 %6 %7 %8 %9 ) echo 0 == %0, 1+ == %* endlocal && exit /b 0
0 == 20, 1+ == 20 0 == 19, 1+ == 19 20 0 == 18, 1+ == 18 19 20 0 == 17, 1+ == 17 18 19 20 0 == 16, 1+ == 16 17 18 19 20 0 == 15, 1+ == 15 16 17 18 19 20 0 == 14, 1+ == 14 15 16 17 18 19 20 0 == 13, 1+ == 13 14 15 16 17 18 19 20 0 == 12, 1+ == 12 13 14 15 16 17 18 19 20 0 == 11, 1+ == 11 12 13 14 15 16 17 18 19 20 21 [Finished in 301ms]
-
-
bat args
最新推荐文章于 2023-04-24 20:45:00 发布