i'm trying create windows version of simple bash script have, cannot seem find windows version of unix 'select' command. there one?
here's script:
#!/bin/bash echo "enter number of file want select:" select the_file in somedir/*; echo "you picked $the_file ($reply)" # stuff here. break; done
it easy find linux example, bit perplexed cannot seem find windows equivalent.
edit: select command prompts user select file given directory (that's oversimplification). clarify, script above produce following output, assuming there subdirectory 'somedir' 3 text files in it.
enter number of file want select: 1) somedir/somefile1.txt 2) somedir/somefile2.txt 3) somedir/somefile3.txt #? 2 picked somedir/somefile2.txt (2)
windows batch commands not include equivalent select
command. have build own version.
you need:
call
command. create subroutine , reuse itfor
command iterate on files, orfor /f
iterate on output of command returning list of files- for short lists,
choice
command more friendly user not needed press enter, longer lists or if don't know number of files select,set /p
better option
here, sample
@echo off setlocal enableextensions disabledelayedexpansion call :select "somedir\*" the_file echo picked %the_file% goto :eof :select mask returnvar setlocal enableextensions disabledelayedexpansion rem configure internal variables set "filenumber=" set "maxfiles=-1" /f "delims==" %%a in ('2^>nul set f[') set "%%a=" echo enter number of file want select: rem search files, show list , create array file list rem using xcopy list of files because show rem relative paths when relative mask used input /f "tokens=1,* delims=:" %%a in (' xcopy "%~1" "%temp%" /l ^| findstr /n /r /c:"[\\\.:]" ') ( echo %%a^) %%b set "f[%%a]=%%~fb" set "maxfiles=%%a" ) rem prompt :select.ask set /p "filenumber=#? " rem validate input set /a "filenumber=filenumber+0" 2>nul if %filenumber% gtr %maxfiles% set "filenumber=-1" if %filenumber% lss 1 set "filenumber=" if not defined filenumber ( echo wrong selection goto :select.ask ) rem retrieve file array setlocal enabledelayedexpansion %%a in ("!f[%filenumber%]!") ( endlocal set "selectedfile=%%~a" ) rem return selection caller endlocal & set "%~2=%selectedfile%" goto :eof