Bat intercept string (for command) recommended collection _dos/bat

Source: Internet
Author: User
Tags abs extend

There are some variables in the for command, their usage many novice friends do not know much, today to explain their usage!

First, list all the variables for the variable:
~i-Remove any quotes ("), Extend%I
%~fi-Will%I Extend to a fully qualified path name
%~di-Will%I only Extend to a drive letter
%~PI-Will%I only Extend to a path
%~ni-Will%I only Extend to a filename
%~xi-Will%I only Extend to a file name extension
%~si-The path to the extension contains only the short name
%~ai-Will%I File attributes extended to files
%~ti-Will%I Date/time of extension to file
%~zi-Will%I Size of file extended to
~ $PATH: I-Find directories listed in PATH environment variables, and%I Extended
To the first fully qualified name found. If the environment variable name
is not defined, or the file is not found, this key combination expands to the
Empty string


We can see that each row has a capital letter "I", and this I is actually the variable we are bringing in for, and what the variable name we are substituting for the statement is.
For example: for/f%%z in (' Set ') do @echo%%z
The variable name we're substituting here is Z, so we're going to change that I to Z, for example%~fi to%~FZ.
As for the front of the%~p such content is the grammar!


Good to start the explanation:

~i-Remove any quotes ("), Extend%I
The role of this variable as his description, delete quotes!
Let's look at this example:

First, we create a name for temp.txt text file on the desktop, enter it inside
"1111
2222 "
"3333"
44 "44
Then create a bat file with the following code:

for/f "delims="%%i in (temp.txt) do @echo%%~i
Pause

After execution, we look at the cmd echo as follows:
1111
2222 "
3333
44 "44
In contrast to the previous temp.txt, we'll find that the quotes in the first and third lines are gone, and that's the effect of removing the quotes ~i!
Remove the quote rule as follows (add!)
1, if the string at both end of the quotation marks, then delete the end of the quotation marks;
2, if there is no quotation mark at the end of the string, delete the first quotation mark;
3. If there is a quotation mark in the middle of the string, or if there is only quotation marks at the end, it is not deleted.

%~fi-Will%I Extend to a fully qualified path name
See Example:
Keep the code in any place, I'll put it on the table.
for/f "delims=="%%i in (' dir/b ') do @echo%%~fi
Pause

After execution, the contents are displayed as follows
C:\Documents and settings\administrator\ Desktop \test.bat
C:\Documents and settings\administrator\ Desktop \test.vbs
When I change the%%~fi in the code directly to%%i
for/f "delims=="%%i in (' dir/b ') do @echo%%i
Pause

This will be displayed after execution
Test.bat
Test.vbs

By contrast, it is easy to see that there is no path, which is "to%I Extended to a fully qualified pathname "role
That is, if the content of the%i variable is a filename, he will print the absolute path of the file, not just the single
Single Print a file name, oneself to start the experiment to know!

%~di-Will%I only Extend to a drive letter
See Example:
The code is as follows, I still put on the desktop execution!
for/f "delims=="%%i in (' dir/b ') do @echo%%~di
Pause
After execution I cmd shows the following
C:
C:
I have two files on my desktop Test.bat,test.vbs,%%~di function is, if the variable%%i content is a file or directory name, he will put his file
or the disk symbol where the directory is printed!


%~PI-Will%I only Extend to a path
This usage is the same as above, he only prints the path does not print the file name
for/f "delims=="%%i in (' dir/b ') do @echo%%~pi
Pause

I will not hit the results, we copy the code to see the results, the following are a few of the use of the code to come out, we see the results!


%~ni-Will%I only Extend to a filename
Print file names only
for/f "delims=="%%i in (' dir/b ') do @echo%%~ni
Pause


%~xi-Will%I only Extend to a file name extension
Print only the extension of a file
for/f "delims=="%%i in (' dir/b ') do @echo%%~xi
Pause


%~si-The path to the extension contains only the short name
Print absolute Short file name
for/f "delims=="%%i in (' dir/b ') do @echo%%~si
Pause


%~ai-Will%I File attributes extended to files
Print the properties of a file
for/f "delims=="%%i in (' dir/b ') do @echo%%~ai
Pause


%~ti-Will%I Date/time of extension to file
Print the date the file was created
for/f "delims=="%%i in (' dir/b ') do @echo%%~ti
Pause


%~zi-Will%I Size of file extended to
Size of the print file
for/f "delims=="%%i in (' dir/b ') do @echo%%~zi
Pause


~ $PATH: I-Find directories listed in PATH environment variables, and%I Extended
To the first fully qualified name found. If the environment variable name
is not defined, or the file is not found, this key combination expands to the
Empty string
This is the last one, and it's not the same, I said alone!
Let's start by creating a Temp.txt file on the desktop that says that.
C:\Windows
C:\Windows\System32
C:\windows\teett

Then save the code as a batch and put it on the desktop.
for/f "delims=="%%i in (temp.txt) do @echo%%~ $PATH: I
Pause

After execution, CMD displays the following contents
C:\WINDOWS
C:\WINDOWS\system32
ECHO is in the open state.

Why is there a line of C:\windows\teett?
We enter the echo%path% directly in CMD to show C:\WINDOWS\system32; C:\WINDOWS; C:\WINDOWS\System32\Wbem
%%~ $PATH: I look for the content in the text to match the value of this path variable, and if the contents are the same as the path variable, print it out
If it's not the same, show a blank line!

can only apply to path variables, other variables not, if you want to compare a lot of values, you can try to give the value to the path variable, I did not test!

Well, for the variable is introduced here!

Here are some examples to share:

BAT batch string substitution function and string intercept function

1, the string substitution is good, the symbol says this, now said%path:str1=str2% the above grammar means: Replaces the str1 in the string variable%path% with the STR2

Copy Code code as follows:

@echo off
@color 02
Set all=demo123
Echo%all%
Set vat=%all:1=aaa%
Echo%vat%
---The result is demoaaa23.
Pause

2, the string interception ********************************************** interception function Unified Syntax format:%a:~[m[,n]]% ********************************* *************
From M to intercept a n-length string, M is an offset (the default is 0), n is the Intercept length (default is all), n can be a negative number

%a:~0,n% corresponds to the function left (a,n) takes the left-hand n bit%a:~-m% corresponds to the function right (A,M) takes the right-hand m bit%a:~m,n% corresponds to the function mid (a,m+1,n) takes the n bit from the m+1 bit%a:~m,-n % is equivalent to the function mid (A,m+1,len (a)-m-n), starting at the m+1 bit, to the reciprocal n+1 bit%a:~m% equal to the function mid (A,m+1,len (a)-m) or right (A,len (a)-m), starting at the m+1 bit and taking all of the right-hand side.
%a:~[m[,n]]%
%123:~0,1%

Result 1

Bat Intercept string

Copy Code code as follows:

@echo off
Set str=123456789
Echo's first character is:%str:~0,1%
The echo first two characters are:%str:~0,2%
The Echo header 5 characters are:%str:~0,5%
Echo removes the string after the last character:%str:~0,-1%
Echo removes the last 3 characters after the string is:%str:~0,-3%
The 4th character of Echo is:%str:~3,1%
Echo 4th and the following 3 characters are:%str:~3,4%
The last character of Echo is:%str:~-1%
The last character of Echo is:%str:~-1,1%
The last two characters of Echo are:%str:~-1,2%
The 4th character of Echo's countdown is:%str:~-4,1%
The 4th and subsequent characters of ECHO are:%str:~-4%
The 4th and subsequent 1 characters of Echo are:%str:~-4,2%
The 4th and subsequent 2 characters of Echo are:%str:~-4,3%
Pause

To illustrate this issue, I am here to take the batch processing character, to do a further explanation, I hope to enlighten the novice
As follows:
Echo%var:~n,k%
Here's a description of each parameter: "%var", which is the string from which we want to intercept characters. "~" to take the word

Character marker (as I understand it), "n" We interpret it as a pointer, "K" we interpret as an offset address. (Note

: Pointers and offset addresses are zero-based.
We still use the example of NAMEJM moderator to do the following explanation:

Copy Code code as follows:

@echo off
Set str=123456789
REM defines a str string of 123456789
Echo's first character is:%str:~0,1%
The REM pointer is 0, and the offset address is 1, starting at the No. 0 bit, and taking 1 bits
The echo first two characters are:%str:~0,2%
The REM pointer is 0, and the offset address is 2, starting at the No. 0 bit, and taking 2 bits
The Echo header 5 characters are:%str:~0,5%
The REM pointer is 0, and the offset address is 5, starting at the No. 0 bit, and taking 5 bits
Echo removes the string after the last character:%str:~0,-1%
rem when "K" is a negative value, we can understand this: start at the beginning of the pointer to take all the characters behind it, and then subtract
Back "abs (k) bit". So we can do the following to explain: start with the No. 0 bit of all the characters
is: 123456789 then subtracts the ABS (k) bit from the back, so the final result is: 12345678
Echo removes the last 3 characters after the string is:%str:~0,-3%
REM the sentence explains ibid. ↑
The last character of Echo is:%str:~-1%
rem Parameters "N," and "K" can all be the default, the default "N," can be understood as: starting from the ABS (k) bit all
The 4th and subsequent characters of ECHO are:%str:~-4%
REM Explanation ditto ↑
The last character of Echo is:%str:~-1,1%
When REM n is negative, it means that the character is intercepted from the beginning, and the K bit is taken (at which point N should start at 1)
The last character of Echo is:%str:~-1,2%
REM Explanation ditto ↑
The 4th character of Echo's countdown is:%str:~-4,1%
REM Explanation ditto ↑
The 4th and subsequent 1 characters of Echo are:%str:~-4,2%
REM Explanation ditto ↑
The 4th and subsequent 2 characters of Echo are:%str:~-4,3%
REM Explanation ditto ↑
Pause

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.