溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

dos下如何遍歷目錄和文件

發(fā)布時間:2021-10-08 13:59:05 來源:億速云 閱讀:315 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下dos下如何遍歷目錄和文件,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

===== 文件夾結(jié)構(gòu) =============================================
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


結(jié)果
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


結(jié)果
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
)


的區(qū)別是什么呢?
在有cd %work_path% 的時候,這兩個命令執(zhí)行的結(jié)果是一樣的,就像我們上面舉的例子。但是
for /R %%s in (.,*) do (
echo %%s
)
的批處理中沒有cd %work_path% ,那么顯示的將是這個批處理文件所在文件夾下面的遍歷結(jié)果。

代碼如下:


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


結(jié)果
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


結(jié)果
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

這樣的話看出來區(qū)別了吧。

再看一個=================================

代碼如下:


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


結(jié)果
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
就是只顯示了文件
VisitF.bat - 對指定路徑指定文件進行遍歷的程序

代碼如下:


:: VisitF.bat - 對指定路徑指定文件進行遍歷的程序
:: 第一參數(shù)為要遍歷的文件(支持通配符),第二參數(shù)為要遍歷的路徑(缺省為C盤根)
@echo off
:main
if [%1]==[] if not exist goto end
:init
if exist if exist goto loop
set file=%1
set base=%2
if [%2]==[] set base=c:
dir %base%\%file% /s /a /b >
echo e 100 ''set file='' >
echo w >>
echo q >>
:loop
fc nul /n | find " 1:" > setfile.bat
if errorlevel 1 goto restore
debug setfile.bat nul
call setfile.bat
echo Visiting the file: %file%
:: User specified visit code replace this line
find "%file%" /v
copy > nul
goto loop
:restore
if exist del
if exist del
if exist del
if exist setfile.bat del setfile.bat
:end



VisitD.bat - 對指定路徑指定目錄進行遍歷的程序

代碼如下:


:: VisitD.bat - 對指定路徑指定目錄進行遍歷的程序
:: 第一參數(shù)為要遍歷的目錄(支持通配符),第二參數(shù)為要遍歷的路徑(缺省為C盤根)
@echo off
:main
if [%1]==[] if not exist goto end
:init
if exist if exist goto loop
set dir=%1
set base=%2
if [%2]==[] set base=c:
dir %base%\%dir% /s /ad /b >
echo e 100 'set dir=' >
echo w >>
echo q >>
:loop
fc nul /n | find " 1:" > setdir.bat
if errorlevel 1 goto restore
debug setdir.bat nul
call setdir.bat
echo Visiting the dir: %dir%
:: User specified visit code replace this line
find "%dir%" /v
copy > nul
goto loop
:restore
if exist del
if exist del
if exist del
if exist setdir.bat del setdir.bat
:end


VisitL.bat - 對指定文件列表中的文件進行遍歷的程序

代碼如下:


:: VisitL.bat - 對指定文件列表中的文件進行遍歷的程序
:: 參數(shù)為要遍歷的文件列表
@echo off
:main
if [%1]==[] if not exist goto end
:init
set filelist=%1
if [%1]==[] set filelist=
if not exist %filelist% goto end
copy %filelist% > nul
if exist goto loop
echo e 100 'set file=' >
echo w >>
echo q >>
:loop
fc nul /n | find " 1:" > setfile.bat
if errorlevel 1 goto restore
debug setfile.bat nul
call setfile.bat
echo Visiting the file: %file%
:: User specified visit code replace this line
find "%file%" /v
copy > nul
goto loop
:restore
if exist del
if exist del
if exist del
if exist setfile.bat del setfile.bat
:end



VisitI.bat - 對指定路徑指定DIR信息的文件進行遍歷操作的的程序

代碼如下:


