FOR syntax learning
一、代码及注释
1 @echo off
2
3 dir
4 g:
5 cd .\tmpWorkspaces
6
7
8
9
10
11
12 rem -- FOR Learning --
13
14
15
16
17
18
19 rem == for /d d=directory; list names of directories in the given directories
20 rem for /d %%i in (g:\*) do echo %%i
21
22
23 rem == for /r r=recursive directory; list specified file names in current directory and its subdirectories.
24 rem for /r g:\ %%i in (*.exe) do echo %%i
25
26
27 rem == for /l l= a sequence digit; output a sequence of digits.
28 rem for /l %%i in (1,2,10) do echo %%i
29
30
31 rem == for /f f = file contents; deal with contets of specified files.
32 rem FOR /F "eol=; tokens=1,2* delims=,- " %%i in (g:\tmpWorkspaces\testfor.txt) do echo %%i %%j %%k
33 rem for /F "tokens=1,2,3,4* " %%i in (g:\tmpWorkspaces\testfor.txt) do echo %%i %%j %%k %%l
34 rem for /F "tokens=1,2,3,4*" %%i in (g:\tmpWorkspaces\testfor.txt) do echo %%i %%j %%k %%l
35 rem for /f "eol== tokens=1,2,3,4* " %%a in (g:\tmpWorkspaces\testfor.txt) do echo %%a %%b %%c %%d
36
37
38
39
40
41
42 rem ENHANCED
43
44
45
46
47 rem == enhanced-part-1
48
49
50 goto explanation
51
52 In addition, substitution of FOR variable references has been enhanced.
53 You can now use the following optional syntax:
54
55 %~I - expands %I removing any surrounding quotes (")
56 %~fI - expands %I to a fully qualified path name
57 %~dI - expands %I to a drive letter only
58 %~pI - expands %I to a path only
59 %~nI - expands %I to a file name only
60 %~xI - expands %I to a file extension only
61 %~sI - expanded path contains short names only
62 %~aI - expands %I to file attributes of file
63 %~tI - expands %I to date/time of file
64 %~zI - expands %I to size of file
65 %~$PATH:I - searches the directories listed in the PATH
66 environment variable and expands %I to the
67 fully qualified name of the first one found.
68 If the environment variable name is not
69 defined or the file is not found by the
70 search, then this modifier expands to the
71 empty string
72
73 The modifiers can be combined to get compound results:
74
75 %~dpI - expands %I to a drive letter and path only
76 %~nxI - expands %I to a file name and extension only
77 %~fsI - expands %I to a full path name with short names only
78 %~dp$PATH:I - searches the directories listed in the PATH
79 environment variable for %I and expands to the
80 drive letter and path of the first one found.
81 %~ftzaI - expands %I to a DIR like output line
82
83 In the above examples %I and PATH can be replaced by other valid
84 values. The %~ syntax is terminated by a valid FOR variable name.
85 Picking upper case variable names like %I makes it more readable and
86 avoids confusion with the modifiers, which are not case sensitive.
87
88 :explanation
89
90
91
92
93 echo enhanced-part-1
94 rem == %%~ni: print file name only.
95 rem FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~ni
96
97
98 rem == %%~fnxazi: f: full path name; n:file name; x:file extension name;
99 rem a:file attributes; z:file size; i:delete quotes(")
100 FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~fnxazi
101
102
103
104
105 rem == enhanced-part-2
106
107
108 goto nl
109
110 说明:这里是重点内容!
111
112
113 %I The %~ syntax is terminated by a valid FOR variable name('I'是for中‘声明的变量名).
114 %I 'I'是for中‘声明的变量名’;如果声明for声明的变量名是y;则扩展为'%~y';
115
116
117 'm'是for中声明的变量名;则有
118
119 扩展 功能
120
121 %~m - expands %m removing any surrounding quotes (")
122 %~fm - expands %m to a fully qualified path name
123 %~dm - expands %m to a drive letter only
124 %~pm - expands %m to a path only
125 %~nm - expands %m to a file name only
126 %~xm - expands %m to a file extension only
127 %~sm - expanded path contains short names only
128 %~am - expands %m to file attributes of file
129 %~tm - expands %m to date/time of file
130 %~zm - expands %m to size of file
131 ... ...
132
133
134 :nl
135
136
137
138
139 echo enhanced-part-2
140 rem == %%~ny: print file name only.
141 rem FOR /F "delims==" %%y IN ('dir /b') DO @echo %%~ny
142
143
144 rem == %%~fnxazy: f: full path name; n:file name; x:file extension name;
145 rem a:file attributes; z:file size; i:delete quotes(")
146 FOR /F "delims==" %%y IN ('dir /b') DO @echo %%~fnxazy
二、参考资料
1、help(environment: cmd.exe) G:\tmpWorkspaces>for /?
2、bat 批处理 for 入门 https://www.jb51.net/article/134025.htm
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章的版权归属于【原创作者】; 转载或引用时请【保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/16929656.html