FOR syntax learning
一、代码及注释
1 @echo off 2 3 dir 4 g: 5 cd .\tmpWorkspaces 6 7 8 9 10 11 12 13 14 rem for -- learning 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 43 44 rem expends '%I' 45 46 47 goto explanation 48 49 In addition, substitution of FOR variable references has been enhanced. 50 You can now use the following optional syntax: 51 52 %~I - expands %I removing any surrounding quotes (") 53 %~fI - expands %I to a fully qualified path name 54 %~dI - expands %I to a drive letter only 55 %~pI - expands %I to a path only 56 %~nI - expands %I to a file name only 57 %~xI - expands %I to a file extension only 58 %~sI - expanded path contains short names only 59 %~aI - expands %I to file attributes of file 60 %~tI - expands %I to date/time of file 61 %~zI - expands %I to size of file 62 %~$PATH:I - searches the directories listed in the PATH 63 environment variable and expands %I to the 64 fully qualified name of the first one found. 65 If the environment variable name is not 66 defined or the file is not found by the 67 search, then this modifier expands to the 68 empty string 69 70 The modifiers can be combined to get compound results: 71 72 %~dpI - expands %I to a drive letter and path only 73 %~nxI - expands %I to a file name and extension only 74 %~fsI - expands %I to a full path name with short names only 75 %~dp$PATH:I - searches the directories listed in the PATH 76 environment variable for %I and expands to the 77 drive letter and path of the first one found. 78 %~ftzaI - expands %I to a DIR like output line 79 80 In the above examples %I and PATH can be replaced by other valid 81 values. The %~ syntax is terminated by a valid FOR variable name. 82 Picking upper case variable names like %I makes it more readable and 83 avoids confusion with the modifiers, which are not case sensitive. 84 85 :explanation 86 87 88 rem == %%~ni: print file name only. 89 rem FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~ni 90 91 rem == %%~fnxazi: f: full path name; n:file name; x:file extension name; 92 rem a:file attributes; z:file size; i:delete quotes(") 93 FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~fnxazi
二、参考资料
1、help(environment: cmd.exe) G:\tmpWorkspaces>for /?
2、bat 批处理 for 入门 https://www.jb51.net/article/134025.htm
标签:bat,file,%%,win,echo,learning,rem,expands,name From: https://www.cnblogs.com/lnlidawei/p/16929656.html