DOS命令:用For命令遍历文件夹
(2011-04-02 21:29:38)
标签:
doscommandbatchprogramming |
分类: 技术探究 |
Syntax:
Description:
- If no directory specification is specified after /R then the
current directory is assumed.
- If set is just a single period (.) character then it will just enumerate the directory tree.
- If set is just a single asterisk (*) character then it will just enumerate the files.
- If set is a single period (.) character followed by a single asterisk (*) character then it will enumerate the sub-folders firstly and then files under the sub-folder recursively.
- If set is a single asterisk (*) character followed
by
a single period (.) character then it will enumerate the files firstly then the sub-folder recursively. - Wildcards characters asterisk(*) and
period (.) can be repeated and the loop will also repeated appropriately. - Question mark(?) is also supported and usually used to filter interested files or sub-folders under specified folder or the current folder.
Example:
@echo off
REM recursively print absolute path of
sub-folders and files under drive D:
for /R "D:\" %%s in (.,*) do
(
)
REM recursively
print absolute path of only
sub-folders under current folder
for /R %%s in (.) do (
)
REM recursively
print absolute path of only files
under current folder
for /R %%s in (*) do (
)
REM recursively
print absolute path of files
under current folder and sub-folders but only for those files with
name matching pattern "list?.xul" (i.e. list0.xul or
listA.xul).
for /R %%s in (list?.xul) do
(
)
REM do the same with the above but
without sleeping
for /R %%s in (list?.xul) do echo
%%s
The above examples just perform echo command. However, you can
do more complex commands as needed. For example, the following
example is used to perform the following operations on all *.xul
file under the current folder and sub-folders
- open Firefox with *.xul file
- open *.xul files with notepad
- sleep for 5 seconds
- forcibly kill firefox and notepad
@echo off
set FIREFOX_ROOT="C:\Program
Files\Mozilla Firefox\"
set FIREFOX="firefox.exe"
for /R %%s in (*.xul) do (
)
前一篇:IQ, 面试 题目汇总
后一篇:哲理:三个旅行者

加载中…