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

来源:互联网 发布:网络推广是做什么 编辑:程序博客网 时间:2024/06/01 08:07
Syntax:
   FOR/R [[drive:]path] %variable IN (set) DO command[command-parameters]

Description:
   Walks the directory treerooted at [drive:]path, executing the FOR statement in eachdirectory of the tree. 
  • If no directory specification is specified after /R then thecurrent directory is assumed. 
  • If set is just a single period (.) character then it will justenumerate the directory tree.
  • If set is just a single asterisk (*) character then it willjust enumerate the files.
  • If set is a single period (.) character followed by a singleasterisk (*) character then it will enumerate the sub-foldersfirstly and then files under the sub-folder recursively.
  • If set is a single asterisk (*) character followedby a single period (.) characterthen it will enumerate the files firstly then the sub-folderrecursively.
  • Wildcards characters asterisk(*) and period(.) can be repeated and the loop will also repeatedappropriately.
  • Question mark(?) is also supported and usually used to filterinterested files or sub-folders under specified folder or thecurrent folder.

Example:
@echo off
REM recursively print absolute path ofsub-folders and files under drive D:
for /R "D:\" %%s in (.,*) do(
  echo%%s
  sleep0.3
)

REM recursivelyprint absolute path of onlysub-folders under current folder
for /R %%s in (.) do (
  echo%%s
  sleep0.3
)

REM recursivelyprint absolute path of only filesunder current folder
for /R %%s in (*) do (
  echo%%s
  sleep0.3
)

REM recursivelyprint absolute path of filesunder current folder and sub-folders but only for those files withname matching pattern "list?.xul" (i.e. list0.xul orlistA.xul).

for /R %%s in (list?.xul) do(

  echo%%s

  sleep0.3

)


 

REM do the same with the above butwithout sleeping

for /R %%s in (list?.xul) do echo%%s


 

The above examples just perform echo command. However, you cando more complex commands as needed. For example, the followingexample is used to perform the following operations on all *.xulfile 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:\ProgramFiles\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"

)

 

flvtool2.exe给某文件夹中flv文件批量插入关键帧

@echo off
for /R "H:\topntweb\topnt\topnt\"  %%s in (*.flv) do (
"H:\topntweb\topnt\topnt\FilesList\Exe\VideoTools\flvtool2.exe" -U %%s
)
pause

原创粉丝点击