我有一個問題,我試圖確定在嘗試下載文件之前給定的目錄是否存在於文件系統上。批處理文件 - 如果不存在不起作用
批處理文件:
:: 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語句被執行的地方不應該是?
由於
我不明白'IF'命令可能是錯誤的,但我確實看到了代碼的另一個問題。您正在代碼塊中設置一個變量,但是您沒有使用延遲擴展來在下一行代碼中使用該變量。 – Squashman
@Squashman加入睡眠不是問題,但找出爲什麼該塊執行,但它不應該?嗯 –
你在說什麼。睡眠與我的評論無關。我正在談論%filename%變量。當你在一個代碼塊中並且你設置了一個變量,並且你需要在該代碼塊中使用該變量時,你需要使用延遲擴展。 – Squashman