2017-05-12 158 views
0

嗨,我是新來這個網站,並表示道歉,如果這已被回答,但雖然我可以找到我的問題在這個網站上的答案的部分我找不到解決方案到我想要做的事情。檢查文件是否存在批量文件中的數組

我需要做的是在批處理腳本中定義一個文件列表,然後循環遍歷包含一些數據文件的目錄,並確保已傳遞到該目錄的文件與我的列表中的內容匹配。如果他們不這樣做,然後建立一個字符串回車界定,然後將形成一個電子郵件警報的文本,將在批處理結束時被調用。我有什麼迄今爲止是這樣的:

echo off 

setlocal EnableDelayedExpansion 

SET ImportDirectory=D:\Data Files\ 

SET MissingFiles= 

@REM Build Array of sorts 

@set arrayline[0]=%ImportDirectory%File1*.xml 

@set arrayline[1]=%ImportDirectory%File2*.xml 

@set arrayline[2]=%ImportDirectory%File3*.xml etc 

@REM Loop through and check whether file exists 
@for /l %%n in (0,1,2) do (

if not exist !arrayline[%%n]! (
    echo File does not exist 
    SET MissingFiles=%MissingFiles%!arrayline[%%n]! 

    REM echo File !arrayline[%%n]! has not been delivered for processing >> %LogFilePath% 
    echo File !arrayline[%%n]! has not been delivered for processing 
) else (
echo File Exists 
) 
) 

@REM Test to see if any files are missing and send email 
IF NOT %MissingFiles% == "" (
@REM Call code that sends the email with Missing Files as the body 
) 

當我嘗試運行此運行,但我的錯誤列表不會被建造。我可以回顯!arrayline [%% n]!在循環中每次都變量,但它不寫入或附加到Missing File變量。同樣,如果我嘗試將結果輸出到文件中,則不會輸出任何內容 - 我只是收到有關回顯關閉的各種消息。

任何指導,將不勝感激。

回答

0

您正在使用延遲擴張!arrayline[%%n]!但也不是爲%MissingFiles%

@echo off 
setlocal EnableDelayedExpansion 
SET ImportDirectory=D:\Data Files\ 
SET MissingFiles= 
REM Build Array of sorts 
set "arrayline[0]=%ImportDirectory%File1*.xml" 
set "arrayline[1]=%ImportDirectory%File2*.xml" 
set "arrayline[2]=%ImportDirectory%File3*.xml" 

REM Loop through and check whether file exists 
for /l %%n in (0,1,2) do (
    if not exist !arrayline[%%n]! (
     echo File does not exist 
     SET "MissingFiles=!MissingFiles!,!arrayline[%%n]!" 

     REM echo File !arrayline[%%n]! has not been delivered for processing >> %LogFilePath% 
     echo File !arrayline[%%n]! has not been delivered for processing 
    ) else (
     echo File !arrayline[%%n]! Exists 
    ) 
) 

REM Test to see if any files are missing and send email 
IF NOT "%MissingFiles%" == "" (
    Echo Missingfiles: %MissingFiles:~1% 
    REM Call code that sends the email with Missing Files as the body 
)