jeudi 25 janvier 2018

Why is my "Else" not working in my batch file?

This should be simple but it's really confusing me at the moment and I can't figure out why. I am reading a URL from a text file in the same location as the batch file but I can't get the part "Else (ECHO DOMAIN)" to work. The (ECHO DOMAIN and GROUP) works however.

The URL in the "Upgrade.txt" is provided to me in the format of:

* CLIENT01 - NAME OF CLENT (Live) Site URL (https://address1.ourdomain.co.uk)

This is the batch file I can't get to work:

@ECHO OFF
setlocal enabledelayedexpansion
PUSHD "%~dp0"

for /f "tokens=2 delims=/." %%d IN (Upgrade.txt) do (
        SET "Domain=%%d"
    )

for /f "tokens=4 delims=/)" %%g IN (Upgrade.txt) do (
    SET "Group=%%g"
    IF "!Group!" NEQ "" (ECHO DOMAIN and GROUP) ELSE (ECHO DOMAIN)  
)

ECHO Domain=!Domain!
ECHO Group=!Group!

PAUSE

Running it gets me the result

Domain=address1
Group=

but is should be:

DOMAIN
Domain=address1
Group=

However if I change the URL in the "Upgrade.txt" to this format of:

* CLIENT01 - NAME OF CLENT (Live) Site URL (https://address1.ourdomain.co.uk/address2)

Running it gets me the correct result of:

DOMAIN and GROUP
Domain=address1
Group=address2

Why is the "Else (ECHO DOMAIN)" not being printed and both ECHO's are ignored completely? (The Echo's will be replaced with commands so I need to know each part is being called correctly and the Echos at the bottom are just to prove to myself what the values are so I know what action should be called)

Many thanks in advance

Aucun commentaire:

Enregistrer un commentaire