Page 1 of 1

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

Posted: 17 Jan 2020 16:53
by Jer
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%

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

Posted: 17 Jan 2020 17:23
by DQ2000
@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%

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

Posted: 17 Jan 2020 19:20
by Jer
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.

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

Posted: 18 Jan 2020 04:47
by penpen
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

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

Posted: 18 Jan 2020 11:25
by Jer
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.

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

Posted: 18 Jan 2020 16:36
by DQ2000
Sin título6.png
Sin título6.png (4.59 KiB) Viewed 8690 times

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

Posted: 18 Jan 2020 16:53
by Jer
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="