最新的区别:一个%%和%变量%变量? [英] whats the difference between: %%a and %variable% variables?

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

问题描述

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

右键,在此code(可能无法正常工作,这就是我试图找出),我们有%%了,这种为命令。

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

首先,最新%变量%和%%之间的差的

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

二,有人可以解释'为'命令我?我已经Google'd它的方式太多,所有的解释似乎办法复杂...

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写着菜单=一个我怎样才能得到它就像%usemenu%的变量设置值的?

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?

感谢提前任何帮助!

推荐答案

%变量%环境的变量。他们设定设置,可以用%富%访问!富<! / code>(延迟扩展如果启用)。 %% A 是由命令创建的特殊变量重新present电流回路项目或令牌的当前行。

%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 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.

您可以


  • 遍历文件:在%X(* .txt)的DO ...

  • 重复东西的 N 的时间:在(1,1,15)不要... (参数是/ L%×启动的和的结束的)

  • 遍历组值:在%X(A,B,C)DO ...

  • 遍历文件的行: FOR / F%X中(foo.txt的)DO ...

  • 文件的标记化行: FOR / F令牌= 2,3 * delims =,在(foo.txt的)%×DO ...

  • 遍历命令的输出: FOR / F%X在('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 如果使用批处理文件外)非常相似参数批处理文件和子程序(%1 %2 ,...)。有些种类的扩展可以适用于他们,例如,如果变量重新presents一个文件名,你可以使用路径来获取是文件名和扩展名 %%〜NXA 。这些的完整概述在帮助,给出了

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%将导致%富%除了每 A 是由替换b 。您还可以利用字符串:%foo的:〜4.2%。这些东西描述可以在帮助集上找到

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.

至于为什么%变量% %% A 是不同的东西,这是一个有点难以回答,可能只是一个历史怪胎。如上所述还有第​​三种类型的变量,%1 等,这些都是非常相似用于那些和已经存在较长,我猜。由于环境变量是一个有点笨拙到使用由于对延迟扩张的决定可能是为了使用相同的机制作为自变量,而不是环境块,因此严重依赖变数。

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.

另外的环境变量可能会更贵,因为这个过程有一个特殊的环境的内存块,他们都存储在变量=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.

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

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

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

这将只运行设置如果preceding命令是成功的,即菜单= A 是找到。这应该要容易得多。从我读你想看看菜单= A 是否不包含冒号一条线,在这种情况下存在 usemenu 应设置为 A ,对不对? (27:11 B C 。你可以尝试哄成这样,通过遍历文件或输出线和适当的记号化弄清楚菜单的值,但根据行的格式这可能会非常棘手如果你有没有什么理论上的工作,那么你应该简单地坚持这一点。但是你可以用它周围的循环,以避免重复在同一行三次 A b C

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%\gamelauncher\options.txt" | find "menu=%%X" && set usemenu=%%X
)

如果您在解析文件很简单,但是,在每行只的name = value 对,其中:富将是一个评论,那么你的可能的使用 以及

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%\gamelauncher\options.txt) do (
  if "%%A"=="menu" set usemenu=%%B
)

但是,这取决于文件的精确格式一点。上面的代码片段,现在将由线和每行读取文件中的行冒号(即 EOL = 选项)后会抛弃一切,用等号作为标记分隔符,抓住两个记号:之前的第一个 = 及其之后的所有部分。该令牌被命名为开头 %% A 所以第二个是隐含 %%乙(同​​样,这解释在帮助)。现在,对于每一行,我们考察的第一个标记,看看它是否是菜单,如果是这样,它的值赋给 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 :-).

这篇关于最新的区别:一个%%和%变量%变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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