===== 文件夾結構 =============================================
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 (
)
的區別是什麼呢?
在有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
就是隻顯示了文件
================================================================