搜索文件但可能需要超时

时间:2015-07-27 16:56:03

标签: batch-file

我有一个程序来搜索特定文件的驱动器。唯一的问题是我不知道它是什么驱动器。当它在一个驱动器中搜索一个文件时,就会出现问题,这个文件在10秒之后就没有办法进入下一个驱动器。到目前为止,我的代码是

    @echo off


setlocal EnableDelayedExpansion

 set count=1
 for /f "skip=1" %%a in ('wmic logicaldisk get caption') do (
  set drive!count!=%%a
  set /a count+=1
)

set "gh=!drive1!"
::Change this to do whatever with the variables
set "fh=!drive2!"
set "hh=!drive3!"
set "jh=!drive4!"

For /R %gh%\ %%G IN (*.ut2) do set jk="%%~dpG"
if defined jk (
echo %jk%
) else (
goto next
)

for /r %jk% %%a in (*) do if "%%~nxa"=="CTF-Hydro-16-2k3.ut2" set  k=%%~dpnxa
if defined k (
echo %k% found
pause
cls
echo You have it
goto end
) else (
echo Map not found
copy %CD:~0,3%\Unrealmap\CTF-Hydro-16-2k3.ut2 %jk%
goto end
)

:next

For /R %fh%\ %%G IN (*.ut2) do set ht="%%~dpG"
if defined ht (
echo %ht%
) else (
goto there
)

for /r %ht% %%a in (*) do if "%%~nxa"=="CTF-Hydro-16-2k3.ut2" set m=%%~dpnxa
if defined k (
echo %m% found
pause
cls
echo You have it
goto end
) else (
echo Map not found
copy %CD:~0,3%\Unrealmap\CTF-Hydro-16-2k3.ut2 %jk%
goto end

:there
:end
cls
echo done
pause

2 个答案:

答案 0 :(得分:0)

app\storage\views

答案 1 :(得分:0)

尝试下一步方法:

@ECHO OFF >NUL
SETLOCAL enableextensions
set /A "ii=0"
for /f "skip=1" %%D in ('
    wmic logicaldisk get caption
    ') do for /F %%d in ("%%D") do (
  echo searching %%d
  for /F "delims=" %%G IN ('dir /B /S "%%d\CTF-Hydro-16-2k3.ut2" 2^>nul') do (
    set /A "ii+=1"
    set "k=%%G"
    rem remove 'rem' from next line to discontinue searching 
    rem goto :testfound
  )
  TIMEOUT /T 10 /NOBREAK >NUL
)
:testfound
if %ii% EQU 0 (
  echo Map not found
) else (
  echo Map found %ii% times: last one "%k%"
)

这里针对for命令的wmic循环是

  • %%D检索caption值;
  • %%d删除返回值中的结尾回车符wmic行为:每个输出行以0x0D0D0A结尾(<CR><CR><LF>)而不是普通0x0D0A<CR><LF>)。

见Dave Benham的WMIC and FOR /F: A fix for the trailing <CR> problem

另一种可能性:解析

wmic datafile where "Extension='ut2' and FileName='CTF-Hydro-16-2k3'" get Name 2>NUL