0

I'm trying to retrieve the min value in an array that I have created calling this from another batch file.

Array has been created fine but the for /l is not working. I think, there is something with the if statement:

@echo off

for /f "usebackq" %%a in ('%2') do set d=%%~a
for /f "usebackq tokens=* delims=%d%" %%G in ('%3')  do set %1=%%~G

set /a i=-1

for %%h in (!%1!) do (
    set /a i+=1
    set %1[!i!]=%%h
)

if %4==min (
    set /a n=%i%
    for /l %%j in (0,1,%n%) do (
        if %%j==0 (
            set %4=!%1[%%j]!
        ) else (
            if !%1[%%j]! lss !%4! (
                set %4=!%1[%%j]!
            )
        )
    ) else (
        set %4="Please write the name of the function correctly"
    )

:::: below the file im calling this function

@echo off
setlocal EnableDelayedExpansion
call test char "," "30,10,40" min

echo !min!
:: char is %1
:: "," is %2
:: "30,10,40" is %3
:: min is %4
pause
Shahim Khlaifat
  • 63
  • 1
  • 1
  • 9
  • 1
    your code is nearly unreadable without context (which parameter means what). But at least you already correctly identified, where your problem is: `for /l`does not run correctly in the `if`block, because `%n%` is not defined. See the [delayed expansion trap](http://stackoverflow.com/a/30284028/2152082) for why it isn't in spite of `set /a n=%i%` – Stephan Jul 19 '15 at 17:04
  • now after i edit my post you can see the input parameters. – Shahim Khlaifat Jul 19 '15 at 18:27

1 Answers1

0

I rearranged your code in order to test it and modified some parts to fix a couple details. See my comments in UPPERCASE letters in the code:

@echo off
setlocal EnableDelayedExpansion
call :test char "," "30,10,40" min

echo !min!
:: char is %1
:: "," is %2
:: "30,10,40" is %3
:: min is %4
GOTO :EOF


:TEST

REM for /f "usebackq" %%a in ('%2') do set d=%%~a
REM Previous line is equivalent to this one:
set d=%~2

REM for /f "usebackq tokens=* delims=%d%" %%G in ('%3')  do set %1=%%~G
REM I don't understand what you want to do in previous line,
REM but the next two lines replace the delimiter in %d% by spaces:
set %1=%~3
set %1=!%1:%d%= !

set /a i=-1

for %%h in (!%1!) do (
    set /a i+=1
    set %1[!i!]=%%h
)

if %4==min (
    set /a n=%i%
    REM The next line requires delayed expansion in !n! variable
    REM or use %i% instead
    for /l %%j in (0,1,!n!) do (
        if %%j==0 (
            set %4=!%1[%%j]!
        ) else (
            if !%1[%%j]! lss !%4! (
                set %4=!%1[%%j]!
                REM The next right parentheses was missing
            )
        )
    )
) else (
    set %4="Please write the name of the function correctly"
)

EXIT /B
Aacini
  • 65,180
  • 12
  • 72
  • 108