2016-10-24 75 views
1

我正在使用此批處理循環直到找到文件。有沒有辦法查找多個文件LookForFile= ("D:\File.txt" "D:\File1.txt")要求找到所有文件,然後結束腳本..使用類似於下面的東西的批處理?批處理腳本以查找多個文件,並保留

SET LookForFile="D:\File.txt" 

:CheckForFile 

IF EXIST %LookForFile% GOTO FoundIt 

REM If we get here, the file is not found. 

REM Wait 5 seconds and then recheck. 
REM If no display is needed, comment/remove the timeout line. 

TIMEOUT /T 5 >nul 

GOTO CheckForFile 


:FoundIt 

REM If you are here the file is found 

ECHO Found: %LookForFile% 

任何建議將是可愛的感謝。

+0

代碼會在至少找到一個文件或找到所有文件的情況下繼續運行嗎? –

+0

@MCND找到所有文件時。添加到問題。 – JPC

+0

您可以使用命令'Where /?'==> [用單個字符串查找多個文件路徑](http://stackoverflow.com/questions/40048402/find-multiple-files-paths-with-single-string/ 40057656#40057656)將''FileName = Readme.txt''改爲''FileName = File * .txt'' – Hackoo

回答

1
@echo off 
    setlocal enableextensions disabledelayedexpansion 

:loop 
    rem Be optimistic 
    set "allFilesFound=1" 

    rem Now, check. If some file is not found clean variable 
    for %%a in (
     "d:\file.txt" "d:\file1.txt" 
    ) do if not exist "%%~a" set "allFilesFound=" 

    rem If the variable is clean (it is not defined) there is 
    rem at least a missing file. Wait and check again 
    if not defined allFilesFound (
     >nul 2>nul ping -n 6 localhost 
     goto :loop 
    ) 

    rem Script continue here when all files are found 
    echo All files found 
相關問題