DOS Batch Advanced Tutorial Chapter III. variable _dos/bat in the for command

Source: Internet
Author: User
Tags extend reserved

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 Expands to the first fully qualified name found. If the environment variable name is not defined, or if the file is not found, the key combination expands to an 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:

One, ~i-Remove any quotes ("), Extend%I

The role of this variable as his description, delete quotes!
Let's look at this example:
First, create a temporary document Temp.txt, which reads as follows
"1111
"2222"
3333 "
"4444" 44
"55" 55 "55

You can create a bat file code as follows:

@echo off
echo ^ "1111>temp.txt
echo" 2222 ">>temp.txt
echo 3333^" >>temp.txt
echo " 4444 "44>>temp.txt
echo ^" "55>>temp.txt

rem" To create a temporary file, note that the quotation marks to be separated by the escape character ^, the redirection symbol do not leave blank for

/F "delims="%%i in (temp.txt) do echo%%~i
pause
del temp.txt

After execution, we look at the cmd echo as follows:
#字符串前的引号被删除了
#字符串首尾的引号都被删除了
3333 "#字符串前无引号, followed by quotes reserved
4444 "#字符串前面的引号删除了, while the middle of the quotes are reserved
#字符串前面的引号删除了, while the middle quotation marks are reserved
Please press any key to continue ...

In contrast to the previous temp.txt, we'll find that the quotes in lines 1th, 2, and 5 are gone, which is 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.

Second,%~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 following is displayed:
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 "the role is that if the content of the%i variable is a filename, he will be the absolute path of the file to print out, rather than just print a file name, you start the experiment will know!

Third,%~di-only will%I 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!

Iv.%~pi-Only%I 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!

V.%~ni-will only%I Extend to a filename

Print file names only

for/f "delims=="%%i in (' dir/b ') do @echo%%~ni
pause

Vi.%~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

Vii.%~si-extended paths contain only short names

Print absolute Short file name

for/f "delims=="%%i in (' dir/b ') do @echo%%~si
pause

Eight,%~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

Ix.%~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

Ten,%~zi-will%I Size of file extended to

Size of the print file

for/f "delims=="%%i in (' dir/b ') do @echo%%~zi
pause

The above example "delims==" can be changed to "delims=", that is, do not separator

Xi. ~ $PATH: I-Find the directory listed in the PATH environment variable, and%I Expands to the first fully qualified name found. If the environment variable name is not defined, or if the file is not found, the key combination expands to an empty string

This is the last one, and it's not the same, I said alone!

Then save the code as a batch and put it on the desktop.

@echo off
for/f ' delims= '%%i in (' notepad.exe ') do echo%%~ $PATH: I
pause

Tornado Supplement: The above code shows the result as C:\WINDOWS\system32\notepad.exe

He meant to search for notepad.exe files in the path specified in the path variable, and if Notepad.exe would print out his absolute path, print a mistake!
Well, for the variable is introduced here!

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.