In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces what are the variables in the FOR command in DOS batch processing. It is very detailed and has a certain reference value. Friends who are interested must read it!
First, list all the variables in FOR:
~ I-remove any quotation marks ("), extend% I
% ~ fI-extends% I to a fully qualified pathname
% ~ dI-extends% I to only one drive letter
% ~ pI-extends% I to only one path
% ~ nI-extends% I to only one file name
% ~ xI-extends% I to only one file extension
% ~ sI-the extended path contains only short names
% ~ aI-extends% I to the file attributes of the file
% ~ tI-extends% I to the date / time of the file
% ~ zI-extends% I to the size of the file
% ~ $PATH:I-find the directory listed in the path environment variable and extend% I to the first fully qualified name found. If the environment variable name is not defined, or if the file is not found, this key combination is extended to an empty string
We can see that each line has an uppercase letter "I". This I is actually the variable we brought in in FOR. We will write here what the variable name is in our FOR statement.
For example: FOR / F% z IN ('set') DO @ echo% z
The name of the variable we substituted here is z, then we have to change that I to z, for example,% ~ fI to% ~ fz
As for the previous% ~ p, this kind of content is grammar!
Okay, let's start with:
1. ~ I-remove any quotation marks (") and extend% I
The function of this variable is just as he explained, delete the quotation marks!
Let's look at this example:
First, set up a temporary file temp.txt, which is as follows
"1111
"2222"
3333 "
"4444" 44
"55" 55
You can create a BAT file with the following code:
@ echo offecho ^ "1111 > temp.txtecho" 2222 "> temp.txtecho 3333 ^" > temp.txtecho "4444" 44 > > temp.txtecho ^ "55" 55 "55 > temp.txtrem creates a temporary file. Note that unpaired quotation marks should be escaped ^. Do not leave spaces FOR / F" delims= "% I IN (temp.txt) DO echo% ~ ipausedel temp.txt before redirection.
After execution, we see the echo of CMD as follows:
The quotation marks before the # string have been deleted
The quotation marks at the beginning and end of the # string have been deleted
3333 "# string is preceded by quotation marks, followed by quotation marks reserved
The quotation marks before the 4444 "44 # string are deleted, while the quotation marks in the middle are retained.
The quotation marks before the 55 "55" 55 # string are deleted, while the quotation marks in the middle are retained.
Please press any key to continue. . .
Compared with the previous temp.txt, we will find that the quotation marks on lines 1, 2, and 5 have disappeared, which is the purpose of deleting quotation marks ~ I!
The rules for deleting quotation marks are as follows (added by brother BAT!)
1. If there are quotation marks at the beginning and end of the string, delete the quotation marks at the beginning and the end of the string
2. If there are no quotation marks at the end of the string, delete the quotation marks at the beginning of the string
3. If there are quotation marks in the middle of the string, or if there are only quotation marks at the end, they are not deleted.
2.% ~ fI-extends% I to a fully qualified pathname
Look at the example:
Save the code anywhere, and I'll put it on the desktop here.
FOR / F "delims=="% I IN ('dir / b') DO @ echo% ~ fipause
After execution, the following is displayed:
Test.bat
Test.vbs
By comparison, we can easily see that there is no path, which is the function of "extending% I to a fully qualified path name", that is, if the content of the% I variable is a file name, he will print out the absolute path where the file is located, instead of just printing a file name, you will know under your own hands-on experiment!
3.% ~ dI-extend% I to only one drive letter
Look at the example:
The code is as follows, I will put it on the desktop for execution!
FOR / F "delims=="% I IN ('dir / b') DO @ echo% ~ dipause
After execution, my CMD shows the following
C:
C:
There are only two files on my desktop test.bat,test.vbs,%%~di function is that if the content of the variable% I is a file or directory name, he will put his file
Or the disk symbol where the directory is located is printed out!
4.% ~ pI-extend% I to only one path
This usage is the same as above, it only prints the path, not the file name.
FOR / F "delims=="% I IN ('dir / b') DO @ echo% ~ pipause
I will not play the results, you see the results, the following several are such a usage, the code is given, you see the results!
5.% ~ nI-extends% I to only one file name
Print only the file name
FOR / F "delims=="% I IN ('dir / b') DO @ echo% ~ nipause
6.% ~ xI-extend% I to only one file extension
Print only the file extension
FOR / F "delims=="% I IN ('dir / b') DO @ echo% ~ xipause
7.% ~ sI-the extended path contains only short names
Print an absolutely short file name
FOR / F "delims=="% I IN ('dir / b') DO @ echo% ~ sipause
8.% ~ aI-extends% I to the file attributes of the file
Properties of the print file
FOR / F "delims=="% I IN ('dir / b') DO @ echo% ~ aipause
9.% ~ tI-extends% I to the date / time of the file
Date on which the print file was created
FOR / F "delims=="% I IN ('dir / b') DO @ echo% ~ tipause
10.% ~ zI-extends% I to the size of the file
The size of the print file
FOR / F "delims=="% I IN ('dir / b') DO @ echo% ~ zipause
The "delims==" in the above example can be changed to "delims=", that is, no delimiter
% ~ $PATH:I-find the directory listed in the path environment variable and extend% I to the first fully qualified name found. If the environment variable name is not defined, or if the file is not found, this key combination is extended to an empty string
This is the last one, which is different from the ones above. I'll talk about it alone!
Then save the code as a batch and put it on the desktop.
@ echo offFOR / F "delims="% I IN ("notepad.exe") DO echo% ~ $PATH:ipause
Add: the above code shows the result as C:\ WINDOWS\ system32\ notepad.exe
He means to search the notepad.exe file in the path specified in the PATH variable, and if there is a notepad.exe, it will print out his absolute path, and if not, print an error!
All right, that's all for FOR's variables!
These are all the contents of the article "what are the variables in the FOR command in DOS batches?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.