2016-11-04 90 views
0

我有一個問題,我試圖確定在嘗試下載文件之前給定的目錄是否存在於文件系統上。批處理文件 - 如果不存在不起作用

批處理文件:

:: Create Apache Directory if does not exist 
mkdir "%HOMEDRIVE%\Apache" 2> nul 

:: Setup Apache Ant if Ant does not exist 
if not exist "%HOMEDRIVE%\Apache\apache-ant-1.9.7\" (
    :: Set filename variable 
    SET "FILENAME=%~dp0\apache-ant-1.9.7-bin.zip" 

    :: Download ANT from mirror 
    bitsadmin.exe /transfer "Apache Ant Download" http://mirrors.ukfast.co.uk/sites/ftp.apache.org//ant/binaries/apache-ant-1.9.7-bin.zip "%FILENAME%" 

    :: Copy Apache Ant to C:\Apache-Ant 
    xcopy "%~dp0apache-ant-1.9.7-bin.zip" %HOMEDRIVE%\Apache\. 

    :: Delete zip file from curent directory 
    del "%~dp0apache-ant-1.9.7-bin.zip" 

    :: Unzip Apache Ant to C:\Apache-Ant 
    call :UnZipFile "%HOMEDRIVE%\Apache\" "%HOMEDRIVE%\Apache\apache-ant-1.9.7-bin.zip" 

    :UnZipFile <ExtractTo> <newzipfile> 
    set vbs="%temp%\_.vbs" 
    if exist %vbs% del /f /q %vbs% 
    >%vbs% echo Set fso = CreateObject("Scripting.FileSystemObject") 
    >>%vbs% echo If NOT fso.FolderExists(%1) Then 
    >>%vbs% echo fso.CreateFolder(%1) 
    >>%vbs% echo End If 
    >>%vbs% echo set objShell = CreateObject("Shell.Application") 
    >>%vbs% echo set FilesInZip=objShell.NameSpace(%2).items 
    >>%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip) 
    >>%vbs% echo Set fso = Nothing 
    >>%vbs% echo Set objShell = Nothing 
    cscript //nologo %vbs% 
    if exist %vbs% del /f /q %vbs% 

    :: Delete zip folder 
    del "%HOMEDRIVE%\Apache\apache-ant-1.9.7-bin.zip" 

    :: Set ANT_HOME path 
    setx ANT_HOME "%HOMEDRIVE%\Apache\apache-ant-1.9.7" /m 

    :: Add ANT to path 
    setx path "%PATH%;%HOMEDRIVE%\Apache\apache-ant-1.9.7\bin" /m 
) 

更新:我添加@aschipfl建議

目錄C:/Apache/apache-ant-1.9.7確實存在這樣的代碼應該失敗,但在運行時它仍下載文件並嘗試在那裏做進一步的設置。任何想法什麼是錯的,爲什麼if語句被執行的地方不應該是?

由於

+1

我不明白'IF'命令可能是錯誤的,但我確實看到了代碼的另一個問題。您正在代碼塊中設置一個變量,但是您沒有使用延遲擴展來在下一行代碼中使用該變量。 – Squashman

+0

@Squashman加入睡眠不是問題,但找出爲什麼該塊執行,但它不應該?嗯 –

+0

你在說什麼。睡眠與我的評論無關。我正在談論%filename%變量。當你在一個代碼塊中並且你設置了一個變量,並且你需要在該代碼塊中使用該變量時,你需要使用延遲擴展。 – Squashman

回答

1

標籤不是在塊(括號內的一系列指令)

如果所需的md將創建中間目錄內允許的。

批量不知道程序。如果你call是一個子程序,那麼當子程序結束時(達到文件結尾或exit)執行將返回call - 如此:UnZipFile與你的代碼之後的指令。

:unzipfile例程移動到文件結尾,並在它之前直接插入goto :eof,以確保代碼不會流向:unzipfile
:unzipfile的末尾添加goto :eof,以便稍後添加額外的代碼(如更多子例程)。 goto :eof具體是指「去物理結束文件」,它終止當前例程。

注意setx不影響當前環境下,也不會影響現有cmd的情況下,只有cmd的情況下,因此執行這兩個setsetx

if exist "%HOMEDRIVE%\Apache\apache-ant-1.9.7\" goto ant197exists 
:: Setup Apache Ant if Ant does not exist 
md "%HOMEDRIVE%\Apache\apache-ant-1.9.7\" 2>nul 
:: Set filename variable 
SET "FILENAME=%~dp0\apache-ant-1.9.7-bin.zip" 

:: Download ANT from mirror 
bitsadmin.exe /transfer "Apache Ant Download" http://mirrors.ukfast.co.uk/sites/ftp.apache.org//ant/binaries/apache-ant-1.9.7-bin.zip "%FILENAME%" 

:: Copy Apache Ant to C:\Apache-Ant 
xcopy "%~dp0apache-ant-1.9.7-bin.zip" "%HOMEDRIVE%\Apache\." 

:: Delete zip file from curent directory 
del "%~dp0apache-ant-1.9.7-bin.zip" 

:: Unzip Apache Ant to C:\Apache-Ant 
call :UnZipFile "%HOMEDRIVE%\Apache\" "%HOMEDRIVE%\Apache\apache-ant-1.9.7-bin.zip" 

:: Delete zip folder 
del "%HOMEDRIVE%\Apache\apache-ant-1.9.7-bin.zip" 

:: Set ANT_HOME path 
set "ANT_HOME=%HOMEDRIVE%\Apache\apache-ant-1.9.7" 
setx ANT_HOME "%HOMEDRIVE%\Apache\apache-ant-1.9.7" /m 

:: Add ANT to path 
set "path=%PATH%;%ant_home%" 
setx path "%PATH%" /m 

:ant197exists 

....whatever whatever 

goto :eof 

:UnZipFile <ExtractTo> <newzipfile> 
set vbs="%temp%\_.vbs" 
if exist %vbs% del /f /q %vbs% 
>%vbs% echo Set fso = CreateObject("Scripting.FileSystemObject") 
>>%vbs% echo If NOT fso.FolderExists(%1) Then 
>>%vbs% echo fso.CreateFolder(%1) 
>>%vbs% echo End If 
>>%vbs% echo set objShell = CreateObject("Shell.Application") 
>>%vbs% echo set FilesInZip=objShell.NameSpace(%2).items 
>>%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip) 
>>%vbs% echo Set fso = Nothing 
>>%vbs% echo Set objShell = Nothing 
cscript //nologo %vbs% 
if exist %vbs% del /f /q %vbs% 
goto :eof 
+0

感謝您的意見,但這仍然不能按我想要的方式工作。即使目錄存在,腳本仍會嘗試下載並解壓縮該文件。 –

+0

道歉 - 忽略我的評論,我運行錯誤的文件; /。也謝謝你解釋幾個位和bobs :) –