Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested If Exist statements in Batch File

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
like image 970
Brian Avatar asked Apr 01 '13 16:04

Brian


People also ask

Can you use if statements in batch files?

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.

What does %% mean in batch?

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.

What is %% f in batch file?

%%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'.


2 Answers

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.

like image 104
Magoo Avatar answered Sep 20 '22 21:09

Magoo


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
like image 44
Shmil The Cat Avatar answered Sep 22 '22 21:09

Shmil The Cat