DOS下的文本遍历和文件遍历代码(6个)

来源:互联网 发布:java main执行 编辑:程序博客网 时间:2024/06/06 14:00
DOS下的文本遍历和文件遍历代码(6个)

CODE:  [Copy to clipboard]
:: VisitF.bat - 对指定路径指定文件进行遍历的程序
:: 第一参数为要遍历的文件(支持通配符),第二参数为要遍历的路径(缺省为C盘根)
@echo off

:main
if [%1]==[] if not exist filelist.txt goto end

:init
if exist filelist.txt if exist xset.asd goto loop
set file=%1
set base=%2
if [%2]==[] set base=c:
dir %base%\%file% /s /a /b > filelist.txt
echo e 100 ''set file='' > xset.asd
echo w >> xset.asd
echo q >> xset.asd

:loop
fc filelist.txt 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  filelist.tx2
copy filelist.tx2 filelist.txt > nul
goto loop

:restore
if exist filelist.txt del filelist.txt
if exist xset.asd del xset.asd
if exist filelist.tx2 del filelist.tx2
if exist setfile.bat del setfile.bat

:end
CODE:  [Copy to clipboard]
:: VisitD.bat - 对指定路径指定目录进行遍历的程序
:: 第一参数为要遍历的目录(支持通配符),第二参数为要遍历的路径(缺省为C盘根)
@echo off

:main
if [%1]==[] if not exist dirlist.txt goto end

:init
if exist dirlist.txt if exist xset.asd goto loop
set dir=%1
set base=%2
if [%2]==[] set base=c:
dir %base%\%dir% /s /ad /b > dirlist.txt
echo e 100 'set  dir=' > xset.asd
echo w >> xset.asd
echo q >> xset.asd

:loop
fc dirlist.txt 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  dirlist.tx2
copy dirlist.tx2 dirlist.txt > nul
goto loop

:restore
if exist dirlist.txt del dirlist.txt
if exist xset.asd del xset.asd
if exist dirlist.tx2 del dirlist.tx2
if exist setdir.bat del setdir.bat

:end
CODE:  [Copy to clipboard]
:: VisitL.bat - 对指定文件列表中的文件进行遍历的程序
:: 参数为要遍历的文件列表
@echo off

:main
if [%1]==[] if not exist filelist.txt goto end

:init
set filelist=%1
if [%1]==[] set filelist=filelist.txt
if not exist %filelist% goto end
copy %filelist% filelist.tx1 > nul
if exist xset.asd goto loop
echo e 100 'set file=' > xset.asd
echo w >> xset.asd
echo q >> xset.asd

:loop
fc filelist.tx1 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  filelist.tx2
copy filelist.tx2 filelist.tx1 > nul
goto loop

:restore
if exist xset.asd del xset.asd
if exist filelist.tx1 del filelist.tx1
if exist filelist.tx2 del filelist.tx2
if exist setfile.bat del setfile.bat

:end
CODE:  [Copy to clipboard]
:: VisitI.bat - 对指定路径指定DIR信息的文件进行遍历操作的的程序
:: 第一参数为指定的DIR信息项,第二参数为要遍历的路径(缺省为当前路径)
:: 注意:DIR信息项可以是文件名,扩展名,日期,时间等DIR命令提供的目录信息项
::          可以同时使用多项,但必须加一对引号,参数格式也须严格符合DIR的信息格式
@echo off

:main
if [%1]==[] goto end

:init
if exist filelist.txt if exist xset.asd goto loop
set info=%1
set base=%2
if [%2]==[] set base=.
dir %base%\. /s /a /b > filelist.txt
echo e 100 ''''set file='''' > xset.asd
echo w >> xset.asd
echo q >> xset.asd

:loop
fc filelist.txt 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  filelist.tx2
:: "%file%" 参数决定了所匹配的子目录下的所有文件和目录均不会再次匹配
copy filelist.tx2 filelist.txt > nul
goto loop

:restore
if exist filelist.txt del filelist.txt
if exist xset.asd del xset.asd
if exist filelist.tx2 del filelist.tx2
if exist setfile.bat del setfile.bat
set info=
set file=
set base=

:end

