加载中…
个人资料
Bruce
Bruce
  • 博客等级:
  • 博客积分:0
  • 博客访问:66,455
  • 关注人气:1
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

DOS命令:用For命令遍历文件夹

(2011-04-02 21:29:38)
标签:

dos

command

batch

programming

分类: 技术探究
Syntax:
   FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]

Description:
   Walks the directory tree rooted at [drive:]path, executing the FOR statement in each directory of the tree. 
  • 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 (
  echo %%s
  sleep 0.3
)

REM recursively print absolute path of only sub-folders under current folder
for /R %%s in (.) do (
  echo %%s
  sleep 0.3
)

REM recursively print absolute path of only files under current folder
for /R %%s in (*) do (
  echo %%s
  sleep 0.3
)

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 (
  echo %%s
  sleep 0.3
)

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 (
  start /D%FIREFOX_ROOT% FIREFOX -chrome %%s
  start notepad %%s
  sleep 5
  taskkill /f /fi "imagename eq firefox.exe"
  taskkill /f /fi "imagename eq notepad.exe"
)

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有