|
plp626
银牌会员
钻石会员
积分 2278
发帖 1020
注册 2007-11-19
状态 离线
|
『楼 主』:
本人寒假在学校,一直在线,欢迎交流,有问必答
本人寒假在学校,一直在线,欢迎交流,有问必答
|
|
2008-1-18 13:18 |
|
|
raythunder
初级用户
积分 47
发帖 30
注册 2007-12-18
状态 离线
|
『第
2 楼』:
有时间过来看看你.
[ Last edited by raythunder on 2008-1-18 at 02:05 PM ]
|
|
2008-1-18 13:37 |
|
|
plp626
银牌会员
钻石会员
积分 2278
发帖 1020
注册 2007-11-19
状态 离线
|
『第
3 楼』:
谢谢,兄多来逛逛,人多了才热闹!!
|
|
2008-1-19 18:18 |
|
|
plp626
银牌会员
钻石会员
积分 2278
发帖 1020
注册 2007-11-19
状态 离线
|
『第
4 楼』:
想问一下论坛的会员或游客,有没有华夏黑客联盟的会员,我没邀请码,不能注册,能给个吗?万分感谢
|
|
2008-1-23 01:56 |
|
|
qiuqiu123456
初级用户
积分 51
发帖 25
注册 2007-12-22 来自 河南
状态 离线
|
『第
5 楼』:
for我一直搞不懂啊!用for /?也看不懂什么意思,LZ能不能再详细的帮我解释解释,万分感谢啊!;)
|
|
2008-2-1 20:44 |
|
|
abcd
银牌会员
积分 1436
发帖 739
注册 2007-10-11
状态 离线
|
『第
6 楼』:
Quote: | Originally posted by plp626 at 2008-1-23 01:56 AM:
想问一下论坛的会员或游客,有没有华夏黑客联盟的会员,我没邀请码,不能注册,能给个吗?万分感谢 |
|
华夏论坛大年三十晚会开放注册
|
|
2008-2-1 20:47 |
|
|
plp626
银牌会员
钻石会员
积分 2278
发帖 1020
注册 2007-11-19
状态 离线
|
『第
7 楼』:
真的吗?abcd兄谢谢了.
Quote: | Originally posted by qiuqiu123456 at 2008-2-1 08:44 PM:
for我一直搞不懂啊!用for /?也看不懂什么意思,LZ能不能再详细的帮我解释解释,万分感谢啊!;) |
|
请问那里你看不懂?
for 共3个参数/r /d /f加上一个缺省的算4种用法,
其中/f参数又有三种用法,
我现在不可能把每个都给你讲一遍,即使全说了你也没印象,
你给个具体的我给你详细说.
我不是很通,很底层原理我不懂.
你也可以中英文帮助结合着看,英文的说的比较到位.
|
|
2008-2-1 21:25 |
|
|
dhlmdsnw
初级用户
积分 127
发帖 59
注册 2008-1-29
状态 离线
|
|
2008-2-2 01:07 |
|
|
qiuqiu123456
初级用户
积分 51
发帖 25
注册 2007-12-22 来自 河南
状态 离线
|
『第
9 楼』:
因为是菜鸟,都看不懂.呵呵,FOR /?又理解不了,
|
|
2008-2-2 13:58 |
|
|
plp626
银牌会员
钻石会员
积分 2278
发帖 1020
注册 2007-11-19
状态 离线
|
『第
10 楼』:
Quote: | 8楼的我已经回你了,我只会batch,而batch处理字符恰恰是弱项. |
|
Quote: | Originally posted by qiuqiu123456 at 2008-2-2 01:58 PM:
因为是菜鸟,都看不懂.呵呵,FOR /?又理解不了, |
|
哪我真晕!我把自家宝贝全拿出来了,你再不行我没折了,先把英文状态下的for帮助翻译过来,翻译一遍后你就吃透了一半,再多实践,应该没问题,
下面代码你练练手,其实系统自带的帮助是最有用的,因为方便嘛.
@echo off
title 正在导出cmd帮助信息到cmdhelp目录内, 稍等...
md cmdhelp||(pause&exit)
chcp 437>nul&call :help E
graftabl 936>nul&call :help C
cd cmdhelp
set mark=───────────────────────────────────
::英文+中文>合并
for %%i in (*.E) do (
echo.>>%%i &echo %mark%>>%%i
copy %%~ni.E+%%~ni.C EC_%%~ni.txt>nul
echo %mark%>>EC_%%~ni.txt &echo.>>EC_%%~ni.txt
)
::删除临时文件
del *.c;*.e
title 已完成.
pause
goto :eof
rem ----------------- subprocess -------------------
:help
for /f %%i in ('help^|findstr "^[A-Z]"') do help %%i>>cmdhelp\%%i.%1
goto :eof
rem -------------------- end ---------------------- 下面是我从网上收藏的宝贝:
FOR [/d|/r|/l|f] [[drive:]path] ["options"] %variable IN (set) DO command [command-parameters]
Conditionally perform a command several times.
syntax-FOR-Files
FOR %%parameter IN (set) DO command
syntax-FOR-Files-Rooted at Path
FOR /R [[drive:]path] %%parameter IN (set) DO command
syntax-FOR-Folders
FOR /D %%parameter IN (folder_set) DO command
syntax-FOR-List of numbers
FOR /L %%parameter IN (start,step,end) DO command
syntax-FOR-File contents
FOR /F ["options"] %%parameter IN (filenameset) DO command
FOR /F ["options"] %%parameter IN ("Text string to process") DO command
syntax-FOR-Command Results
FOR /F ["options"] %%parameter IN ('command to process') DO command
The operation of the FOR command can be summarised as...
Take a set of data
Make a FOR Parameter %%G equal to some part of that data
Perform a command (optionally using the parameter as part of the command).
Repeat for each item of data
If you are using the FOR command at the command line rather than in a batch program, specify %parameter instead of %%parameter.
FOR Parameters
The first parameter has to be defined using a single character, I tend to use the letter G.
e.g. FOR %%G IN ...
In each iteration of a FOR loop, the IN ( ....) clause is evaluated and %%G set to a different value
If this results in a single value then %%G is set = to that value and the command is performed.
If this results in a multiple values then extra parameters are implicitly defined to hold each. These are automatically assigned in alphabetical order %%H %%I %%J ...(implicit parameter definition)
For example
FOR /F %%G IN ("This is a long sentence") DO @echo %%G %%H %%J
will result in the output
This is long
You can of course pick any letter of the alphabet other than %%G.
%%G is a good choice because it does not conflict with any of the pathname format letters (a, d, f, n, p, s, t, x) and provides the longest run of non-conflicting letters for use as implicit parameters.
G > H > I > J > K > L > M
Other Environment variables
Environment variables within a FOR loop are expanded at the beginning of the loop and won't change until AFTER the end of the DO section.
The following example counts the files in the current folder, but %count% always returns 1:
@echo off
SET count=1
FOR /f "tokens=*" %%G IN ('dir /b') DO (
echo %count%:%%G
set /a count+=1)
To make this work correctly we must force the variable %count% to be evaluated during each iteration, using the CALL :subroutine mechanism:
@echo off
SET count=1
FOR /f "tokens=*" %%G IN ('dir /b') DO (call :s_do_sums "%%G")
GOTO :eof
:s_do_sums
echo %count%:%1
set /a count+=1
GOTO :eof
Nested FOR commands
FOR commands can be nested FOR %%G... DO (for %%U... do ...)
when nesting commands choose a different letter for each part. you can then refer to both parameters in the final DO command.
If Command Extensions are disabled, the FOR command will only support the basic syntax with no enhanced variables:
FOR %%parameter IN (set) DO command [command-parameters]
"Those who cannot remember the past are condemned to repeat it" - George Santayana
Related Commands:
FOR - Loop through a set of files in one folder
FOR /R - Loop through files (recurse subfolders)
FOR /D - Loop through several folders
FOR /L - Loop through a range of numbers
FOR /F - Loop through items in a text file
FOR /F - Loop through the output of a command
FOR (default)
Conditionally perform a command on several files.
Syntax
FOR %%parameter IN (set) DO command
Key
command : The command to carry out, including any
command-line parameters.
%%parameter : A replaceable parameter:
e.g. in a batch file use %%G
(on the command line %G)Examples
Copy a single file
FOR %%G IN (MyFile.txt) DO copy %%G d:\backups\
Copy a list of several files
FOR %%G IN (Myfile.txt SecondFile.txt) DO copy %%G d:\backups\
Copy a single file
FOR %%G IN (c:\program^ files\MyFile.txt) DO copy %%G d:\backups\
In the line above the source folder has spaces that need to be escaped with ^
Although wildcards can be used an alternative method of processing files is to process the output of the command 'DIR /b'
This can be useful when you want to use some of the other DIR options like sorting.
FOR /R
Loop through files (Recurse subfolders)
Syntax
FOR /R [[drive:]path] %%parameter IN (set) DO command
Key
drive:path : The folder tree where the files are located.
FOR /D
Conditionally perform a command on several folders
Syntax
FOR /D [/r] %%parameter IN (folder_set) DO command
Key
folder_set : A set of one or more folders. Wildcards may be used.
command : The command to carry out, including any
command-line parameters.
%%parameter : A replaceable parameter:
in a batch file use %%G (on the command line %G)
/r : Recurse into subfoldersExamples
List separately each subfolder that is one level below the current folder
FOR /D %%G in (*.*) DO Echo ***Listing contents of %%G*** & dir /s "%%G"
FOR /L
Conditionally perform a command for a range of numbers
Syntax
FOR /L %%parameter IN (start,step,end) DO command
Key
start : The first number
step : The amount by which to increment the sequence
end : The last number
command : The command to carry out, including any
command-line parameters.
%%parameter : A replaceable parameter:
in a batch file use %%G (on the command line %G)So (20,-5,10) would generate the sequence (20 15 10)
(1,1,5) would generate the sequence 1 2 3 4 5
Examples
FOR /L %%G IN (1,1,5) DO echo %%G
Non-numeric lists can use a standard FOR command:
FOR %%G IN (Sun Mon Tue Wed Thur Fri Sat) DO echo %%G
FOR /F
Loop command: against a set of files - conditionally perform a command against each item.
Syntax
FOR /F ["options"] %%parameter IN (filenameset) DO command
FOR /F ["options"] %%parameter IN ("Text string to process") DO command
Key
options:
delims=xxx The delimiter character(s) (default = a space)
skip=n A number of lines to skip at the beginning of the file.
(default = 0)
eol=; Character to indicate a comment (end of line)
tokens=n Specifies which numbered items to read from each line
(default = 1)
usebackq Specify `back quotes`:
- Use double quotes to quote long file names in filenameset.
- Use single quotes for 'Text string to process'
(useful if the text string contains double quotes)
Filenameset : A set of one or more files. Wildcards may be used.
If (filenameset) is a period character (.) then FOR will
loop through every file in the folder.
command : The command to carry out, including any
command-line parameters.
%%parameter : A replaceable parameter:
in a batch file use %%G (on the command line %G)FOR /F processing of each text file consists of reading the file one line of text at a time and then breaking the ine up into individual items of data or 'tokens'. The DO command is then executed with the parameter(s) set to the token(s) found.
By default, /F breaks up the line at each blank space, and any blank lines are skipped.
You can override this default parsing behavior by specifying the "options" parameter. The options must be contained within "quotes"
Within a FOR loop the visibility of FOR variables is controlled via SETLOCAL EnableDelayedExpansion
The ` backquote character is just below the ESC key on most keyboards, the usebackq option is not available in NT4 or earlier.
Tokens
tokens=2,4,6 will cause the second, fourth and sixth items on each line to be processed
tokens=2-6 will cause the second, third, fourth, fifth and sixth items on each line to be processed
tokens=* will cause all items on each line to be processed
tokens=3* will cause the 3rd and all subsequent items on each line to be processed
Each token specified will cause a corresponding parameter letter to be allocated.
If the last character in the tokens= string is an asterisk, then additional parameters are allocated for all the remaining text on the line.
Delims
More than one delimiter may be specified so a string like 'abcd+efg+hijk+lmno;pqr;stu+vwzyz' can be broken up using "delims=;+".
You can use any character as a delimiter, but they are case sensitive.
If you don't specify delims it will default to "delims=<tab><space>"
Notice that some text editors will enter the TAB character as a series of spaces, specifying more than one delimiter has been known to cause problems with some data sets.
Examples
Extracting data from this text file:
12-AUG-06,DEPOSIT,450,23,55
14-JAN-07,WITHDRAWAL,285,122
03-FEB-07,DEPOSIT,200
FOR /F "tokens=1,3* delims=," %%G IN (myfile.txt) DO @echo %%G %%H %%I
The command above will split each line into tokens delimited by a comma as shown below.
token1 token3 *
= All the rest
%%G %%H %%I
12-AUG-06 DEPOSIT 450 23,55
14-JAN-07 WITHDRAWAL 285 122
03-FEB-07 DEPOSIT 200
%%G is declared in the FOR statement and the %%H and %%I are implicitly declared via the tokens= option. You can specify up to 26 tokens via the tokens= line, provided this does not cause an attempt to declare a parameter higher than the letter 'Z'.
FOR parameter names are global, so in complex scripts which call one FOR statement from within another FOR statement you can refer to both sets of parameters. You cannot have more than 26 parameters active at any one time.
Parse a text string:
A string of text will be treated just like a single line of input from a file, the string must be enclosed in double quotes (or single quotes with usebackq).
Echo just the date from the following string
FOR /F "tokens=4 delims=," %%G IN ("deposit,$45.50,23.7,12-AUG-07") DO @echo Date paid %%G
Parse the output of a command:
FOR /F %%G IN ('"C:\program Files\command.exe"') DO ECHO %%G
Parse the contents of a file:
FOR /F "usebackq tokens=1,2* delims=," %%G IN ("C:\My Documents\my textfile.txt") DO ECHO %%G
FOR /F "tokens=1,2* delims=," %%G IN (C:\MyDocu~1\mytex~1.txt) DO ECHO %%G
Filenameset
To specify an exact set of files to be processed, such as all .MP3 files in a folder including subfolders and sorted by date - just use the DIR /b command to create the list of filenames ~ and use this variant of the FOR command syntax.
Unicode
Many of the newer commands and utilities (e.g. WMIC) output text files in unicode format, these cannot be read by the FOR command which expects ASCII.
To convert the file format use the TYPE command.
set : A set of one or more files. Wildcards may be used.
If (set) is a period character (.) then FOR will
loop through every folder.
command : The command to carry out, including any
command-line parameters.
%%parameter : A replaceable parameter:
in a batch file use %%G (on the command line %G)This command walks down the folder tree starting at [drive:]path, and executes the DO statement against each matching file.
If the [drive:]path are not specified they will default to the current drive:path.
Example:
To delete every .bak file in every subfolder starting at C:\temp
FOR /R C:\temp\ %%G IN (*.bak) DO del %%G
Loop command: against the results of another command.
Syntax
FOR /F ["options"] %%parameter IN ('command_to_process') DO command
Key
options:
delims=xxx - The delimiter character(s)
(default = a space)
skip=n - A number of lines to skip at the beginning.
(default = 0)
eol=; - character to indicate a comment (end of line)
tokens=n - specifies which numbered items to
read from each line
(default = 1)
usebackq - under Windows 2000 (and greater) the usebackq option
specifies that an alternative set of FOR command
delimiters are to be used:
- a `command_to_process` is placed in BACK quotes
instead of 'straight' quotes
(see the FOR /F filename syntax for more)
command_to_process : The output of the 'command_to_process' is
passed into the FOR parameter.
command : The command to carry out, including any
command-line parameters.
%%parameter : A replaceable parameter:
in a batch file use %%G (on the command line %G)FOR /F processing of a command consists of reading the output from the command one line at a time and then breaking the line up into individual items of data or 'tokens'. The DO command is then executed with the parameter(s) set to the token(s) found.
By default, /F breaks up the command output at each blank space, and any blank lines are skipped.
You can override this default parsing behavior by specifying the "options" parameter. The options must be contained within "quotes"
Tokens
tokens=2,4,6 will cause the second, fourth and sixth items on each line to be processed
tokens=2-6 will cause the second, third, fourth, fifth and sixth items on each line to be processed
tokens=* will cause all items on each line to be processed
tokens=3* will cause the 3rd and all subsequent items on each line to be processed
Each token specified will cause a corresponding parameter letter to be allocated.
If the last character in the tokens= string is an asterisk, then additional parameters are allocated for all the remaining text on the line.
Delims
More than one delimiter may be specified so a string like 'abcd+efg+hijk+lmno;pqr;stu+vwzyz' can be broken up using "delims=;+".
You can use any character as a delimiter, but they are case sensitive.
If you don't specify delims it will default to "delims=<tab><space>"
Notice that some text editors will enter the TAB character as a series of spaces, specifying more than one delimiter has been known to cause problems with some data sets.
Examples:
To ECHO from the command line, the name of every environment variable.
FOR /F "delims==" %G IN ('SET') DO @Echo %GThe same command with usebackq (Windows 2000 and above)
FOR /F "usebackq delims==" %G IN (`SET`) DO @Echo %GTo put the Windows Version into an environment variable
@echo off
::parse the VER command
FOR /F "tokens=4*" %%G IN ('ver') DO SET _version=%%G
:: show the result
echo %_version%List all the text files in a folder
FOR /F "tokens=*" %%G IN ('dir /b C:\docs\*.txt') DO echo %%G
FOR /F "tokens=*" %%G IN ('dir/b ^"c:\program files\*.txt^"') DO echo %%GIn the example above the long filename has to be surrounded in "quotes"
these quotes have to be escaped using ^
The "tokens=*" has been added to match all parts of any long filenames returned by the DIR command.
Although the above is a trivial example, being able to set %%G equal to each long filename in turn could allow much more complex processing to be done.
More examples can be found on the Syntax / Batch Files pages and the other FOR pages below.
关于for变量扩充,不用看多少,重在应用
给你个代码对for变量扩充的理解一次性掌握:
@echo off&mode con lines=40 cols=100&title 变量扩充举例
::(这里是用%0为例,在for语句中可以用相应的变量代换0)
echo 用百分号0得到本批处理文件的带双引号的自身完整路径:
echo %0
echo.
echo 用 百分号~0
echo 打印结果: %~0
echo 用 百分号~f0
echo 打印结果: %~f0
echo 用 百分号~d0
echo 打印结果: %~d0
echo 用 百分号~p0
echo 打印结果: %~p0
echo 用 百分号~n0
echo 打印结果: %~n0
echo 用 百分号~x0
echo 打印结果: %~x0
echo 用 百分号~s0
echo 打印结果: %~s0
echo 用 百分号~a0
echo 打印结果: %~a0
echo 用 百分号~t0
echo 打印结果: %~t0
echo 用 百分号~z0
echo 打印结果: %~z0
echo 用 百分号~$PATH:0
echo 打印结果: %~$PATH:0
echo ----------------可以用组合修饰符来得到多重结果:
echo.
echo 用 百分号~dp0
echo 打印结果: %~dp0
echo 用 百分号~nx0
echo 打印结果: %~nx0
echo 用 百分号~fs0
echo 打印结果: %~fs0
echo 用 百分号~dp$PATH:0
echo 打印结果: %~dp$PATH:0
echo 用 百分号~ftza0
echo 打印结果: %~ftza0
pause>nul
|
|
2008-2-2 17:56 |
|
|
qiuqiu123456
初级用户
积分 51
发帖 25
注册 2007-12-22 来自 河南
状态 离线
|
『第
11 楼』:
谢谢LZ,看过你发的英文和中文的解释,恍然大悟,万分感谢。
|
|
2008-2-2 23:47 |
|
|
qq43142691
中级用户
积分 327
发帖 152
注册 2007-5-4
状态 离线
|
|
2008-2-3 23:10 |
|
|
qq43142691
中级用户
积分 327
发帖 152
注册 2007-5-4
状态 离线
|
『第
13 楼』:
我测试了好多次了。。是可以用了。。1楼发的是错误的。。后面补回来的才是修正的。。可以用的啊。。只是想精简下的了
|
|
2008-2-4 07:23 |
|
|
moniuming
银牌会员
永远的菜鸟
积分 1335
发帖 574
注册 2007-11-27 来自 广西
状态 离线
|
『第
14 楼』:
向你请教个问题
我用以下的代码查找文件夹,出现问题了,好奇怪
原来的代码是要找到文件夹后把文件复制进去的,但是出错了,然后精简为下面的代码后发现出问题了,有的时候显示的是要找的文件夹,但是有的时候查找的结果却是要查找的文件夹下所有的子文件夹
@echo off
set /p aa=请输入要查找的文件夹:
for %%a in (c d e f) do (
for /f "delims=" %%i in ('dir /s /ad /b %%a:\"%aa%" 2^>nul') do (
echo "%%i"
)
)
echo 按任意键退出...
pause>nul
下面是查找wxp文件夹的结果:
请输入要查找的文件夹:wxp
"d:\wxp\INF"
"d:\wxp\system"
"d:\wxp\system32"
"d:\wxp\twain_32"
"d:\wxp\system32\DRIVERS"
"d:\wxp\twain_32\snphv71"
"d:\wxp\twain_32\Topro"
"d:\wxp\twain_32\VM301b"
"d:\wxp\twain_32\ZSCAM"
按任意键退出...
下面是查找system32的结果:
请输入要查找的文件夹:system32
"c:\WINDOWS\system32"
"d:\RECYCLER\S-1-5-21-220523388-1004336348-725345543-1003\Dd14\system32"
"d:\WXP\system32"
望能给予解释,非常感谢!
|
|
2008-2-17 01:57 |
|
|