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 Install. Same with the Folder2, and then if neither folder exists just skip to install.
But even when Folder1 doesn't exist, this still sets %Folder1% to equal 1. What am I missing/not doing?
Thanks!
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
One of the common uses for the 'if' statement in Batch Script is for checking variables which are set in Batch Script itself. The evaluation of the 'if' statement can be done for both strings and numbers.
Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.
%%parameter A replaceable parameter: in a batch file use %%G (on the command line %G) FOR /F processing of a text file consists of reading the file, one line of text at a time and then breaking the line up into individual items of data called 'tokens'.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With