WindowXp下仿Linux之locate命令

来源:互联网 发布:凯撒贝利亚act淘宝 编辑:程序博客网 时间:2024/04/30 00:10

建立locate.bat在windows目录,内容如下

@echo off
rem 快速文件定位,仿Linux locate程序
rem 作者:苏晓 2006年4月

rem 如下{}测试的写法是最好的!其他的会出错。
cls
if {%1}=={} goto USEAGE
if {%1}=={/?} goto USEAGE
if {%1}=={/update} goto UPDATE
if {%1}=={/} goto TARGET
if {%1}=={/del} goto TARGETDEL
if {%2}=={/I}  goto FINDEX
if {%2}=={/i} goto FINDEX

rem 开始判断'-'的包涵与否
echo %1 > restrict.sut
findstr /M /C:"-" restrict.sut >result.sut
for /F "tokens=1,2*" %%j in (result.sut) do if {%%j}=={restrict.sut} goto BAD
goto OK
:BAD
del result.sut
del restrict.sut
echo 请不要在字符串使用'-',而用.来代替。
goto END

:OK
del result.sut
del restrict.sut

:FIND
if exist %windir%/dirdb.txt findstr %1 %windir%/dirdb.txt
if not exist %windir%/dirdb.txt echo 没有目录数据库,请先建立然后再搜索。
goto END
:FINDEX
if exist %windir%/dirdb.txt findstr %2 %1 %windir%/dirdb.txt
if not exist %windir%/dirdb.txt echo 没有目录数据库,请先建立然后再搜索。
goto END

:UPDATE
echo 开始更新文件目录数据库。。。请等待
echo 这是locate程序使用的数据库文件>%windir%/dirdb.txt
echo 作者:苏晓 2006年4月>>%windir%/dirdb.txt
for %%i in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do if exist %%i: echo 处理%%i盘 && echo true>%%i:/Driver.true && if exist %%i:/Driver.true pushd %%i:/ && del Driver.true && dir /a /s /b >>%windir%/dirdb.txt && popd && echo %%i盘处理完毕
echo 数据库更新成功!
goto END
rem 部分目录更新
:TARGET
if {%2}=={} goto USEAGE
if {%3}=={} goto TARUPDATE

rem 开始判断'-'的包涵与否
echo %3 > restrict.sut
findstr /M /C:"-" restrict.sut >result.sut
for /F "tokens=1,2*" %%j in (result.sut) do if {%%j}=={restrict.sut} goto TARBAD
goto TAROK
:TARBAD
del result.sut
del restrict.sut
echo 请不要在字符串使用'-',而用.来代替。
goto END

:TAROK
del result.sut
del restrict.sut

if {%4}=={/I}  goto TARFINDEX
if {%4}=={/i} goto TARFINDEX

:TARFIND
if exist %2/dirdb.txt ( findstr %3 %2/dirdb.txt ) else ( echo 没有该目录数据库,请先建立然后再搜索。)
goto END
:TARFINDEX
if exist %2/dirdb.txt ( findstr %4 %3 %2/dirdb.txt ) else ( echo 没有该目录数据库,请先建立然后再搜索。)
goto END

:TARUPDATE
if not exist %2 goto NOPATH
echo 开始更新目标地址文件目录数据库。。。请等待
echo 这是locate程序使用的数据库文件>%2/dirdb.txt
echo 作者:苏晓 2006年4月>>%2/dirdb.txt
for %%i in (%2) do if exist %%i echo 处理%%i目录 && echo true>%%i/Driver.true && if exist %%i/Driver.true pushd %%i/ && del Driver.true && dir /a /s /b >>%2/dirdb.txt && popd && echo %%i处理完毕
goto END

:NOPATH
echo 目录不存在或者书写不正确。
goto END

:TARGETDEL
if {%2}=={} goto USEAGE
if exist %2/dirdb.txt del %2/dirdb.txt
goto END
:USEAGE
echo 快速文件定位,仿Linux locate程序
echo 作者:苏晓 2006年4月
echo 使用方法:
echo %0 {[/update] [[/][/del] [targetpath]] [filename [options:/I,/i]]}
echo /update 重新建立分区目录数据库dirdb.txt,在%windir%目录下
echo / 开启目标目录方式 targetpath是目标路径,结尾没有'/'符号
echo /del 用来删除目标目录下的dirdb.txt文件
echo filename 要查找的文件名称可以是大概的,支持使用findstr通配符。
echo 目标目录方式下,不带filename则是新建立其目录数据库
echo options 可以是 /I,/i 表示忽略大小写
findstr /?
echo 用法实例:
echo %0 "/.rmvb$ /.avi$" 查找所有.rmvb和.avi结尾的文件
echo %0 "/.rm[vb]*$ /.avi$" 查找所有 .rm和.rmvb(.rmvvbb等等)和.avi结尾的文件
echo %0 / d: 建立d:下的数据库
echo %0 / d:/soft 建立d:/soft下的数据库
echo %0 / d:/soft /.exe$ /i 查找d:/soft目录数据库中的记录,查找.exe文件忽略大小写
echo %0 /del d:/soft 删除d:/soft目录数据库
:END

原创粉丝点击