批处理集锦——(6)【转】批处理实现全盘搜索指定文件获取其完整路径方法大全

【方案一】for /f + dir

@echo off
rem 指定待搜索的文件
set "FileName=string.bat"

echo 正在搜索,请稍候...

for %%a 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 %%a:\ (
  
        for /f "delims=" %%b in ('dir /a-d /s /b "%%a:\*%FileName%" 2^>nul') do (

        if /i "%%~nxb" equ "%FileName%" (
            echo,%%b
        )
      
    )
    
  )
  
)

【方案二】for /r(比 for /f + dir 效率高,但不会遍历隐藏文件)

@echo off
rem 指定待搜索的文件
set "FileName=BatHome_Batcher.txt"
echo 正在搜索,请稍候...
for %%a 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 %%a:\ (
  pushd %%a:\
  for /r %%b in (*%FileName%) do (
   if /i "%%~nxb" equ "%FileName%" (
    echo,%%b
   )
  )
  popd
 )
)

【方案三】WMIC(Win2000/WinXP HOME版、WinXP 精简版可能不支持)

@echo off
rem 指定待搜索的文件
set "FileName=BatHome_Batcher.txt"
echo 正在搜索,请稍候...
for %%a in ("%FileName%") do (
 set "nName=%%~na"
 set "xName=%%~xa"
)
set "xName=%xName:~1%"
for /f "skip=1 delims=" %%a in ('wmic datafile where "filename='%nName%' and Extension='%xName%'" get name') do (
 echo,%%a
)

【方案四】使用 Windows7 系统自带的 where 命令

@echo off
rem 指定待搜索的文件
set "FileName=BatHome_Batcher.txt"
echo 正在搜索,请稍候...
for %%a 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 %%a:\ (
  for /f "delims=" %%b in ('where /r %%a: "%FileName%" 2^>nul') do (
   if /i "%%~nxb" equ "%FileName%" (
    echo,%%b
   )
  )
 )
)

出处:http://www.jb51.net/article/54791.htm

posted @ 2016-07-30 02:01  工程内业编程  阅读(2790)  评论(0编辑  收藏  举报