【方案一】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:\ (
- for /f "delims=" %%b in ('dir /b /s /a-d "%%a:\*%FileName%" 2^>nul') do (
- if /i "%%~nxb" equ "%FileName%" (
- echo,%%b
- )
- )
- )
- )
- pause
复制代码 【方案二】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
- )
- )
- pause
复制代码 【方案三】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
- )
- pause
复制代码 【方案四】使用 Win7 / Win10 系统自带的 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
- )
- )
- )
- )
- pause
复制代码 【方案五】只在所有盘符的某个固定文件夹下面搜索
@echo off- REM 指定待搜索的文件
- set "FileName=BatHome_Batcher.txt"
- REM 指定在盘符根目录下哪个文件夹里面搜索
- set FolderName=bbs\bathome\net
- 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:\%FolderName%" (
- pushd "%%a:\%FolderName%"
- for /f "delims=" %%b in ('dir /b /s /a-d "%FileName%" 2^>nul') do (
- echo,%%b
- )
- popd
- )
- )
- pause
复制代码
|