Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Generic User Avatar

Request for translation (batch vertical bar)


  • Please log in to reply
3 replies to this topic

#1 ray5450

ray5450

  •  Avatar image
  • Members
  • 628 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:12:12 AM

Posted 19 March 2023 - 12:59 PM

I am requesting a translation in English words for the first line below/what it does.  I think it has something to do with an OR condition.  I would like to know exactly.

 

set /p d=Enter value: || set d=10000
echo %d%

 

Thanks.


Edited by hamluis, 21 March 2023 - 06:02 PM.
Moved from DOS to Programming - Hamluis.


BC AdBot (Login to Remove)

 


#2 ctigga

ctigga

  •  Avatar image
  • Members
  • 204 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:01:12 AM

Posted 19 March 2023 - 10:40 PM

I am requesting a translation in English words for the first line below/what it does.  I think it has something to do with an OR condition.  I would like to know exactly.

 

set /p d=Enter value: || set d=10000
echo %d%

 

Hello, it's been a while since I've written a serious batch script.

Assuming the script syntax is correct (it looks plausible to me), my English description for the script's behavior would be:

 

"

Display "Enter value:" to the user and allow them to provide input. 

If the user provides input, it is stored in variable "d". 

If the user doesn't provide input (presses ENTER without specifying a value), variable "d" is set to 10000.

Finally, the value stored in variable "d" is displayed

"

 

Explanation:

The script uses logical-OR (||) to simulate an if/else branch construct.

The first line is really two expressions separated by logical-OR:

set /p d=Enter value:

||

set d=10000

 

With logical-OR, only ONE sub-expression needs to be TRUE (non-zero) for the overall logical-OR expression to be TRUE.

This can lead to so-called "short circuit evaluation", where the script only executes logical-OR sub-expressions until it finds the FIRST TRUE sub-expression.

 

In your example, "set /p d=Enter value:" is the first sub-expression.  If the user enters a value it is stored in "d", but the sub-expression evaluates to a TRUE (non-zero) value (indicating the user entered something).  Because a TRUE value logical-ORed with anything is always TRUE, the script won't even bother evaluating the "set d=10000" sub-expression.

 

If, however, the script evaluates "set /p d=Enter value:" and the user doesn't enter a value, that sub-expression will evaluate to FALSE.  This FALSE sub-expression will force the logical-OR expression to evaluate subsequent sub-expression(s) (if any) until it finds one that is TRUE. In your example, the next sub-expression is set d=10000 and so "d" is set to 10000.

In this specific case the result of the logical-OR expression is not used.

 

In pseudo code, your script could be written as:

 

DISPLAY "ENTER VALUE:"

INPUT = GET_INPUT()

IF (INPUT IS NOT EMPTY)

{

 D = INPUT

}

ELSE

{

 D = 10000

}

 

Good luck and happy scripting :)



#3 ray5450

ray5450
  • Topic Starter

  •  Avatar image
  • Members
  • 628 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:12:12 AM

Posted 23 March 2023 - 06:17 AM

Your explanation is very detailed.  You took a lot of time to do so.  Thank-you, so much.

 

I knew it was something like that.  I have never come across the "||" before. 

This is what I was looking for...the "a-ha" part:  "If the user doesn't provide input (presses ENTER without specifying a value), variable "d" is set to 10000."

 

This is not the actual script I will be using, but a simplified example to make it easier for someone to explain.

 

Thanks, again.



#4 ctigga

ctigga

  •  Avatar image
  • Members
  • 204 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:01:12 AM

Posted 25 March 2023 - 04:59 AM

No problem, happy to hear it was helpful.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users