Gabe
Gabe

Reputation: 27

My batch files if else statement isn't working

So I have created a file that works and when testing it without the if/else statement. When I put in this in a if statement it doesn't work at all though, is my syntax wrong or what? Thanks.

@echo off
set /p id= Folder Name:
set /p yn= Subfolders? (y/n):

If %yn% == "y"
Set rootDirectory = Y:\
md %id

Set rootDirectory = Y:\%id%
md %id%\Source
md %id%\Work
md %id%\PrintFinal

%SystemRoot%\explorer.exe %id%

Else
Set rootDirectory = Y:\
md %id%

%SystemRoot%\explorer.exe %id%

Upvotes: 1

Views: 71

Answers (3)

Gary
Gary

Reputation: 13922

You need to enclose the blocks of code within parenthesis.

@echo off
set /p id= Folder Name:
set /p yn= Subfolders? (y/n):

If %yn% == "y" (
  Set rootDirectory = Y:\
  md %id%

  Set rootDirectory = Y:\%id%
  md %id%\Source
  md %id%\Work
  md %id%\PrintFinal
) else (
  Set rootDirectory = Y:\
  md %id%
)

%SystemRoot%\explorer.exe %id%

Upvotes: 1

PinoyDev
PinoyDev

Reputation: 1047

Here's an updated one

@echo off
set /p id= Folder Name:
set /p yn= Subfolders? (y/n):

  If %yn% == y
  Set rootDirectory = Y:\
  md %id

it works for me

Upvotes: -1

Marged
Marged

Reputation: 10973

You forgot to add ( and ) to group statements.

Call IF /? and you will get a help page which describes the syntax of the IF statement

Upvotes: 1

Related Questions