Smartsav10
Smartsav10

Reputation: 123

Batch File If Statements causing close of CMD and not working

I am creating a should-be-simple batch file that will allow me to input a class name and it will take me to the correct google classroom. However, my if statement doesn't work, even when I input the word "Social Studies". It does not take me to my classroom, and on top of that, the CMD is just closed. When I remove the If Statement line, the code works fine and the cmd just stays open after inputting a class.

    set /p class="Enter Class: "

    IF "%class%" /I EQU "Social Studies" (START https://classroom.google.com) 
    
    cmd /k

Upvotes: 0

Views: 1403

Answers (2)

Magoo
Magoo

Reputation: 79983

IF /I "%class%" EQU "Social Studies"...

The parsing logic for an if statement is very specific; if [/i][NOT] arg1 op arg2 where /i and not are optional, but must if used, be used in that order.

Your code sees /i where it expects a comparison-operator and generates a syntax-error.

When you use the point-click-and-giggle method of executing a batch, the batch window will often close if a syntax-error is found. You should instead open a 'command prompt' and run your batch from there so that the window remains open and any error message will be displayed.

Upvotes: 1

Omer Tal
Omer Tal

Reputation: 371

You can write @echo off whice prevents the prompt and contents of the batch file from being displayed. I replaced the your EQ with == and now it works:

@echo off
set /p class="Enter Class: "

IF "%class%"=="Social Studies" (START https://classroom.google.com)
PAUSE

The PAUSE at the end will make the CMD remain open after it's done

Upvotes: 0

Related Questions