0

i neeed to Write a bat file that accepts three parameters. Of these three parameters, find the smallest one, then display all the numbers from 1 to AND the smallest parameter on the screen. i tried this but it does not work

@ECHO OFF
set /a c=%1
set /a b=%2
set /a a=%3

set Smallest=%d%
if %c% lss %Smallest% set Smallest=%c%
if %b% lss %Smallest% set Smallest=%b%
if %a% lss %Smallest% set Smallest=%a%

Echo Smallest number is %Smallest%
pause>nul
mosess
  • 1
  • 3
    `set Smallest=999999999`. `d` is not defined, so `smallest` would be set to *nothing*. – Magoo Feb 04 '23 at 10:48
  • soo like set /a d=%999999999 – mosess Feb 04 '23 at 11:00
  • Depending upon the way your integers look, you may be able to do it like this: **1.** ```@For /F %%G In ('"(For %%H In (%*) Do @Echo(%%H) | %SystemRoot%\System32\sort.exe /R"') Do @Set "smallest=%%G"```, **2.** ```@Echo(%smallest%``` – Compo Feb 04 '23 at 11:14
  • ok it did with the way that magoo showed me but how do i do the display part – mosess Feb 04 '23 at 11:17
  • `set /a d=%999999999` would likely work - but by failing to fail. The `%9` means the 9th parameter, which is likely to be empty, so `d` would be set to `99999999` that is, 8 `9`s. What do you mean by "all the numbers from 1 to AND" - an example would be good. – Magoo Feb 04 '23 at 12:19
  • No need to define `%d%`, just change the line `if %c% lss %Smallest% set Smallest=%c%` by the command `set Smallest=%c%`… – aschipfl Feb 04 '23 at 13:14

2 Answers2

0

Here is an example code for this homework which outputs the smallest number and all numbers from 1 to the smallest number if the smallest number is not less than 1.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "Smallest=%~1"
if not defined Smallest goto OutputHelp
if "%~2" == "" goto OutputHelp

for %%I in (%*) do if %%~I LSS %Smallest% set "Smallest=%%~I"
echo The smallest number is: %Smallest%
echo(
if %Smallest% GEQ 1 (
    for /L %%I in (1,1,%Smallest%) do echo %%I
) else (
    for /L %%I in (1,-1,%Smallest%) do echo %%I
)
goto EndBatch

:OutputHelp
echo %~nx0 must be run with at least two integer numbers as arguments.
echo(
echo Example: %~nx0 489 0x2AF 0715
echo(
echo    An integer number beginning with 0x is interpreted hexadecimal.
echo    An integer number beginning with 0 is interpreted as octal number
echo    which cannot contain the digits 8 and 9 to be valid. An invalid
echo    octal number is interpreted with value 0.
echo(
echo    There is not checked if an argument string is a valid string for
echo    a decimal, hexadecimal or octal integer number in the value
echo    range -2147483648 to 2147483647 (32 bit signed integer).

:EndBatch
echo(
pause
endlocal

The output of this batch file on running it with the three numbers of the example without the output of all numbers from 1 to smallest number is:

The smallest number is: 0715

The octal number 0715 is decimal 461 and the hexadecimal number 0x2AF is decimal 687 which makes the octal number 0715 the smallest number.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • set /?
  • setlocal /?

I recommend to read also:

Mofi
  • 46,139
  • 17
  • 80
  • 143
0

This is the way I would do it:

@echo off
setlocal

set /A c=%1, b=%2, a=%3
set /A "comp=((a-b)>>31)+1,one=!comp*a+comp*b,comp=((one-c)>>31)+1,Smallest=!comp*one+comp*c"

echo Smallest: %Smallest%
for /L %%i in (1,1,%Smallest%) do echo %%i

That is, if a > b then (a-b) is positive or zero (else, is negative), and (a-b)>>31 is zero (else, is -1 because >> is a signed shift of 31 bits) so finally ((a-b)>>31)+1 is one if a > b or is zero if a < b. In this way, we use this value comp to get b, or not this value !comp! to get a; and store the lesser of both in one variable.

The same method is used to get the lesser of one and c.

At end, we show all values from 1 to the lesser...

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • ok thanks its for a project and i was having a hard time on it because we im new in the bat files programming soo thx – mosess Feb 06 '23 at 13:19
  • Ok. If this answer is useful to you, it is customary to select it as Best Answer by checking the green check mark. Thanks! – Aacini Feb 07 '23 at 05:20