国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > BAT > 正文

dos下遍歷目錄和文件的代碼(主要利用for命令)

2020-06-09 13:54:17
字體:
來源:轉載
供稿:網友
===== 文件夾結構 =============================================
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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 麻江县| 谷城县| 嘉禾县| 秭归县| 资阳市| 成都市| 正阳县| 军事| 余姚市| 汾阳市| 德州市| 九龙城区| 太和县| 上林县| 仙游县| 漳州市| 青州市| 莒南县| 乌兰察布市| 河北区| 松原市| 鲁山县| 改则县| 台湾省| 鸡泽县| 南丹县| 东港市| 永仁县| 陆丰市| 台北市| 康平县| 宜丰县| 江口县| 二手房| 壶关县| 抚松县| 泸溪县| 屯留县| 沁水县| 兴国县| 玉树县|