Windows bat批处理用for遍历、循环、查找的变量不能在for外用

前言全局说明

Windows bat批处理用for遍历、循环、查找的变量不能在for外用

Windows bat 不像 Linux shell 有很完善的语法,bat中除了判断,很多查询或要遍历的东西都要用 for 完成。


一、说明

1.1 环境:

Windows 

二、for 循环变量

下面的写法,for 循环外是获取不到 file ,因为 for结束时,里面的 file 也被销毁了。
file是局部变量

bak_file_lists=%0;win.bat;git_pull.sh
for %%d in (%bak_file_lists%) do (
	set "file=%file% %%d"
)
echo %file%

三、解决方法

在 for 定义一个全局变量

bak_file_lists=%0;win.bat;git_pull.sh
set file=
for %%d in (%bak_file_lists%) do (
	set "file=%file% %%d"
)
echo %file%


免责声明:本号所涉及内容仅供安全研究与教学使用,如出现其他风险,后果自负。




参考、来源:
百度AI



posted @ 2025-01-08 11:58  悟透  阅读(283)  评论(0)    收藏  举报