CODE:  [Copy to clipboard]
:: Visit.bat - 文件遍历批处理程序
:: Will Sort - 10/17/2004 - V2
::
:: 程序用途:
::     对指定文件集/目录集/文件列表执行指定操作
::
:: 命令行说明:
::     1. VISIT 文件集/目录集 [参数]
::        对文件集/目录集执行指定操作
::     2. VISIT @ 文件列表
::        对指定文件列表中的文件执行指定操作
::         
:: 注意事项:
::     - 文件集/目录集 中可包含有效路径和通配符
::     - 路径缺省为当前路径,文件集缺省为 *.* (并非所有文件)   
::     - 文件集/目录集 含空格时需用双引号引起
::     - [参数] 支持的DIR开关: /S /A /O /L等不与 /B 冲突者
::     - [参数] 不支持的DIR开关: /W /P /V 等与 /B 冲突者
::     - [操作] 由调用者预先写入 visitcmd.bat 中
::     - [操作] 中使用 %VisitFile% 引用遍历文件
::     - 程序检查检查 [文件列表] 是否存在,但不检查它是否有效
::     - 不遍历隐藏/系统目录下的目录和文件(在命令行中指定时例外)
::
:: 用法示例:
::     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
::     在遍历未显式指定的隐藏/系统目录时,可以用"attrib 文件集 /s"生成
:: 文件列表,然后在visitcmd.bat的代码中引用%VisitFile%第三至最后的串,
:: 再使用文件列表进行遍历
::
:: 测试报告:
::     在 Win98 命令行方式下有限黑箱测试通过
::     性能仍然是最大的瓶颈
::         
@echo off
if "%1"=="@" goto CopyList

:MakeList
dir /b %1 %2 %3 %4 %5 %6 > ~visit.lst
find "~visit.lst" /v < ~visit.lst > ~visit.tmp
if not errorlevel 1 copy ~visit.tmp ~visit.lst>nul
goto MakePreLine

:CopyList
if not [%2]==[] if exist %2 copy %2 ~visit.lst>nul
if not exist ~visit.lst goto End

:MakePreLine
echo set VisitFile=> ~visit.pre
for %%c in (rcx e w q) do echo %%c>> ~visit.asd
debug ~visit.pre < ~visit.asd > nul
if [%OS%]==[Windows_NT] chcp 936 > nul

:LoopVisit
copy ~visit.pre+~visit.lst ~visit.tmp > nul
find "set VisitFile=" < ~visit.tmp > ~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 < ~visit.tmp > ~visit.lst
goto LoopVisit

:Clear
del ~visit.*
set VisitFile=

:End
CODE:  [Copy to clipboard]
:: VisitCE.bat - 文件遍历批处理程序命令行增强版
:: Will Sort - 10/17/2004 - V2CE
::
:: 程序用途:
::     对指定路径/文件列表下的指定文件/目录集执行指定操作
::
:: 命令行说明:
::     1. VISIT [路径1 路径2...] [开关1 开关2...] [文件集1 文件集2...]
::        对 [路径] 和 [开关] 限定下的 [文件集] 执行指定操作
::     2. VISIT @ 文件列表1 [文件列表2...]
::        对指定 [文件列表] 中的文件执行指定操作
::         
:: 注意事项:
::     - [路径] [参数] [文件集] 均可不选或多选
::     - [路径] 中不可包含通配符,[文件集] 中可包含有效路径和通配符
::     - [路径] 缺省为当前路径,[文件集] 缺省为 *.* (并非所有文件)   
::     - [路径] [文件集] 含空格时需用双引号引起
::     - [参数] 支持的DIR开关: /S /A /O /L等不与 /B 冲突者
::     - [参数] 不支持的DIR开关: /W /P /V 等与 /B 冲突者
::     - [操作] 由调用者预先写入 visitcmd.bat 中
::     - [操作] 中使用 %VisitFile% 引用遍历文件
::     - 程序检查检查 [文件列表] 是否存在,但不检查它是否有效
::     - 不遍历隐藏/系统目录下的目录和文件(在命令行中指定时例外)
::
:: 用法示例:
::     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                   遍历当前盘根目下所有文件和目录
::     在遍历未显式指定的隐藏/系统目录时,可以用"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% >> ~visit.lst
    goto MakeNext
:ForMake
    for %%p in (%VisitPath%) do dir %%p.\%2 /b %VisitSwitch% >> ~visit.lst
:MakeNext
    shift
if not [%2]==[] goto MakeList
find "~visit.lst" /v < ~visit.lst > ~visit.tmp
if not errorlevel 1 copy ~visit.tmp ~visit.lst>nul
goto MakePreLine

:CopyList
if not [%2]==[] if exist %2 type %2>>~visit.lst
shift
if not [%2]==[] goto CopyList

:MakePreLine
if not exist ~visit.lst goto End
echo set VisitFile=> ~visit.pre
for %%c in (rcx e w q) do echo %%c>> ~visit.asd
debug ~visit.pre < ~visit.asd > nul
if [%OS%]==[Windows_NT] chcp 936 > nul

:LoopVisit
copy ~visit.pre+~visit.lst ~visit.tmp > nul
find "set VisitFile=" < ~visit.tmp > ~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 < ~visit.tmp > ~visit.lst
goto LoopVisit

:Clear
for %%f in (~visit.*) do del %%f
for %%e in (Command Path Switch Filter File) do set Visit%%e=

:End
原创粉丝点击