找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 25439|回复: 6

[文件操作] [已解决]批处理如何全盘搜索指定程序并执行?

[复制链接]
发表于 2011-9-25 20:02:54 | 显示全部楼层 |阅读模式
有什么办法全盘搜索指定程序并执行
要求:1. 运行后退出搜索
      2.  优先搜索所有磁盘下的某目录(C、D、E盘等下的指定目录)

在网上找的能运行但会继续搜索
  1. for /f "skip=1" %%i in ('wmic logicaldisk where "drivetype=3" get name') do (
  2. for /f "delims=" %%e in ('dir /s /b %%i\XX.exe 2^>nul') do start "" "%%~e"
  3. )
复制代码
发表于 2011-9-25 21:44:15 | 显示全部楼层
  1. @echo off
  2. for /f "skip=1" %%i in ('wmic logicaldisk where "drivetype=3" get name') do (
  3.     for /f "delims=" %%e in ('dir /s /b %%i\XX.exe 2^>nul') do (
  4.         set "fullpath=%%~e"
  5.         goto :breakloop
  6.     )
  7. )
  8. goto :eof

  9. :breakloop
  10. if exist "%fullpath%" (
  11.     start "" "%fullpath%"
  12. )
复制代码
 楼主| 发表于 2011-9-26 12:44:13 | 显示全部楼层
本帖最后由 xxez 于 2011-9-26 12:47 编辑

回复 2# awk
优先搜索目录该在何处添加(多个磁盘下),不是很懂,麻烦解释下,谢谢!!
 楼主| 发表于 2011-9-26 14:19:45 | 显示全部楼层
求解!!!
发表于 2011-9-26 14:51:08 | 显示全部楼层
回复 3# xxez
  1. @echo off
  2. set "firstlook=test\folder\myfolder"
  3. for /f "skip=1" %%i in ('wmic logicaldisk where "drivetype=3" get name') do (
  4.     for /f "delims=" %%e in ('dir /s /b "%%i\%firstlook%\XX.exe" 2^>nul') do (
  5.         set "fullpath=%%~e"
  6.         if exist "%fullpath%" (
  7.             goto :breakloop
  8.         )
  9.     )
  10. )
  11. for /f "skip=1" %%i in ('wmic logicaldisk where "drivetype=3" get name') do (
  12.     for /f "delims=" %%e in ('dir /s /b %%i\XX.exe 2^>nul') do (
  13.         set "fullpath=%%~e"
  14.         if exist "%fullpath%" (
  15.             goto :breakloop
  16.         )
  17.     )
  18. )
  19. goto :eof

  20. :breakloop
  21. start "" "%fullpath%"
复制代码
发表于 2011-9-26 22:53:21 | 显示全部楼层
本帖最后由 cjiabing 于 2011-9-26 22:56 编辑

  1. @echo off
  2. echo,
  3. set /p nvar=    请输入搜索名称:
  4. echo,
  5. set /p pvar=    请输入搜索路径(直接回车全盘搜索):
  6. echo,
  7. if not defined pvar if "%pvar%"=="" goto qpss
  8. if defined pvar if not "%pvar%"=="" if exist %pvar% (
  9.     for /f "tokens=*" %%a in ('dir /b /s  %pvar%\*%nvar%') do (if exist "%%~a" echo;%%a&start "" "%%~a"&pause)
  10.     exit
  11. )
  12. :qpss
  13. 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 (
  14.     if exist %%i:  for /f "tokens=*" %%a in ('dir /b /s %%i:\*%nvar%') do (if exist "%%~a" echo;%%a&start "" "%%~a"&pause)
  15. )
  16. exit
复制代码

优先搜索CD盘,这个没什么优先的,批处理按顺序就是从第一个开始的。当然上面的也可以调整。
现在上面的代码,要么指定路径搜索(去掉第一个EXIT可以形成优先),要么指定全盘搜索。如果既要优先又要全屏,请去掉第一个EXIT命令。
 楼主| 发表于 2011-9-27 12:48:32 | 显示全部楼层
谢谢,问题已解决!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-18 03:08 , Processed in 0.019712 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表