一组语句不会出现在我的批处理文件工作 [英] set statements don't appear to work in my batch file

查看:93
本文介绍了一组语句不会出现在我的批处理文件工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这似乎这里是我的问题的解释

I've found what appears to be an explanation of my problem here

<一个href=\"http://stackoverflow.com/questions/14347038/dos-batch-why-are-my-set-commands-resulting-in-nothing-getting-stored\">DOS批量:为什么导致没有得到存储在我的命令集

但我真的不明白的解释。

but I don't really understand the explanation.

下面是我的脚本...

Here is my script...

for /R /d  %%f in (\Product\Database\SQL\Project\Model\Scripts\*) DO (

REM echo %%f
SET !LOAD_FILE_FILTER= %%f\*.sql
echo file: %!LOAD_FILE_FILTER%
CALL %!BATCH_FILE% -u %!USER_NAME% -p %!PASSWORD% -s %!SERVER_NAME% -d %!DATABASE_NAME% -f %!LOAD_FILE_FILTER% -o %!LOG_FILE%
IF %!EchoErrors%==1 (
    ECHO [
    TYPE %!LOG_FILE%
    ECHO ]
)

回声始终打印文件:* .SQL和脚本我通过这个变种总是抱怨LOAD_FILE_FILTER是空的。

The echo always prints file: *.sql and the script I pass this var to always complains LOAD_FILE_FILTER is empty.

我曾尝试加入 SETLOCAL EnableDelayedExpansion 在文章中建议,但它并没有解决问题。在回声文件:%!LOAD_FILE_FILTER%始终打印在我从运行目录下的子目录最后。在回声%%˚F始终打印正确的值。

I have tried adding setlocal EnableDelayedExpansion as suggested in the article but it doesn't solve the problem. The echo file: %!LOAD_FILE_FILTER% always prints the last subdirectory in the directory I'm running from. The echo %%f always prints the correct value.

什么的!变量背后做/我?

What does the '!' behind the variable do for/to me?

在一个侧面说明,可能有人向我解释

On a side note, could someone explain to me the difference between

SET!VAR

SET VAR

SET !VAR and SET VAR

%VAR和放大器;
&安培;!VAR和放大器;
!VAR!
%% VAR

%VAR& &!VAR& !VAR! %%VAR

推荐答案

我们将开始一个简单的例子

We are going to start with a simple case

set "var="
set "var=test" 
echo %var%

读code,它消除了变量的内容,赋予它新的价值和它相呼应。

Reading the code, it removes the content of the variable, assigns it a new value and echoes it.

让我们改变了一点串联的最后两个命令

Let's change it a bit concatenating the last two commands

set "var="
set "var=test" & echo %var%

相同code,但在这种情况下,输出到控制台不会显示在变量的值。

"Same" code, but in this case the output to console will not show the value in the variable.

为什么呢?在批处理文件,行执行被解析并执行。在语法分析阶段,每一个变量的读操作(其中,检索该变量的值)被替换为在分析时所存储的变量中的值。一旦这样做,将所得的命令被执行。所以,当第二行被解析的previous样品中,它被转换为

Why? In batch files, lines to execute are parsed and then executed. During the parse phase, every variable read operation (where you retrieve the value of the variable) is replaced with the value stored inside the variable at parse time. Once this is done, the resulting command is executed. So, in the previous sample when the second line is parsed, it is converted to

set "var=test" & echo 

现在,有(将当执行线路被分配)就行了,并没有任何价值的回声,因为当线被readed可变未持有任何值没有读操作,以便读出操作已被什么也没有更换。在这一点上,code被执行,感知的行为是设置命令失败,因为我们没有得到回应劝慰明显的价值。

now, there are no read operations on the line and no value to echo, as when the line was readed the variable didn't hold any value (it will be assigned when the line is executed) so the read operation has been replaced with nothing. At this point, the code is executed and the perceived behaviour is that the set command failed as we don't get the "obvious" value echoed to console.

此行​​为也以块找到。块是一组圆括号线(通常为如果结构)以及由解析器被处理如果块中的所有行与串连命令只有一行。全块readed,所有变量读取操作去除并用变量内的值,然后全块,没有变量引用内被执行替换。

This behaviour is also found in blocks. A block is a set of lines enclosed in parenthesis (usually for and if constructs) and are handled by the parser as if all the lines in the block are only one line with concatenated commands. The full block is readed, all variable read operations removed and replaced with the value inside the variables, and then the full block, with no variable references inside is executed.

目前的执行时间上有块内变量无读取操作中,只有其初始值,因此,分配给一个变​​量的块内的任何值不能同一块内检索,因为没有任何读操作

At execution time there are no read operation on variables inside the block, only its initial values, so, any value assigned to a variable inside the block can not be retrieved inside the same block, as there isn't any read operation.

所以,在这个code

So, in this code

set "test=before"
if defined test (
    set "test=after"
    echo %test%
)

后的第一个设置执行,块(即如果命令,所有的code封闭在其括号)将被解析并转换成

after the first set is executed, the block (the if command and all the code enclosed in its parenthesis) will be parsed and converted into

if defined test (
    set "test=after"
    echo before
)

显示错误的价值。

showing the "wrong" value.

的常用方法来对付它是使用延迟扩展。它可以让你改变,在需要的地方,语法读取%VAR%的变量!无功! ,指明该读操作不能在分析时,直到执行命令删除,但延迟分析器

The usual way to deal with it is to use delayed expansion. It will allow you to change, where needed, the syntax to read the variable from %var% into !var!, indicating to the parser that the read operation must not be removed at parse time, but delayed until the command is executed.

setlocal enabledelayedexpansion
set "var="
set "var=test" & echo !var!

现在第三行被转换在分析时,以

The now third line is converted at parse time to

set "var=test" & echo !var!

是的,变量引用不会被删除。读操作延迟,直到回声指令被执行,当变量的值已经改变了。

yes, the variable reference is not removed. The read operation is delayed until the echo command will be executed, when the value of the variable has been changed.

所以

%VAR%是将在分析时被替换变量引用

%var% is a variable reference that will be replaced at parse time

!无功!是将在执行时被​​替换变量引用

!var! is a variable reference that will be replaced at execution time

%X X 单个字符,通常是一个替换参数,将举行被interated当前元素的变量。由它自己的本性,会在执行时进行扩展。用一个百分号语法在命令行中使用。内部批处理文件百分号需要进行转义,并指可替换参数的语法是 %% X

%x with x a single character is usually a for replaceable parameter, a variable that will hold the current element being interated. By its own nature, will be expanded at execution time. The syntax with a single percent sign is used at command line. Inside batch files the percent sign need to be escaped and the syntax to refer to the replaceable parameters is %%x

这篇关于一组语句不会出现在我的批处理文件工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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