批处理之家's Archiver

verbs 发表于 2020-2-27 11:48

批处理命令set 如何查看指定变量?

如上图我想仅查看 test_1 变量,却显示了 test_1 和 test_10,请问没有办法只显示 test_1?

万分感谢。

Batcher 发表于 2020-2-27 12:17

[b]回复 [url=http://bbs.bathome.net/redirect.php?goto=findpost&pid=226824&ptid=54934]1#[/url] [i]verbs[/i] [/b]


set命令的特性决定了它无法像你希望的那样工作:
[quote]SET command invoked with just a variable name, no equal sign or value
will display the value of all variables whose prefix matches the name
given to the SET command.  For example:
    SET P
would display all variables that begin with the letter 'P'[/quote]

有什么特别的原因必须要用set而不能用echo吗:[code]echo %test_1%[/code]

verbs 发表于 2020-2-27 12:44

[i=s] 本帖最后由 verbs 于 2020-2-27 13:32 编辑 [/i]

感谢大神指点,
我希望根据变量名不同,取到不是的变量值,所以不无法直接使用 echo

测试脚本如下:[code]@echo off
set index=1
:loop_get_value
set test=test_%index%
set | findstr /i /b "\<%test%\>">nul || goto parameters_error
for /f "usebackq delims== tokens=2" %%i in (`set %test%`) do (
echo %%i
)
set /a index+=1
goto loop_get_value

:parameters_error
echo 参数不存在
pause
goto lable_exit

:lable_exit[/code]有没有其它办法,可以实现类似效果,感谢指点

Batcher 发表于 2020-2-27 13:05

[b]回复 [url=http://bbs.bathome.net/redirect.php?goto=findpost&pid=226827&ptid=54934]3#[/url] [i]verbs[/i] [/b]


不知道这个代码是想表达什么意图
但是执行到第5行的时候肯定会 goto parameters_error
第6行永远不会被执行到

verbs 发表于 2020-2-27 13:31

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=226828&ptid=54934]4#[/url] [i]Batcher[/i] [/b]


是的这段代码没有实际意义,主是要实现取的变量的变量值(....>o<....   有点绕)

第五行,不是肯定执行到 goto parameters_erro, 当前的表达式为真时不会执 || 后面的表达式

所以的第六行可以被执行到

zaqmlp 发表于 2020-2-27 14:01

[i=s] 本帖最后由 zaqmlp 于 2020-2-27 14:02 编辑 [/i]

开延迟就行了,判断存不存在可用define[code]set a=1
set b_1=2
call echo;%%b_%a%%%
setlocal enabledelayedexpansion
echo;!b_%a%!
pause[/code]

Batcher 发表于 2020-2-27 16:54

[b]回复 [url=http://bbs.bathome.net/redirect.php?goto=findpost&pid=226830&ptid=54934]5#[/url] [i]verbs[/i] [/b]


有几个变量嵌套的例子可以参考一下
[url]http://bbs.bathome.net/thread-2899-1-1.html[/url]

verbs 发表于 2020-3-18 14:59

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=226850&ptid=54934]7#[/url] [i]Batcher[/i] [/b]


    非常感谢,学习了。

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.