%%a 和 %variable% 变量之间有什么区别? [英] whats the difference between: %%a and %variable% variables?

查看:19
本文介绍了%%a 和 %variable% 变量之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for /f "tokens=*" %%a in ('find /v ":" "%appdata%gamelauncheroptions.txt" ^| find "menu=a"') do ( set usemenu=a )
for /f "tokens=*" %%a in ('find /v ":" "%appdata%gamelauncheroptions.txt" ^| find "menu=b"') do ( set usemenu=b )
for /f "tokens=*" %%a in ('find /v ":" "%appdata%gamelauncheroptions.txt" ^| find "menu=c"') do ( set usemenu=c )

是的,在这段代码中(这可能不起作用,我正在尝试找出)我们在for"命令中有这个%%a".

Right, in this code (which may not work, that what i'm trying to find out) we have this "%%a" in that 'for' command.

首先,%variable% 和 %%a 有什么区别?

First, whats the difference between %variable% and %%a?

第二,有人可以向我解释for"命令吗?我在谷歌上搜索得太多了,所有的解释似乎都很复杂......

Second, can someone explain the 'for' command to me? I have Google'd it way too much and all the explanations seem way to complicated...

我想要做的是从 options.txt 中提取一个变量,以便我可以更改游戏启动器的菜单样式.有 3 种样式(a、b 和 c),因此如果 options.txt 显示menu=a",我如何才能将 %usemenu% 之类的变量设置为 a 的值?

What I am trying to do is pull a variable from options.txt, so i can change the menu style of my game launcher. there are 3 styles (a, b and c), so if the options.txt reads "menu=a" how can i get it to set a variable like %usemenu% to the value of a?

提前感谢您的帮助!

推荐答案

%variable%environment 变量.它们是用 set 设置的,可以用 %foo%!foo! 访问(如果启用了延迟扩展).%%a 是由 for 命令创建的特殊变量,用于表示当前循环项或当前行的标记.

%variable% are environment variables. They are set with set and can be accessed with %foo% or !foo! (with delayed expansion if enabled). %%a are special variables created by the for command to represent the current loop item or a token of a current line.

for 可能是批处理文件中最复杂和最强大的部分.如果您需要循环,那么在大多数情况下 for 都可以满足您的需求.help for 有一个总结.

for is probably about the most complicated and powerful part of batch files. If you need loop, then in most cases for has you covered. help for has a summary.

你可以

  • 迭代文件:for %x in (*.txt) do ...
  • 重复 n 次:for/l %x in (1, 1, 15) do...(参数是 startstepend)
  • 迭代一组值:for %x in (a, b, c) do ...
  • 遍历文件的行:for/f %x in (foo.txt) do ...
  • 标记文件的行:for/f "tokens=2,3* delims=," %x in (foo.txt) do ...
  • 迭代命令的输出:for/f %x in ('somecommand.exe') do ...
  • iterate over files: for %x in (*.txt) do ...
  • repeat something n times: for /l %x in (1, 1, 15) do... (the arguments are start, step and end)
  • iterate over a set of values: for %x in (a, b, c) do ...
  • iterate over the lines of a file: for /f %x in (foo.txt) do ...
  • tokenize lines of a file: for /f "tokens=2,3* delims=," %x in (foo.txt) do ...
  • iterate over the output of a command: for /f %x in ('somecommand.exe') do ...

这只是一个简短的概述.它变得更复杂,但请阅读帮助.

That's just a short overview. It gets more complex but please read the help for that.

%%a 形式的变量(或 %a 如果 for 在批处理文件之外使用)非常类似于批处理文件和子程序(%1%2、...).可以对它们应用某些类型的扩展,例如,如果变量表示带有路径的文件名,则仅获取文件名和扩展名,您可以使用 %%~nxa.help for 中给出了这些的完整概述.

Variables of the form %%a (or %a if for is used outside of batch files) are very similar to arguments to batch files and subroutines (%1, %2, ...). Some kinds of expansions can be applied to them, for example to get just the file name and extension if the variable represents a file name with path you can use %%~nxa. A complete overview of those is given in help for.

另一方面,环境变量还有其他一些特殊的东西.您可以通过 %foo:a=b% 在它们中执行替换将导致 %foo% 除了每个 a 被替换为 <代码>b.您也可以使用子字符串:%foo:~4,2%.这些内容的描述可以在help set中找到.

On the other hand, environment variables have other kinds of special things. You can perform replacements in them via %foo:a=b% would result in %foo% except that every a is replaced by a b. Also you can take substrings: %foo:~4,2%. Descriptions of those things can be found in help set.

至于为什么 %variables%%%a 是不同的东西,这有点难以回答,可能只是历史上的古怪.如上所述,还有第三种变量,%1 等,它们与 for 中使用的那些非常相似,并且存在的时间更长,我猜.由于环境变量在 for 中使用起来有点笨拙,因为块和因此严重依赖延迟扩展,因此可能决定使用与参数相同的机制而不是环境变量.

As to why %variables% and %%a are different things that's a bit hard to answer and probably just a historical oddity. As outlined above there is a third kind of variable, %1, etc. which are very similar to those used in for and have existed for longer, I guess. Since environment variables are a bit unwieldy to use in for due to blocks and thus heavy reliance on delayed expansion the decision probably was made to use the same mechanisms as for arguments instead of environment variables.

此外,环境变量可能更昂贵,因为该进程有一个特殊的环境"内存块,它们存储在 variable=value␀ 对中,因此更新环境变量涉及潜在的复制一点内存,而其他类型的变量可能更轻量级.不过,这只是猜测.

Also environment variables could be more expensive, given that the process has a special "environment" block of memory where they are stored in variable=value␀ pairs, so updating environment variables involves potentially copying around a bit of memory while the other kind of variables could be more lightweight. This is speculation, though.

至于你的问题,你真的不需要 for 这里:

As for your problem, you don't really need for here:

find /v ":" "%appdata%gamelauncheroptions.txt" | find "menu=a" && set usemenu=a

如果前面的命令成功,即找到menu=a,这只会运行set.这应该比 for 容易得多.根据我读到的内容,您正在尝试查看 menu=a 是否存在于不包含冒号的行中,在这种情况下 usemenu 应设置为 一个,对吧?(对于 bc 也是如此.您可以尝试通过循环文件或输出的行并适当地标记化来哄 for 这样做找出 menu 的值,但根据行的格式,这可能会很棘手.如果你在理论上可行,那么你应该坚持下去.但是你可以使用循环避免在 abc 中重复同一行 3 次:

This will only run the set if the preceding command was successful, i.e. menu=a was found. This should be considerably easier than for. From what I read you're trying to look whether menu=a exists in a line that does not contain a colon and in that case usemenu should be set to a, right? (And likewise for b and c. You could try coaxing for into doing that by looping over the lines of the file or output and tokenizing appropriately to figure out the value of menu but depending on the format of the lines this can be tricky. If what you have there works in theory then you should simply stick to that. You can however use a loop around it to avoid having to repeat the same line three times for a, b and c:

for %%X in (a b c) do (
  find /v ":" "%appdata%gamelauncheroptions.txt" | find "menu=%%X" && set usemenu=%%X
)

如果您正在解析的文件很简单,但是,每行中只有 name=value 对,其中 : foo 将是注释,那么您也可以使用for:

If the file you are parsing is simple, however, with just name=value pairs in each line where : foo would be a comment, then you could use for as well:

for /f "tokens=1,* eol=: delims==" %%A in (%appdata%gamelauncheroptions.txt) do (
  if "%%A"=="menu" set usemenu=%%B
)

但这在一定程度上取决于文件的确切格式.上面的代码片段现在将逐行读取文件,每一行将丢弃冒号后的所有内容(eol=: 选项),使用等号作为标记分隔符并捕获两个标记:部分在第一个 = 之前和之后的所有内容.标记以 %%A 开头,所以第二个标记是隐含的 %%B(同样,这在 help for 中有解释).现在,对于每一行,我们检查第一个标记并查看它是否是 menu,如果是,则将其值分配给 usemenu 变量.如果你有很多可能的选项来支持这肯定更容易维护:-).

But that depends a little on the exact format of the file. Above snippet would now read the file line by line and for each line would discard everything after a colon (the eol=: option), use the equals sign as a token delimiter and capture two tokens: The part before the first = and everything after it. The tokens are named starting with %%A so the second one is implicitly %%B (again, this is explained in help for). Now, for each line we examine the first token and look whether it's menu and if so, assign its value to the usemenu variable. If you have a lot of possible options to support this is certainly easier to maintain :-).

这篇关于%%a 和 %variable% 变量之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