:: VisitI.bat - 對指定路徑指定DIR信息的文件進行遍歷操作的的程序
:: 第一參數(shù)為指定的DIR信息項,第二參數(shù)為要遍歷的路徑(缺省為當前路徑)
:: 注意:DIR信息項可以是文件名,擴展名,日期,時間等DIR命令提供的目錄信息項
:: 可以同時使用多項,但必須加一對引號,參數(shù)格式也須嚴格符合DIR的信息格式
@echo off
:main
if [%1]==[] goto end
:init
if exist if exist goto loop
set info=%1
set base=%2
if [%2]==[] set base=.
dir %base%\. /s /a /b >
echo e 100 ''''set file='''' >
echo w >>
echo q >>
:loop
fc nul /n | find " 1:" > setfile.bat
if errorlevel 1 goto restore
debug setfile.bat nul
call setfile.bat
dir "%file%" | find %info% > nul
if not errorlevel 1 echo Visit file: "%file%"
:: if not errorlevel 1
find "%file%" /v
:: "%file%" 參數(shù)決定了所匹配的子目錄下的所有文件和目錄均不會再次匹配
copy > nul
goto loop
:restore
if exist del
if exist del
if exist del
if exist setfile.bat del setfile.bat
set info=
set file=
set base=
:end



Visit.bat - 文件遍歷批處理程序

代碼如下:


:: Visit.bat - 文件遍歷批處理程序
:: Will Sort - 10/17/2004 - V2
::
:: 程序用途:
:: 對指定文件集/目錄集/文件列表執(zhí)行指定操作
::
:: 命令行說明:
:: 1. VISIT 文件集/目錄集 [參數(shù)]
:: 對文件集/目錄集執(zhí)行指定操作
:: 2. VISIT @ 文件列表
:: 對指定文件列表中的文件執(zhí)行指定操作
::
:: 注意事項:
:: - 文件集/目錄集 中可包含有效路徑和通配符
:: - 路徑缺省為當前路徑,文件集缺省為 *.* (并非所有文件)
:: - 文件集/目錄集 含空格時需用雙引號引起
:: - [參數(shù)] 支持的DIR開關(guān): /S /A /O /L等不與 /B 沖突者
:: - [參數(shù)] 不支持的DIR開關(guān): /W /P /V 等與 /B 沖突者
:: - [操作] 由調(diào)用者預先寫入 visitcmd.bat 中
:: - [操作] 中使用 %VisitFile% 引用遍歷文件
:: - 程序檢查檢查 [文件列表] 是否存在,但不檢查它是否有效
:: - 不遍歷隱藏/系統(tǒng)目錄下的目錄和文件(在命令行中指定時例外)
::
:: 用法示例:
:: visit c:\ /ad /s 遍歷C盤所有目錄,包含子目錄
:: visit "C:\My document" /a-d 遍歷"C:\My document"下所有文件
:: visit C:\*.zip /a /s 遍歷C盤所有.zip壓縮包,包含子目錄
:: 如想遍歷多個文件/目錄集,可多次使用"DIR 文件集 /a /s>>文件列表"
:: 生成一個完整的文件列表,再使用文件列表進行遍歷;或者使用VisitCE.Bat
:: 在遍歷未顯式指定的隱藏/系統(tǒng)目錄時,可以用"attrib 文件集 /s"生成
:: 文件列表,然后在visitcmd.bat的代碼中引用%VisitFile%第三至最后的串,
:: 再使用文件列表進行遍歷
::
:: 測試報告:
:: 在 Win98 命令行方式下有限黑箱測試通過
:: 性能仍然是最大的瓶頸
::
@echo off
if "%1"=="@" goto CopyList
:MakeList
dir /b %1 %2 %3 %4 %5 %6 > ~
find "~" /v < ~ > ~
if not errorlevel 1 copy ~ ~>nul
goto MakePreLine
:CopyList
if not [%2]==[] if exist %2 copy %2 ~>nul
if not exist ~ goto End
:MakePreLine
echo set VisitFile=> ~
for %%c in (rcx e w q) do echo %%c>> ~
debug ~ < ~ > nul
if [%OS%]==[Windows_NT] chcp 936 > nul
:LoopVisit
copy ~+~ ~ > nul
find "set VisitFile=" < ~ > ~visit.bat
call ~visit.bat
if "%VisitFile%"=="" goto Clear
if not exist visitcmd.bat echo Visiting %VisitFile%
if exist visitcmd.bat call visitcmd.bat
find "set VisitFile=" /v < ~ > ~
goto LoopVisit
:Clear
del ~visit.*
set VisitFile=
:End


