dos下遍历目录和文件

===== 文件夹结构 =============================================
D:\test

---A Folder 1

  |-----A file 1.txt

  |-----A file 2.txt

  |-----A file 3.txt

---B Folder 2

  |-----B file 1.txt

  |-----B file 2.txt

  |-----B file 3.txt

  |---B Folder 3

     |-----B sub file 1.txt

     |-----B sub file 2.txt

     |-----B sub file 3.txt



================================================================
@echo off
set work_path=D:\test
D:
cd %work_path%

for /R %%s in (.,*) do (

  echo %%s

)

pause

结果

D:\test\.
D:\test\A Folder 1\.
D:\test\A Folder 1\A file 1.txt
D:\test\A Folder 1\A file 2.txt
D:\test\A Folder 1\A file 3.txt
D:\test\B Folder 2\.
D:\test\B Folder 2\B file 1.txt
D:\test\B Folder 2\B file 2.txt
D:\test\B Folder 2\B file 3.txt
D:\test\B Folder 2\B Folder 3\.
D:\test\B Folder 2\B Folder 3\B sub file 1.txt
D:\test\B Folder 2\B Folder 3\B sub file 2.txt
D:\test\B Folder 2\B Folder 3\B sub file 3.txt

================================================================

@echo off
set work_path=D:\test
D:
cd %work_path%

for /R %%s in (.) do (
  echo %%s
)
pause

结果 

D:\test\.
D:\test\A Folder 1\.
D:\test\A Folder 1\A file 1.txt
D:\test\A Folder 1\A file 2.txt
D:\test\A Folder 1\A file 3.txt
D:\test\B Folder 2\.
D:\test\B Folder 2\B file 1.txt
D:\test\B Folder 2\B file 2.txt
D:\test\B Folder 2\B file 3.txt
D:\test\B Folder 2\B Folder 3\.
D:\test\B Folder 2\B Folder 3\B sub file 1.txt
D:\test\B Folder 2\B Folder 3\B sub file 2.txt
D:\test\B Folder 2\B Folder 3\B sub file 3.txt



那么

for /R %%s in (.,*) do (

  echo %%s

)

for /R %%s in (.) do (

  echo %%s

)

的区别是什么呢?

在有cd %work_path% 的时候,这两个命令执行的结果是一样的,就像我们上面举的例子。但是

for /R %%s in (.,*) do (

  echo %%s

)

的批处理中没有cd %work_path% ,那么显示的将是这个批处理文件所在文件夹下面的遍历结果。


================================================================

@echo off
for /R "D:\test" %%s in (.) do (
  echo %%s
)
pause

结果

D:\test\.
D:\test\A Folder 1\.
D:\test\B Folder 2\.
D:\test\B Folder 2\B Folder 3\.

================================================================

@echo off
for /R "D:\test" %%s in (.,*) do (
  echo %%s
)
pause

结果

D:\test\.
D:\test\A Folder 1\.
D:\test\A Folder 1\A file 1.txt
D:\test\A Folder 1\A file 2.txt
D:\test\A Folder 1\A file 3.txt
D:\test\B Folder 2\.
D:\test\B Folder 2\B file 1.txt
D:\test\B Folder 2\B file 2.txt
D:\test\B Folder 2\B file 3.txt
D:\test\B Folder 2\B Folder 3\.
D:\test\B Folder 2\B Folder 3\B sub file 1.txt
D:\test\B Folder 2\B Folder 3\B sub file 2.txt
D:\test\B Folder 2\B Folder 3\B sub file 3.txt


这样的话看出来区别了吧。


再看一个=================================

@echo off
for /R "D:\test" %%s in (*) do (
  echo %%s
)
pause

结果

D:\test\A Folder 1\A file 1.txt
D:\test\A Folder 1\A file 2.txt
D:\test\A Folder 1\A file 3.txt
D:\test\B Folder 2\B file 1.txt
D:\test\B Folder 2\B file 2.txt
D:\test\B Folder 2\B file 3.txt
D:\test\B Folder 2\B Folder 3\B sub file 1.txt
D:\test\B Folder 2\B Folder 3\B sub file 2.txt
D:\test\B Folder 2\B Folder 3\B sub file 3.txt

就是只显示了文件

================================================================


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章