If/Else in 1 line--only do something from Else statement

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

If/Else in 1 line--only do something from Else statement

#1 Post by Jer » 17 Jan 2020 16:53

What is the best coding form in writing this If/Else statement on 1 line?
I have "echo OK" where nothing should be done. What should I replace it with?
"(rem)" acted as if the batch file did nothing.
Sorry if this topic was covered somewhere in history and I forgot the solution :?
Thanks.
Jerry

Code: Select all

@echo off
setlocal
set "code=100"

If %code% gtr 0 If %code% leq 255 (echo OK) else set "code="

If defined code (echo %code% is within limits) else echo value is NOT within limits

If %code% gtr 0 If %code% leq 255 (
  rem
) else (
  set "code="
)
echo after code block, code is %code%

DQ2000
Posts: 38
Joined: 07 Aug 2019 17:26

Re: If/Else in 1 line--only do something from Else statement

#2 Post by DQ2000 » 17 Jan 2020 17:23

@echo off
setlocal
set "code=100"

If %code% gtr 0 If %code% leq 255 (echo OK) else set "code="

If defined code (echo %code% is within limits) else echo value is NOT within limits

If %code% gtr 0 If %code% leq 255 () else (set "code=")

echo after code block, code is %code%

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: If/Else in 1 line--only do something from Else statement

#3 Post by Jer » 17 Jan 2020 19:20

DQ2000, what happened when you tested your solution?
I get the error message: ) was unexpected at this time.

Something like this works:

If %code% gtr 0 If %code% leq 255 (set .=) else set "code="

but I'm asking to see if if there's a right way to do this, as I am
prone to do it the wrong way first.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: If/Else in 1 line--only do something from Else statement

#4 Post by penpen » 18 Jan 2020 04:47

Jer wrote:
17 Jan 2020 16:53
What is the best coding form in writing this If/Else statement on 1 line?
That depends on how you define the term "best coding" (the fastest, the easiest to read, the most beautiful to read, ...) and what exactly you want to change (that part "(echo OK)" only, ..., all).

For example you could use:

Code: Select all

If %code% gtr 0 If %code% leq 255 ( rem: ) else set "code="
I'm also unsure if you want to test for negative numbers and empty variables (this might help you; untested):

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
set "code=255"

set /a "test=%code:~0,4%>>8"
if "%test%" == "0" (echo(%code% is within limits) else echo(value is NOT within limits

goto :eof
penpen

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: If/Else in 1 line--only do something from Else statement

#5 Post by Jer » 18 Jan 2020 11:25

penpen, I will use your "( rem: )" solution to do nothing when a numeric value passes the lower and upper limits test.
Speed in not an issue because only a few values are checked from a command-line entry.
Thanks for your solution:
If %code% gtr 0 If %code% leq 255 ( rem: ) else set "code="
Entries are pre-checked in a function for being numeric, and "0" returned if not a positive integer.
Each code is converted to the character in a batch-only tool to compose an angular shape
from ascii characters.

DQ2000
Posts: 38
Joined: 07 Aug 2019 17:26

Re: If/Else in 1 line--only do something from Else statement

#6 Post by DQ2000 » 18 Jan 2020 16:36

Sin título6.png
Sin título6.png (4.59 KiB) Viewed 7215 times

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: If/Else in 1 line--only do something from Else statement

#7 Post by Jer » 18 Jan 2020 16:53

My O/S: Microsoft Windows [Version 10.0.18362.592]
Obviously your Windows version is superior to mine :wink:

Code: Select all

@echo off
setlocal
set "code=100"
If %code% gtr 0 If %code% leq 255 () else set "code="
If defined code (echo %code% is within limits) else echo value is NOT within limits
C:\Temp\DOSBatch>test5
) was unexpected at this time.
C:\Temp\DOSBatch>
with echo on:
) was unexpected at this time.
C:\Temp\DOSBatch>If 100 gtr 0 If 100 leq 255 () else set "code="

Post Reply