There are some variables in the FOR command, and many novices don’t know much about their usage. Today I will explain their usage to you!
List all the variables of FOR first:
~I -Remove any quotes (") and expand %I
%~fI
%~dI
%~pI
%~nI
%~xI
%~sI
%~aI
%~tI
%~zI
%~$PATH:I - Find the directory listed in the path environment variables and expand %I
To the first fully qualified name found. If the environment variable name
Undefined or file not found, this key combination will be extended to
Empty string
We can see that each line has a capital letter "I". This I is actually the variable we bring in FOR. What is the name of the variable we substitute in the FOR statement, and what is written here.
For example:FOR /F %%z IN ('set') DO @echo %%z
The variable name we substituted here is z, so we need to change that I to z, for example, %~fI to %~fz
As for the previous content like %~p, it is syntax!
OK to start explaining:
~I -Remove any quotes (") and expand %I
The function of this variable is as he stated, remove the quotes!
Let's look at this example:
First, we create a text file named as text on the desktop and enter these contents in it.
"1111
2222"
"3333"
44"44
Then create a BAT file code as follows:
FOR /F "delims=" %%i IN () DO @echo %%~i
pause
After execution, we look at the echo of CMD as follows:
1111
2222"
3333
44"44
Compare with the contents in the previous one and we will find that the quotes on the first and third lines have disappeared, which is the purpose of deleting the quotes ~i!
The rules for deleting quotes are as follows (Brother BAT!)
1. If there are quotes at the beginning and end of a string at the same time, delete the quotes at the beginning and end of a string;
2. If there are no quotes at the end of the string, delete the quotes at the beginning of the string;
3. If there are quotes in the middle of the string, or only quotes at the end, they will not be deleted.
%~fI
See examples:
Save the code anywhere, I will put it on the desktop here.
FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~fi
pause
After execution, the content is displayed as follows
C:\Documents and Settings\Administrator\Desktop\
C:\Documents and Settings\Administrator\Desktop\
When I change the %%~fi in the code directly to %%i
FOR /F "delims==" %%i IN ('dir /b') DO @echo %%i
pause
These contents will be displayed after execution
By comparison, we can easily see that there is no path, which is what "extend %I to a fully qualified pathname" does
That is, if the content of the %i variable is a file name, it will print out the absolute path where the file is located, not just a single one.
Print a file name alone and experiment with it yourself!
%~dI -Extend %I to only one drive letter
See examples:
The code is as follows, I will put it on the desktop to execute!
FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~di
pause
After execution, my CMD shows the following
C:
C:
I only have two files on my desktop, %%~di is, if the content of the variable %%i is a file or directory name, he will put it in this file
Or print out the disk symbol where the directory is located!
%~pI -Extend %I to only one path
This usage is the same as above, it only prints the path but not the file name
FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~pi
pause
I won’t be able to hit the results. Please copy the code to see the results yourself. The following are all usages. Give it the code and you can see the results yourself!
%~nI
Print only file names
FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~ni
pause
%~xI -Extend %I to only one file extension
Print only file extensions
FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~xi
pause
%~sI - The extended path only contains short names
Print absolute short file name
FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~si
pause
%~aI
Properties of the print file
FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~ai
pause
%~tI
Print the date of file creation
FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~ti
pause
%~zI
Print file size
FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~zi
pause
%~$PATH:I - Find the directory listed in the path environment variables and expand %I
To the first fully qualified name found. If the environment variable name
Undefined or file not found, this key combination will be extended to
Empty string
This is the last one, which is different from the above ones, I will talk about it separately!
Let's create a file on the desktop and write these contents in it
c:\windows
c:\windows\system32
c:\windows\teett
Then save the code as a batch and place it on the desktop.
FOR /F "delims==" %%i IN () DO @echo %%~$PATH:i
pause
After execution, the CMD displays the following content
c:\WINDOWS
c:\WINDOWS\system32
ECHO is on.
Why is this happening? There is another line of c:\windows\teett?
We directly enter echo %path% in CMD and will display C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem
%%~$PATH:i means looking for content matching the value of this PATH variable in the text. If the content inside is the same as the PATH variable, print it out.
If it is different, a blank line will be displayed!
It can only be applied to PATH variables, but other variables cannot. If you want to compare many values, you can try to assign the value to the PATH variable. I haven't tested it!
Okay, that's all about FOR's variables!
Here are some examples for you:
bat batch string replacement function and string intercept function
1. String replacement. Okay, when it comes to the symbol, now say %PATH:str1=str2% The above syntax means: replace str1 in the string variable %PATH% with str2
@echo off
@color 02
set ALL=demo123
echo %ALL%
set VAT=%ALL:1=aaa%
echo %VAT%
---The result is demoaaa23
pause
2. String interception ****************************************** The unified syntax format of the interception function is: %a:~[m[,n]]% *********************************************************
From mth, the string with n length intercepted, m is the offset (default is 0), n is the intercepted length (default is all), n can be a negative number
%a:~0,n% is equivalent to the function left(a,n) Take the n bit on the left %a:~-m% Equivalent to the function right (a,m) Take the m bit on the right %a:~m,n% is equivalent to the function mid(a,m+1,n) Starting from the m+1 bit %a:~m,-n% is equivalent to the function mid(a,m+1,len(a)-m-n), start from the m+1 bit, to the reciprocal n+1 bit %a:~m % Equivalent to the function mid(a,m+1,len(a)-m) or right(a,len(a)-m), start from the m+1 bit, and take the all on the right starting from the m+1 bit.
%a:~[m[,n]]%
%123:~0,1%
Results 1
bat intercept string
@echo off
set str=123456789
echo The first character is: %str:~0,1%
The first two characters of echo are: %str:~0,2%
The first 5 characters of echo are: %str:~0,5%
echo The string after removing the last character is: %str:~0,-1%
echo The string after removing the last 3 characters is: %str:~0,-3%
echo The fourth character is: %str:~3,1%
echo the 4th and the 3 characters after it are: %str:~3,4%
echo The last character is: %str:~-1%
The last character of echo is: %str:~-1,1%
echo The last two characters are: %str:~-1,2%
echo The fourth last character is: %str:~-4,1%
echo The 4th last character and after it is: %str: ~-4%
echo The 4th last and 1 character after it is: %str: ~-4,2%
echo The 4th last and the 2 characters after it are: %str: ~-4,3%
pause
To illustrate this issue, I will give a further explanation of the batch character collection here, hoping to inspire newbies.
as follows:
echo %var:~n,k%
Here we explain each parameter: "%var", that is, the string from which we want to intercept the characters." ~ "Take the character
The symbol identifier (this is how I understand it), "n" we understand it as a pointer, and "k" we understand it as an offset address.
: Pointer and offset addresses are both counted from zero)
Let's use the namejm moderator's example to explain:
@echo off
set str=123456789
rem defines a str string as 123456789
echo The first character is: %str:~0,1%
The rem pointer is 0 and the offset address is 1, that is, starting from the 0th bit, take 1 bit
The first two characters of echo are: %str:~0,2%
The rem pointer is 0 and the offset address is 2, that is, starting from the 0th bit, take 2 bits
The first 5 characters of echo are: %str:~0,5%
rem pointer is 0 and offset address is 5, that is, starting from bit 0, take 5 bits
echo The string after removing the last character is: %str:~0,-1%
When "k" is negative, we can understand it like this: start taking all the characters after it from the beginning of the pointer, and then subtracting it
The following is "abs(k) bit". So we can explain this sentence as follows: take all its characters from the 0th bit
It is: 123456789 and then subtract the abs(k) bit from the back, so the final result is: 12345678
echo The string after removing the last 3 characters is: %str:~0,-3%
rem This sentence is the same as above↑
echo The last character is: %str:~-1%
rem parameters "n," and "k" can be both default. When defaulting "n," it can be understood as: take all of them from the abs(k) bit
echo The 4th last character and after it is: %str: ~-4%
Rem Explanation
The last character of echo is: %str:~-1,1%
When rem n is a negative value, it means that the characters are intercepted from the following and the k bits are taken (at this time n should start from 1)
The last character of echo is: %str:~-1,2%
rem Explanation the same as above↑
echo The fourth last character is: %str:~-4,1%
rem Explanation the same as above↑
echo The 4th last and 1 character after it is: %str: ~-4,2%
rem Explanation the same as above↑
echo The 4th last and the 2 characters after it are: %str: ~-4,3%
rem Explanation the same as above↑
pause