Preprocessing implementation code in Batch Processing

Source: Internet
Author: User

Reprinted from nxhujiee to final edit [preprocessing] In LJ_SunTB Batch Processing]
When there are too many threads, there are too many threads, too many threads.

If you are familiar with the processing mechanism of the delimiters "^", you can continue to read it.
For more information, see the articles about delimiters.

I. What exactly does preprocessing do?

In my experience, preprocessing is to replace variable values and process special symbols. Which operation should I perform first? I think it is necessary to replace the variable value first. There are three reasons:

1. Logically speaking

Set var = 2 & echo % var %
For a statement like this, if special symbols are processed first, the symbol "&" must be processed first, while "&" is used to connect two commands, in this way, the row should be understood as two sentences, so what should we do with variable latency. Here it should be
Assign values to the variable var first, and then process the special symbol "&".

2. view the running results

Copy codeThe Code is as follows: @ echo off
Set var = ^>
Echo % var %
Pause

This "set var = ^>" is also preprocessed. After preprocessing, the var value is "^> ".
In this example, the output result is ">", so it can be proved that the system first replaces the value of the variable with "^>" and then processes the special symbol "^ ".

3. From the perspective of Variable SubstitutionCopy codeThe Code is as follows: @ echo off
Set ^ & var = hero
Echo % & var %
Pause

Result: "hero" is displayed"
This also indicates that the replacement of variables is prior to the processing of special symbols.

2. How does preprocessing work after variable delay is started?

In my opinion, if the statement contains an English exclamation mark "!", It will be preprocessed twice. Otherwise, it will be preprocessed once. Because the delimiters are special, we can use this symbol to illustrate several examples.

(1)Copy codeThe Code is as follows: @ echo off
Echo! ^>
Setlocal enabledelayedexpansion
Echo! ^>
Pause

The two echo statements have different results. The following is an analysis:
For the first echo statement, the variable delay is not enabled. When preprocessing is performed, the sentence is preprocessed as "echo! ^> ", Which is the output result. It can be seen that preprocessing is performed only once.

For the second echo statement, variable delay is enabled at this time, because there is "!" Yes. First, perform a preprocessing to get "echo! ^> ", And then obtain" echo ^>.
The reason why no exclamation mark is output is that the variable delay is enabled and the exclamation mark is changed to a special symbol.

(2)Copy codeThe Code is as follows: @ echo off
Setlocal enabledelayedexpansion
Set var = hero
Echo! Var!
Pause

Like the "echo! Var !" Instead of being preprocessed, It is preprocessed twice. You can understand the following code.Copy codeThe Code is as follows: @ echo off
Setlocal enabledelayedexpansion
Set var = hero
Echo! Var! ^>
Pause

The running result is "hero ^> ". Let's analyze it. During the first preprocessing, Var !", Therefore, the special symbols are processed without replacing the variable values. After the processing, it becomes "echo! Var! ^> "; Perform preprocessing again,
Replace "! Var !" After processing, it becomes "echo hero ^> ".

(3)
Let's take a look at the absence of an exclamation mark in the statement when the variable delay is enabled.Copy codeThe Code is as follows: @ echo off
Echo ^>
Setlocal enabledelayedexpansion
Echo ^>
Pause

@ Echo off
Set var = hero
Echo % var % ^>
Setlocal enabledelayedexpansion
Echo % var % ^>
Pause

So if there is no "!", No second processing will be performed.

(4)
For !! Special symbols are processed before the variable is replaced.

Example,Copy codeThe Code is as follows: @ echo off
Setlocal enabledelayedexpansion
Set ^ & var = hero
Echo! & Var!
Pause

The running result of this Code is incorrect.

Example,Copy codeThe Code is as follows: @ echo off
Setlocal enabledelayedexpansion
Set var = ^ &
Echo! Var!
Pause

The running result of this Code is correct.

(5)
Since all symbols must be processed, the % Type and !! Is it the same process?

(1) The example in can already explain the problem, but I still have examples to prove it.

Example,Copy codeThe Code is as follows: @ echo off
Echo "^ "!!
Setlocal enabledelayedexpansion
Echo "^ "!!
Pause

For the % type, the delimiters between double quotation marks are not processed during symbol processing. !! Type.

Iii. call issues

(1)
Call and delimiters

Example,Copy codeThe Code is as follows: @ echo off
Set/p var = Echo "% var %"
Call echo "% var %"
Pause

The content in hero.txt contains eight delimiters: ^

The result is:
"^"
"^"
Press any key to continue...

Are the results unexpected? We know that the system does not process double quotation marks during preprocessing, which means that the call command doubles the number of delimiters. It seems that the call command and the delimiters are a bit ambiguous ".

Example,Copy codeThe Code is as follows: @ echo off
Set/p var = Echo % var %
Call echo % var %
Pause

In this example, the var variable has eight "^" values. When you run "call echo % var %, first, replace % var % with ^, and then change the symbol to ^, the call Command doubles the number of delimiters to eight, and then
Call preprocessing, so the result is 4 "^ ".

This explains why the following code displays four "^ ".Copy codeThe Code is as follows: @ echo off
Call echo ^
Pause

(2)
Call and other special characters
The "other special characters" mentioned here mainly refer to "&", ">", "|", and so on.

Here, allow me to customize two nouns:
Main preprocessing process: the general name of the system preprocessing process, including the % Type and !! Type.
Subpreprocessing process: a general term for the pre-processing process caused by the call command.
"Other special characters" are recognized by the system during the main preprocessing process, while
There is a problem with the identification of these symbols in the process.

Example,Copy codeThe Code is as follows: @ echo off
Call echo hero! ^ & Pause
Pause

In this example, after the main preprocessing process, & is recognized as a common character, and the recognition of the symbol & in the secondary preprocessing process will cause problems. As mentioned in the command line reference, do not use pipelines and redirection symbols in the call command. (This is not to say that the call statement cannot
These symbols cannot be passed as parameters to the call command .)

This also shows that the sentence structure (one or more statements) and functions (from the targeted output or other statements) of a row are determined in the main preprocessing process.

All of the above content is my personal opinion. It is for reference only because there is no official documentation.

So what are the practical uses of the above content? I think I can write more personalized code by understanding the above principles. It can also be used as a disguise in practice.

Copy codeThe Code is as follows: @ echo off
Set ^ & = setlocal enabledelayedexpansion
Set ^ hero = ^ & p
Set ^ au = ^ au
Set ^ = blind eye
% & %
Set ^ se = ^ se!
Echo % ^ %! % ^ Hero %! Au % ^ se %

How about this code?

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.