!BAT

#load module if not already loaded
debug 0
delmod -l getstr.g4b || insmod /%grub%/getstr.g4b
debug 1

# %grub% must be set to path where g4b files are
if not exist ()/%grub%/LstFExt.g4b echo grub variable not correct! && goto :EOF

if "%2"=="" echo Usage: %0 <folder> <extension> && echo Returns: %^CHOSEN% && goto :EOF
if "%1"=="" goto :EOF

set CCC=0

:LOOP
set ask=
# clear screen if we loop
if not "%CCC%"=="0" clear
set CCC=1
set CHOSEN=
echo
getstr.g4b $$STRbp01
echo    %STR%
echo
call  ()/%grub%/LstFExt.g4b %1 %2 SILENT
# clear variables set by LstFExt.g4b that are not needed
set FILEPATH=
set FILENAME=
set FILEEXT=
set FILEDIR=

if "%FCOUNT%"=="0" echo NO %2 FILES! && exit

# IF ZZ then echo action for 0 choice
if not "%ZZ%"=="" echo 0 = %ZZ%
# clear for next time

# variables are at (md)0x900+0x10 as SET FILE_x=xxxxxx

#pause if fill screen
pager on

set FL=1
set ST=3

:DLOOP
#echo Look for FILE_%FL%
cat --locate="FILE_%FL%=" --skip=%ST% (md)0x900+0x10 > nul
if "%?%"=="0x0" goto :findisp
cat --locate="=" --skip=%?% --number=1 (md)0x900+0x10 > nul
if "%?%"=="0x0" goto :findisp
set EQ=%?%
#echo EQ=%EQ%
cat --locate=\x0d --skip=%?% --number=1 (md)0x900+0x10 > nul
if "%?%"=="0x0" goto :findisp
#echo found cr at %?%
set /a L=%?%-%EQ% > nul
if not "%DD%"=="" echo START=%ST%  EQ=%EQ% LENGTH=%L% && pause Paused in PickaFile.g4b... (Debug DD)
echo -n -e !BAT\n set A > (md)0x6000+1
cat --skip=%EQ% --length=%L% (md)0x900+0x10 >> (md)0x6000+1
#cat (md)0x6000+1
call (md)0x6000+1
echo -e %FL% = %A%
set /a FL=%FL%+1 > nul
set /a ST=%ST%+%L% > nul
goto :DLOOP


:findisp
pager off
echo
# v1.14 if only one choice then use that!
debug -1

getstr.g4b $$STRbp02
if "%ZZ%"=="" if "%FCOUNT%"=="1" echo -e %STR% && pause --wait=3 && debug 1 && set ask=1 && goto :jp1
if not "%ZZ%"=="" if "%FCOUNT%"=="0" pause --wait=3 No choice available!!!... && debug 1 && set ask=0 && goto :jp1
debug 1

getstr.g4b $$STRbp03
if "%ZZ%"=="" set /p ask=%STR% (1-%FCOUNT%) : 
if not "%ZZ%"=="" set /p ask=%STR% (0-%FCOUNT%) : 
echo

:jp1

if "%ZZ%%%ask%"=="0" goto :LOOP
###set ZZ=
if "%ask%"=="" set EQ= && set ST= && set FL= && set CCC= && set L= && set ZZ= && exit
set /a ask=%ask%+0 > nul
if "%ask%"=="0" set CHOSEN=0 && exit
if %ask%<=%FCOUNT% goto :OK
goto :LOOP
:OK


# get string from (md)0x900+10

cat --locate="FILE_%ask%=" (md)0x900+0x10 > nul
#echo Found string at %?%
cat --locate="=" --skip=%?% --number=1 (md)0x900+0x10 > nul
#echo Found = at %?%
set EQ=%?%
cat --locate=\x0d --skip=%?% --number=1 (md)0x900+0x10 > nul
#echo Found CR at %?%
set /a L=%?%-%EQ% > nul
echo -n -e !BAT\n set CHOSEN > (md)0x6000+1
cat --skip=%EQ% --length=%L% (md)0x900+0x10 >> (md)0x6000+1
call (md)0x6000+1
set CCC=
set EQ=
set L=
set ZZ=
set ask=
set ST=
set FL=
set A=
# clear variables set by LstFExt.g4b
set FCOUNT=
set FILEPATH=
set FILENAME=
set FILEEXT=
set FILEDIR=
#echo CHOSEN FILE=%CHOSEN%
# check if user entered a valid filename
if not exist %1/%CHOSEN% echo CANNOT FIND %1/%CHOSEN% !!! && pause --wait=3 && exit
exit
