Section 3 of the bat tutorial (variables in the for command)

Source: Internet
Author: User

Section 3 of the bat tutorial (variables in the for command)

ForCommandThere are some variables,Their usage is not well understood by many new users.,I will explain their usage to you today !
FirstForAll the variables are listed :
~ I-Delete any quotation marks("), Expansion % I
% ~ Fi-Set% IExtend to a fully qualified path name
% ~ Di- Only% IExtend to a drive letter
% ~ Pi- Only% IExtend to a path
% ~ Ni- Only% IExtend to oneFileName
% ~ Xi- Only% IExtend to a file extension
% ~ Si- The extension path contains only short names.
% ~ Ai- Set% IFile Attributes extended to files
% ~ Ti- Set% IDate extended to file/Time
% ~ Zi- Set% IExtend To File Size
% ~ $ Path: I- Find the directory that is listed in the PATH environment variable and set% IExtension
The first fully qualified name found. If the environment variable name
This key combination is extended
Null String
We can see that each row has an uppercase letter."I ",ThisIIn fact, we areForVariable,WeForWhat is the variable name entered by the statement?,What to write here? .
For example : For/F % Z in ('set') Do @ echo % Z
The variable name we entered here isZThen we needIChangeZ,For example% ~ FiChange % ~ FZ
As for the previous% ~ PThis is the syntax. !
Good start :
~ I-Delete any quotation marks("), Expansion % I
The function of this variable is described in his explanation.,Delete quotation marks !
Let's look at this example. :
First, we createTemp.txtText Files,Enter the content
1111
2222"
"3333"
44 "44
ThenBatFileCodeAs follows: :
For/F "delims =" % I in (temp.txt) Do @ echo % ~ I
Pause
After execution,Let's seeCMDThe echo of is as follows: :
1111
2222"
3333
44 "44
And beforeTemp.txtContent Comparison in,We will find that the quotation marks for the first and third rows have disappeared.,This is to delete quotation marks.~ IThe role !
The rules for deleting quotation marks are as follows:(BatBrother supplement !)
1If both the start and end of a string have quotation marks, the quotation marks at the beginning and end are deleted;
2 If no quotation marks exist at the end of the string, the quotation marks at the beginning of the string are deleted;
3 If there are quotation marks in the middle of the string, or there are only quotation marks at the end, it is not deleted.
% ~ Fi- Set% IExtend to a fully qualified path name
Look at the example :
Save the code anywhere,Let me put it on the desktop. .
For/F "delims =" % I in ('dir/B ') Do @ echo % ~ Fi
Pause
The following content is displayed after execution:
C: \ Documents and Settings \ Administrator \ Desktop \ Test. bat
C: \ Documents and Settings \ Administrator \Desktop \ Test. vbs
When I put% ~ FiDirectly change % I
For/F "delims =" % I in ('dir/B ') Do @ echo % I
Pause
The content is displayed after execution.
Test. bat
Test. vbs
Comparison,We can easily see that there is no path.,This is"Set% IExtend to a fully qualified path name"Function
That is, if% IIf the variable content is a file name,And print out the absolute path of the file.,Not just a single
Print a file name,You can find it in your own manual experiment. !
% ~ Di-Only% IExtend to a drive letter
Look at the example :
The Code is as follows:,I still put it on the desktop for execution !
For/F "delims =" % I in ('dir/B ') Do @ echo % ~ Di
Pause
After execution, ICMDShown in
C:
C:
I have two files on my desktop.Test. bat, test. vbs, % ~ DiThe role is,If the variable% IIs a file or directory name.,He will take this file
Or print the disk symbol of the directory. !
% ~ Pi-Only% IExtend to a path
This usage is the same as above,It only prints the path and does not print the file name.
For/F "delims =" % I in ('dir/B ') Do @ echo % ~ Pi
Pause
I won't hit the results.,Copy the code and check the result.,The following are all examples of usage:,Code,Let's take a look at the results. !
% ~ Ni-Only% IExtend to a file name
Print only the file name
For/F "delims =" % I in ('dir/B ') Do @ echo % ~ Ni
Pause
% ~ Xi- Only% IExtend to a file extension
Print file extensions only
For/F "delims =" % I in ('dir/B ') Do @ echo % ~ XI
Pause
% ~ Si- The extension path contains only short names.
Print absolute short file name
For/F "delims =" % I in ('dir/B ') Do @ echo % ~ Si
Pause
% ~ Ai- Set% IFile Attributes extended to files
Print file attributes
For/F "delims =" % I in ('dir/B ') Do @ echo % ~ AI
Pause
% ~ Ti- Set% IDate extended to file/Time
Print the date when the file was created
For/F "delims =" % I in ('dir/B ') Do @ echo % ~ Ti
Pause
% ~ Zi- Set% IExtend To File Size
Print File Size
For/F "delims =" % I in ('dir/B ') Do @ echo % ~ Zi
Pause
% ~ $ Path: I- Find the directory that is listed in the PATH environment variable and set% IExtension
The first fully qualified name found. If the environment variable name
This key combination is extended
Null String
This is the last,Different from those above,Let me talk about it separately. !
Save the code as a batch,On the Desktop .
For/F "delims =" % I in ('net.exe ') Do @ echo % ~ $ Path: I
Pause
He meansPathSearch in the specified path in the VariableNet, exeFile.Net.exeThe absolute path of the file will be printed out, and an error will be printed if no path exists!
Okay.,The variable is introduced here. !
ByHeadache

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.