如何设置变量,然后在命令提示符的同一行中使用它 [英] How set a variable and then use it in the same line in command prompt

查看:9
本文介绍了如何设置变量,然后在命令提示符的同一行中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个变量,例如%p% 然后在 CMD 的同一行中使用它.

I want to set a variable e.g. %p% and then use it in the same line in CMD.

例如:

set p=notepad.exe&%p%

这行不通.但是 %p% 是为下一行设置的.因此,如果我第二次执行此行,它会起作用.

This not works. But %p% is set for the next line. Therefore if I execute this line for second time, it works.

如何在同一行中使用 %p%?

How can I use %p% in the same line?

推荐答案

当你执行一个批处理文件时,每一行或每一行(括在括号中的行)首先被解析,然后被执行.在解析过程中,对变量的读取操作被替换为变量内的值在命令执行之前.因此,如果在行/块中更改了变量值,您将无法检索此更改后的值,因为解析器已在更改之前删除了对变量中的值的读取操作.

When you execute a batch file, each line or block of lines (lines enclosed in parenthesis) is first parsed and then executed. During the parse, the read operations on the variables are replaced with the value inside the variable before the commands are executed. So if in a line/block a variable value is changed, you can not retrieve this changed value as the parser has removed the read operation with the value in the variable before the change has been made.

所以,你的代码

set p=notepad.exe&%p%

被解析并转换为

set p=notepad.exe&

获取 %p% 的读取操作已被替换为变量中的值(示例假设变量为空).然后执行这个解析的行.

where the read operation to get %p% has been replaced with the value in the variable (the sample assumes the variable is empty). Then this parsed line is executed.

为什么它第二次工作?因为在前一次运行中变量已经被设置,如果它没有被重置,当解析器在第二次运行中进行替换时,变量在行中包含一个要替换的值.

Why does it work the second time? Because in the previous run the variable has been set and, if it has not been reset, when the parser do the replacement in the second run the variable contains a value to replace in the line.

要查看这是执行行为之前的解析,您可以将行更改为

To see that this is a parse before execute behaviour, you can change your line to

set p=notepad.exe&set p

即设置变量并转储环境内容(以p开头的变量),你会看到变量已经设置为notepad.exe.由于这一行不包含对变量的任何读取操作,所以一切都按预期工作.

that is, set the variable and dump the environment content (variables starting with p) and you will see that the variable has been set to notepad.exe. As this line does not contain any read operation on the variable everything works as expected.

如何解决您的问题?有一些选择

How to solve your problem? There are some options

延迟扩张

启用延迟扩展后,可以根据需要将变量读取的语法从 %var% 更改为 !var!,向解析器表明读取操作的替换需要延迟到命令执行

When delayed expansion is enabled, the syntax on variable reads can be changed, where needed, from %var% to !var!, indicating to the parser that the substitution on the read operation needs to be delayed until the command execution

setlocal enabledelayedexpansion
set p=notepad.exe&!p!

如果使用 /v:on

cmd /v:on /c "set p=notepad.exe&!p!"

强制对命令进行第二次解析

这使用 call 命令来强制在线上进行第二次解析

This uses the call command to force a second parse on the line

set p=notepad.exe&call %%p%%

第一个默认解析将文字 %%p%% 替换为文字 %p%(%% 是转义的百分号) 没有做任何变量替换,并且当 call 命令被执行时,该行再次被解析,所以文字 %p% 被解释为一个变量读取并被替换与变量中的值

The first default parse replaces the literal %%p%% with the literal %p% (%% is a escaped percent sign) without doing any variable replacement, and when the call command is executed, the line is parsed again, so the literal %p% is interpreted as a variable read and is replaced with the value in the variable

用什么?这取决于.延迟扩展解决方案具有比 call 解决方案更好的执行时间(call 命令做了更多的工作),但是当启用延迟扩展时,感叹号 (!>) 包含在数据中成为需要妥善处理的问题.

What to use? It depends. Delayed expansion solution has better execution times that call solution (call command does a lot more work), but when delayed expansion is enabled exclamation characters (!) contained in the data become a problem that needs to be properly handled.

这篇关于如何设置变量,然后在命令提示符的同一行中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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