Board logo

标题: [文件操作] [已解决]批处理如何全盘搜索指定程序并执行? [打印本页]

作者: xxez    时间: 2011-9-25 20:02     标题: [已解决]批处理如何全盘搜索指定程序并执行?

有什么办法全盘搜索指定程序并执行
要求: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. )
复制代码

作者: awk    时间: 2011-9-25 21:44

  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. )
复制代码

作者: xxez    时间: 2011-9-26 12:44

本帖最后由 xxez 于 2011-9-26 12:47 编辑

回复 2# awk
优先搜索目录该在何处添加(多个磁盘下),不是很懂,麻烦解释下,谢谢!!
作者: xxez    时间: 2011-9-26 14:19

求解!!!
作者: awk    时间: 2011-9-26 14:51

回复 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%"
复制代码

作者: cjiabing    时间: 2011-9-26 22:53

本帖最后由 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命令。
作者: xxez    时间: 2011-9-27 12:48

谢谢,问题已解决!




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2