Batch – Choose a directory by number

If you like your users to manually choose a directory in you filesystem but do not want them to type in the complete name of it, just use following script.

@echo off
setLocal EnableDelayedExpansion
set i=0
set c=0
for /f "tokens=*" %%a in ('dir C:temp /AD /b') do (
 set /a i+=1
 set dir!i!=%%a
 if !i! leq 9 (echo [ !i!] %%a)
 if !i! gtr 9 (echo [!i!] %%a)
set /p choice="Please choose: "
if !dir%choice%! equ ^!dir^%choice^%^! echo Your choice !dir%choice%! was not found. &goto choose
echo Your choice was !dir%choice%!
If the users executes the script there is a list of all directories created. If the user chooses a directory and before this very directory was delete the value of !dir%choice%! will be exactly the name of the value. This is why i catched this exception. Not a very common case but you could never know! 🙂
This works up to 99 directories (no directories not tested) otherwise the menu choice would not look that fine.