Preprocessing implementation code _dos/bat in batch processing

Source: Internet
Author: User

Reprinted from Nxhujiee final edit lj_suntb batch processing "preprocessing"
━━━━━━━━━━━━━━━━━━━━━━━━━━

If you are familiar with the processing mechanism of the caret character "^" then you can read it, no
Please refer to the relevant articles of the caret character first.

First, what is the pretreatment to do?

According to my experience, preprocessing is to do with the substitution of variable values and the processing of special symbols. What do I do first, I think, is to replace the value of the variable first. There are three reasons:

1, from the logical view

Set Var=2&echo%var%
Similar to such a statement, if the first to deal with special symbols, it is bound to deal with the symbol "&", and "&" is used to connect two commands, so that the line is taken for granted by two sentences, then we also want to delay what the variable. This is supposed to be
Assign the variable var first, and then handle the special symbol "&".

2, from the results of the operation to see

Copy Code code as follows:

@echo off
Set var= ^^ ^>
Echo%var%
Pause

The phrase "set var= ^^ ^>" is preprocessed first, and the value of Var after preprocessing is "^>".
The output of this example 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 variable replacement to see
Copy Code code as follows:

@echo off
Set ^&var=hero
Echo%&var%
Pause

Results: Show "Hero"
This also shows that the substitution of variables precede the processing of special symbols.

Second, start the variable delay after the pretreatment is how to do?

My view is this: if there is an English exclamation mark in the statement "!" Will be preprocessed two times, the other is still preprocessed once. Because the character is more special, so this symbol to write a few examples to illustrate.

A
Copy Code code as follows:

@echo off
Echo, ^^ ^^ ^>.
Setlocal enabledelayedexpansion
Echo, ^^ ^^ ^>.
Pause

The results of the two ECHO statements are different. Here's an analysis:
For the first echo statement, the variable delay is not turned on, and the sentence is preprocessed as "Echo! ^^ >", which is the result of the output. This shows that preprocessing was only carried out once.

For the second ECHO statement, the variable delay is turned on, due to the "!" exists, the first preprocessing gets "Echo! ^^ >", and the result is "echo ^>".
The reason that the exclamation mark is not output is because the variable delay is turned on, and the exclamation marks become special symbols.


Two
Copy Code code as follows:

@echo off
Setlocal enabledelayedexpansion
Set Var=hero
Echo!var!
Pause

Like "Echo!var!" here. Instead of being preprocessed, it was preprocessed two times. Look at the code below to get a sense of it.
Copy Code code as follows:

@echo off
Setlocal enabledelayedexpansion
Set Var=hero
echo!var! ^^ ^^ ^>
Pause

The result of the run is: "Hero^>". We will analyze the first preprocessing, because of "!var!", so first of all, do not replace the value of a special symbol for processing, after processing became "Echo!var! ^^ >";
It's time to replace "!var!." , and after processing it became "Echo hero^>".


Three
Let's take a look at the case where the English exclamation mark is not present in the statement when the variable delay is opened.
Copy Code code as follows:

@echo off
echo ^^ ^^ ^>
Setlocal enabledelayedexpansion
echo ^^ ^^ ^>
Pause

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

How, that is to say, if there is no "!" There would be no second treatment.


Four
For!! Type, the processing of special symbols is done before the variable is replaced.

Cases
Copy Code code as follows:

@echo off
Setlocal enabledelayedexpansion
Set ^&var=hero
Echo!&var!
Pause

This code is wrong to run the result.

Cases
Copy Code code as follows:

@echo off
Setlocal enabledelayedexpansion
Set var=^&
Echo!var!
Pause

This code runs the correct result.


Five
Since all have to deal with the symbols, then the%-percent and!! Will the symbolic processing of the type be the same process?

The examples in (a) are already illustrative, but I have an example to prove it.

Cases
Copy Code code as follows:

@echo off
echo "^^ ^^ ^^ ^^"!!
Setlocal enabledelayedexpansion
echo "^^ ^^ ^^ ^^"!!
Pause

For the%-percent type, in the symbolic processing, do not deal with double quotes between the caret character; Type is the opposite.


Third, call leads to some of the problems

A
Call and caret characters

Cases
Copy Code code as follows:

@echo off
set/p Var=echo "%var%"
Call echo "%var%"
Pause

The contents of the Hero.txt are 8 characters: ^^ ^^ ^^ ^^

The result:
"^^^^^^^^"
"^^^^^^^^^^^^^^^^"
Please press any key to continue ...

Is the result somewhat unexpected? We know that the system does not process the caret character between the two quotes during preprocessing, which means that the call command adds the number of characters that followed. It seems that the call command and hyphenation characters are really a little ambiguous.

Cases
Copy Code code as follows:

@echo off
set/p Var=Echo%var%
Call Echo%var%
Pause

The value of Var in this example is 8 "^", when running "Call Echo%var%", replace the%var% with the ^ ^^ ^^ ^^ ^, and then turn to ^ ^^ ^ once again, as the call command causes the number of hyphenation characters to increase by one time to 8, and then
Call itself is preprocessed, so the result is 4 "^".

This will explain why the following code shows 4 "^".
Copy Code code as follows:

@echo off
Call, call, call echo ^^ ^^ ^^ ^^
Pause


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

Here, allow me to customize two nouns:
The main pretreatment process: The system itself the general name of the pretreatment process, including the%-percent type and!! Type.
Secondary preprocessing process: The general name of the preprocessing process caused by the call command.
"Other special characters" are identified by the system during the main preprocessing, while in the secondary preprocessing
There is a problem in the identification of these symbols during the process.

Cases
Copy Code code as follows:

@echo off
Call Echo Hero!^&pause
Pause

In this example, the main preprocessing process,& is recognized as a common character, and in the secondary preprocessing process, the identification of symbol & will cause problems. As mentioned in the command line reference-do not use pipes and redirection symbols in the call command. (This is not to say that the call statement cannot make the
Use those symbols, but these symbols cannot be passed to the call command as arguments. )

This also explains to some extent whether the sentence structure (one or more) of a line statement and the function (from directed output or others) are determined during the main preprocessing process.

All of the above, just my personal opinion, because there is no official document support, so for reference only.

So what are the practical uses of all these things? I think, understand the above reason can write more personalized code, but also can be used as a camouflage in practice.

Copy Code code as follows:

@echo off
Set ^&=setlocal enabledelayedexpansion
Set ^^ ^^ ^hero= ^^ ^^ ^&p
Set ^au= ^^ ^au
Set ^^ ^^ ^^ ^^ ^= Camouflage
%&%
Set ^^ ^^ ^se= ^^ ^se!
echo% ^^ ^^%!% ^^ hero%!au%^se%

How about this piece of code can you see?

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.