Batch Processing: Groove exclamation point and Variable Delay Extension

Source: Internet
Author: User

Let's talk about Variable Delay extension. Of course, you can see the variable delay ExtensionArticleSo I will give a brief introduction here. Let's take a look at a batch processing:

 
SetSTR = test

If% STR % = test (
SetSTR = another test
Echo% STR %
)

The aboveCodeSegment is extremely simple. assign a value to STR to determine whether the value is test. If yes, assign another value to another test and then display the value of Str.

As a normal person's mind, the display here must be another test, but it is not actually, it is still test, why? Because: when Windows interprets and executes this code segment, after encountering the parentheses after the if statement, it is treated as one statement rather than two statements. Therefore, in the second statement, % STR % will be replaced with its current value test. The above code is equivalent to the following code:

SetSTR = test

If% STR % = test (
SetSTR = another test
Echo Test: Note:
)

Therefore, the output is test.

In this way, the programming flexibility is greatly reduced, so M $ thought of a workground method, that is, variable latency, which is very simple. See the following code:

@ Echo Off
SetlocalEnabledelayedexpansion::Note:

SetSTR = test

If% STR % = test (
SetSTR = another test
Echo! Str!::Note:
Echo% STR %::Differences
)

What will be output now? If you try it out, the first line outputs another test, and the second line outputs test.

Now let's explain that setlocal enabledelayedexpansion is used to enable variable delay, which tells the interpreter that when a composite statement is encountered, it should not be processed as a statement at the same time, but should be interpreted one by one. But it must be used now! Str! To reference the variable. If % STR % is still used to reference the variable, it does not work.

Well, the variable delay extension is explained, at least this is the variable delay extension I know.

========================================== I am proud of the separation line, if you only want to know what the variable delay extension is, now, we can see that it is OK ================================

Today, variable delay extension and exclamation point have been bothering me for a long time, so now I want to talk about them.

Try the following code snippet:

 
@ Echo Off

SetlocalEnabledelayedexpansion

SetSTR = test !!!

Echo% STR %

Enable variable delay, assign values to STR, and output Str. Therefore, the output is test !!! . But Windows tells us that it is wrong again. The output is test, and the exclamation point is eaten by windows.

Of course, if you comment out the line for enabling variable delay, these lines can work normally, so enabling Variable Delay affects our normal operations, however, I still have no idea why this is the case. Only M $ knows why.

So, disable variable delay,ProgramWhat should I do if I need variable latency to implement some logic? Two methods:

1. temporarily disable variable delay:

 
@ Echo Off

SetlocalEnabledelayedexpansion

::Do something here

SetlocalDisabledelayedexpansion: Disable Variable Delay

SetSTR = test !!!

Echo% STR %

SetlocalEnabledelayedexpansion

::Continue...

To process the exclamation point, temporarily disable the variable delay and enable the variable after processing. The exclamation point can be output normally.

2. Set:

 
@ Echo Off

SetSTR = test !!!

SetlocalEnabledelayedexpansion

Echo! Str!: Note:

Set the variable before enabling the variable delay, but note that when using the variable, you need to use the exclamation point reference. The exclamation point can also be output normally.

Finally, I posted a piece of code indicating why it hurts me (variable latency is naturally open ):

SetServer = % ~ 1
SetUsername = % ~ 2
SetPassword = % ~ 3
Echo%Date%, %Time% [Info] Report Server address: [% server %], Username: [% username %], password: [******]

Rs.exe-I "publishreports. RSS"-s "% server %"-U "% username %"-P "% password %"-l 600

In order not to echo the password, I output a large string *. During the test on my machine, all the characters in my password are normal and pass. However, there is an exclamation point in the password of QA...

The key is that the rs.exe of report serverof M $ is called to upload the template. It throws an exception from cocould not connect. Therefore, I naturally thought of a problem with the service of the Report Server, but I haven't found any problem with the Report Server after checking it for a long time... Who will think of TMD is someone else's password with an exclamation point, who will think of TMD is caused by a conflict with the so-called delay variable, and who will think of it, I am clever enough to show a large string of hard code asterisks so that the password value is not displayed...

So many coincidences together, who can not hurt...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.