DOS 遍历目录及子目录,删除特定名称文件夹或文件

明亮 posted @ 2011年12月23日 11:23 in 【软件工程】 , 9593 阅读
本文发表于:http://fml927.is-programmer.com

1、遍历某个文件夹及其子文件夹目录,删除全部名为CVS的目录:

      进入CMD界面执行以下两句,第一句进入特定待查找的目录,第二句删除当前目录及子目录下全部名为CVS的文件夹

cd "E:\3_DSP"

for /R  %s in (.,*) do  rd /q /s %s\CVS

参考:http://blog.sina.com.cn/s/blog_517d1cb00100se0v.html

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

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

Image转载

标签:

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"

)

  • 无匹配
  • 无匹配
fxz_abc 说:
2011年12月25日 13:21

(.,*)
.,后面那是个什么字符?

Avatar_small
明亮 说:
2012年8月03日 10:55

@fxz_abc: 可以把它复制到记事本中看,会看到那是个星号。网页显示不清楚。


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter