Reputation: 5471
I have a problem whereby I am trying to determine if given directory exists on file system before attempting to download a file.
batch file:
:: 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
)
Updated: I have added @aschipfl suggestion
the directory C:/Apache/apache-ant-1.9.7 DOES exist so the code should fail but when run it still downloads the file and tries to do further setups there. Any idea whats wrong and why the if statement is executed where it should not be ?
Thanks
Upvotes: 1
Views: 1205
Reputation: 80033
Labels are not allowed within a block (series of instructions within parentheses)
an md
will create intermediate directories if required.
Batch has no idea of procedures. If you call
a subroutine, then when the subroutine ends (reaches end-of-file or an exit
) execution will return to the instruction after the call
- so :UnZipFile
with your code.
Move the :unzipfile
routine to the end-of-file and insert a goto :eof
directly before it to ensure the code does not flow-through to :unzipfile
.
Add a goto :eof
to the end of :unzipfile
so that you can add extra code (like more subroutines) later. goto :eof
specifically means "go to physical end-of-file" which terminates the current routine.
note that setx
does NOT affect the current environment, nor does it affect existing cmd
instances, only new cmd
instances, hence execute both set
and setx
.
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
Upvotes: 1