VisitCE.bat - 文件遍歷批處理程序命令行增強版

代碼如下:


:: VisitCE.bat - 文件遍歷批處理程序命令行增強版
:: Will Sort - 10/17/2004 - V2CE
::
:: 程序用途:
:: 對指定路徑/文件列表下的指定文件/目錄集執(zhí)行指定操作
::
:: 命令行說明:
:: 1. VISIT [路徑1 路徑2...] [開關(guān)1 開關(guān)2...] [文件集1 文件集2...]
:: 對 [路徑] 和 [開關(guān)] 限定下的 [文件集] 執(zhí)行指定操作
:: 2. VISIT @ 文件列表1 [文件列表2...]
:: 對指定 [文件列表] 中的文件執(zhí)行指定操作
::
:: 注意事項:
:: - [路徑] [參數(shù)] [文件集] 均可不選或多選
:: - [路徑] 中不可包含通配符,[文件集] 中可包含有效路徑和通配符
:: - [路徑] 缺省為當前路徑,[文件集] 缺省為 *.* (并非所有文件)
:: - [路徑] [文件集] 含空格時需用雙引號引起
:: - [參數(shù)] 支持的DIR開關(guān): /S /A /O /L等不與 /B 沖突者
:: - [參數(shù)] 不支持的DIR開關(guān): /W /P /V 等與 /B 沖突者
:: - [操作] 由調(diào)用者預先寫入 visitcmd.bat 中
:: - [操作] 中使用 %VisitFile% 引用遍歷文件
:: - 程序檢查檢查 [文件列表] 是否存在,但不檢查它是否有效
:: - 不遍歷隱藏/系統(tǒng)目錄下的目錄和文件(在命令行中指定時例外)
::
:: 用法示例:
:: visit c:\ /ad /s 遍歷C盤所有目錄,包含所有子目錄
:: visit "C:\My document" /a-d 遍歷"C:\My document"下所有文件
:: visit c:\ d:\ e:\ /s /a /on 遍歷C,D,E中所有文件,并按文件名排序
:: visit \ /a 遍歷當前盤根目下所有文件和目錄
:: 在遍歷未顯式指定的隱藏/系統(tǒng)目錄時,可以用"attrib 文件集 /s"生成
:: 文件列表,然后在visitcmd.bat的代碼中引用%VisitFile%第三至最后的串,
:: 再使用文件列表進行遍歷
::
:: 測試報告:
:: 在 Win98 命令行方式下有限黑箱測試通過
:: 性能仍然是最大的瓶頸
::
@echo off
if "%1"=="$" goto MakeList
if "%1"=="@" goto CopyList
if "%1"=="" goto End
set VisitCommand=%0
:GetArgu
:GetPath
if not exist %1.\nul goto GetSwitch
set VisitPath=%VisitPath% %1
goto GetNext
:GetSwitch
echo %1 | find "/" > nul
if errorlevel 1 goto GetFilter
set VisitSwitch=%VisitSwitch% %1
goto GetNext
:GetFilter
echo %1 | find "*" > nul
if not errorlevel 1 goto SetFilter
echo %1 | find "?" > nul
if errorlevel 1 goto End
:SetFilter
set VisitFilter=%VisitFilter% %1
:GetNext
shift
if not [%1]==[] goto GetArgu
%VisitCommand% $ %VisitFilter%
:MakeList
if not [%VisitPath%]==[] goto ForMake
:DirMake
dir %2 /b %VisitSwitch% >> ~
goto MakeNext
:ForMake
for %%p in (%VisitPath%) do dir %%p.\%2 /b %VisitSwitch% >> ~
:MakeNext
shift
if not [%2]==[] goto MakeList
find "~" /v < ~ > ~
if not errorlevel 1 copy ~ ~>nul
goto MakePreLine
:CopyList
if not [%2]==[] if exist %2 type %2>>~
shift
if not [%2]==[] goto CopyList
:MakePreLine
if not exist ~ goto End
echo set VisitFile=> ~
for %%c in (rcx e w q) do echo %%c>> ~
debug ~ < ~ > nul
if [%OS%]==[Windows_NT] chcp 936 > nul
:LoopVisit
copy ~+~ ~ > nul
find "set VisitFile=" < ~ > ~visit.bat
call ~visit.bat
if "%VisitFile%"=="" goto Clear
if not exist visitcmd.bat echo Visiting %VisitPath% %VisitSwitch% %VisitFilter% - %VisitFile%
if exist visitcmd.bat call visitcmd.bat
find "set VisitFile=" /v < ~ > ~
goto LoopVisit
:Clear
for %%f in (~visit.*) do del %%f
for %%e in (Command Path Switch Filter File) do set Visit%%e=
:End


