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

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

问题描述

我想设置一个变量,例如%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%?

推荐答案

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

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天全站免登陆