Ok, I\'m trying to do a couple nested IF EXIST statements to check for the presense of a couple folders. If the first folder exists, set Folder1 to equal 1, and then skip to
if exist "c:\folder1" (
set Folder1=1
echo %Folder1%
goto install
) if exist "c:\folder2" (
set Folder2=1
echo %Folder2%
goto Install
) else goto Install
:Install
Two fundamental problems:
A compound statement must be parenthesised.
Within parentheses, changing a variable value will NOT be visible UNLESS you have executed a SETLOCAL ENABLEDELAYEDEXPANSION
- and even then you'd need to use !var! not %var%
So:
SETLOCAL ENABLEDELAYEDEXPANSION
if exist "c:\folder1" (
set Folder1=1
echo !Folder1!
goto install
) else if exist "c:\folder2" (
set Folder2=1
echo !Folder2!
goto Install
) else goto Install
:Install
Or preferably,
@ECHO off
if exist "c:\folder1" (
set Folder1=1
goto install
) else if exist "c:\folder2" (
set Folder2=1
goto Install
) else goto Install
:Install
SET folder
Or even simpler
@ECHO off
if exist "c:\folder1" set Folder1=1&goto install
if exist "c:\folder2" set Folder2=1&goto Install
:Install
SET folder
Test:
@ECHO OFF
setlocal
SET "folder1="
SET "folder2="
ECHO.----------No folders
DIR /b /ad c:\folder*
CALL :test
ECHO.----------Folder 1 only
MD c:\folder1
DIR /b /ad c:\folder*
CALL :test
ECHO.----------Folder 2 only
RD c:\folder1
MD c:\folder2
DIR /b /ad c:\folder*
CALL :test
ECHO.----------Both
MD c:\folder1
DIR /b /ad c:\folder*
CALL :test
RD c:\folder1
RD c:\folder2
GOTO :eof
:test
if exist "c:\folder1" set Folder1=1&goto install
if exist "c:\folder2" set Folder2=1&goto Install
:Install
SET folder
SET "folder1="
SET "folder2="
GOTO :eof
This test creates and deletes the two directories in question.
Here's the result:
----------No folders
----------Folder 1 only
folder1
Folder1=1
----------Folder 2 only
folder2
Folder2=1
----------Both
folder1
folder2
Folder1=1
Note that
SET "folder1="
SET "folder2="
Which is executed both at the start and after each report ensures that the environment variables in question are removed from the environment to prevent the code giving false results on stale information.
This code doesn't set %folder1%=1 if folder1 doesn't exists, and it produces no output in this case. If Folder1 doesn't exists AND Folder2 exists THEN %folder2% is set to 1, otherwise %folder2% is empty. Put an unclosed left parentheses after the echo
command to suppress the output, if a variable is empty.
@echo off &setlocal
if exist "c:\folder1" set "Folder1=1"
echo(%Folder1%
if not defined Folder1 if exist "c:\folder2" set "Folder2=1"
echo(%Folder2%
goto Install
:Install
endlocal