在2000sp4 下出現(xiàn)類似的問題,第二個問題實際上也是fc的不兼容問題,他在按行號顯示時,每行文字前有8 個字節(jié)位,而不是dos6和win9x 下的9 個,所以解決起來也很簡單,將debug 腳本中的 e 100 'set file='改為 e 100 'set fil=' 即可。

  但是,這只是就事論事而已,以上的提到的三個批處理,只是我在dos 下練兵用的試驗原型,做得很粗糙,自從今年3 月份正式轉(zhuǎn)向win98 之后,使用中遇到了很多新問題,比如文件名包含空格的問題,系統(tǒng)/ 隱藏屬性目錄的問題;在nt下使用,又遇到了中文文件名的問題;而且由于循環(huán)體中使用了過多的文本流控制,導致了效率的嚴重低下;另外,三個程序功能上和實現(xiàn)上并沒有太大的差別,沒有拆分的必要。
  綜所上述,將以上代碼重寫就成為必須。合并代碼,彌補漏洞,改善性能,書寫文檔,就出現(xiàn)了所謂的 V2 版,這中間其實并沒有花很多功夫。

  然而,一個偶然的下午,突然心血來潮,何不將visit 的命令行功能做一下增強,比如支持多個路徑、多個過濾器(filter)、多個文件列表,原想只是加一個命令行參數(shù)分析循環(huán),然而真正實現(xiàn)起來,卻再次體會到了命令行的復雜,if for嵌套時沖定向輸出,判定目錄時根目錄與子目錄的不同,for 對過濾器的低能替換(win9x/dos) ,一個個新問題摩肩接踵,層出不窮,竟然整整耗了我大半個工作日的時間,才勉強實現(xiàn)了一個粗糙的版本,這就是所謂的 V2CE 版,對與嚴格的測試我沒有多大的信心。

  現(xiàn)在,再回頭看我的工作,其實那個 V2CE 版其實是沒有多大的應用價值。多個路徑和過濾器,完全可以通過多次調(diào)用visit 來實現(xiàn),而且實際上,我們大多數(shù)情況下只用一個路徑和過濾器;而且,多參數(shù)控制帶來了易用度的下降,用戶總是對參數(shù)繁多的程序有一定的心理障礙,我個人對dos 壓縮軟件的愛好,從高到低依次是rar,zip,arj, 易用度就是第一靠量;另外,最重要的是,多參數(shù)控制導致了代碼量和復雜性的大幅提升,這個程序一度讓我鉆進了if for構(gòu)建的幽魂迷宮而不知南北西東,程序的可讀性越來越差,調(diào)試起來也越來越困難,同時兼容性上拉上了更多的絆馬索。

  這些,就是功能提升所付出的代價,雖然這只是一個比較極端的例子,但是功能與簡潔的平衡在程序設計的其重要性可見一斑。親和性的界面并不意味著親和性的代碼,人性化的需求并不意味著人性化的實現(xiàn),一味將使用方的復雜性轉(zhuǎn)嫁給設計者(比如Windows), 并不是一個很聰明的主意,反之亦然(比如Linux ),這尚不考慮代碼功能增強對設計方和使用方的雙重施壓。

以上是“dos下如何遍歷目錄和文件”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

dos
AI