-1

Using the following code I am able to create a list of folders in a directory -

Setlocal EnableDelayedExpansion
set /p pattern=Enter Search Term:
echo.
dir /b /A:D %pattern%*

For example, if the user inputs con, the result may be

-construction1

-construction2

-construction_Docs

etc.

I would like to be able to attach a value to each item eg.

1_Construction1

2_Construction_Docs

etc.

Am I correcting in thinking that I would have to output the initial list I created to a .txt and then read and attach a variable to every line?

The end result would be a user being able to select one of the items based on the number we attach to it, and then have further actions being taken on that item.

Joshua Patterson
  • 433
  • 1
  • 4
  • 15
  • 1
    The last time I saw a menu system coded as batch scripts was before Windows 3.1. It's certainly possible but are you sure that vbscript or powershell wouldn't get you a faster, easier to maintain solution? I realise that this falls into the category of "not helping" but I've wasted a lot of time trying to make batch scripts do things they weren't built for. – christutty Jan 13 '16 at 02:05
  • 2
    @christutty - I've found that unless you're manipulating GUIs or doing math with numbers that aren't integers or larger than 2^32, batch is perfectly suitable to the task. – SomethingDark Jan 13 '16 at 02:14
  • I only have experience with batch, and a limited experience at that. A user selecting one of the items from the list is just about as complicated as I plan on making it, the rest of the batch will be things like robocopy and tree. I am open to other solutions if they're not too complicated however! Thanks for the input – Joshua Patterson Jan 13 '16 at 02:15
  • 1
    And to answer the question, you don't need the txt file at all; just run the `dir` command through a `for /f` loop and use a counter variable to put a number in front of each result. – SomethingDark Jan 13 '16 at 02:16
  • Could you explain that a little more? I'm unsure as to what you mean by counter variable. And how to run dir through a loop!! Thanks – Joshua Patterson Jan 13 '16 at 02:28
  • 1
    I would pipe the output of the dir command to the findstr command in a FOR command to number the lines. Then you could also create an array to easily acces the option the user selected. – Squashman Jan 13 '16 at 02:55

2 Answers2

3
dir /b /A:D %pattern%*|find /n /v ""

would yield

[1]construction1
[2]construction2
[3]construction_Docs

or

dir /b /A:D %pattern%*|findstr /n /v ":"

would yield

1:construction1
2:construction2
3:construction_Docs

You could then try

set /a max=0
for /f "delims=:" %%a in ('dir /b /A:D %pattern%*^|findstr /n /v ":" 2^>nul') do set /a max=%%a

which would set max to 0 if none found or the maximum number

  • The ^ tells batch that the pipe/> is part of the command to be executed
  • 2>nul suppresses error messages in the case of no matches found

It's then a simple matter of

if %max%==0 echo none found&goto askagain
set /p "selection=Please choose [1..%max%] ? "
for /f "tokens=1*delims=:" %%a in ('dir /b /A:D "%pattern%*"^|findstr /n /v ":" 2^>nul') do if "%%a"=="%selection%" set "dirselected=%%b"&goto found
echo %selection% is invalid
goto askagain
:found
echo %dirselected% selected.

to make a complete job.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Excellent answer but I'd add the explanation text as comments so that anyone that tries to maintain the final script has some idea of how it works. IMHO batch scripts this complex need to be commented at the same level as assembler. – christutty Jan 13 '16 at 04:25
  • This works up until I try and select one of the options - I get the error "Do is not a recognized command" - where did I go wrong? – Joshua Patterson Jan 13 '16 at 04:55
  • I **think** I made it work by removing the last 'do' command on the 3rd line of your code and changing %%b to %%a – Joshua Patterson Jan 13 '16 at 05:00
  • 1
    Soz - You'd also need to add the 'tokens=1*' in that line to assign the dirname to `%%b`. It would also be a good idea to `"quote %pattern%* throughout"` to guard against spaces in input. You could also use `"*%pattern%*"` to do a partial-match-anywhere rather than a partial-match-at-start. (happens all the time when I just enter freehand - normally, I test then cut-and-paste... – Magoo Jan 13 '16 at 05:17
3

This task is relatively simple to achieve if you use the right tools, like an array:

@echo off
setlocal EnableDelayedExpansion

set /P "pattern=Enter Search Term: "
echo/

rem Get the folders, store they in the array and show the menu
set "num=0"
for /D %%f in (%pattern%*) do (
   set /A num+=1
   set "folder[!num!]=%%f"
   echo !num!- %%f
)
if %num% equ 0 echo No folders found & goto :EOF
echo/

:selectFolder
set /P "num=Enter the desired folder: "
echo/
if not defined folder[%num%] goto selectFolder

set "item=!folder[%num%]!"
echo Folder selected: %item%

For further details on array management in Batch files, see this post.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • congrats on this one! I try it, its so nice. Two things if can be added will be so nice. 1) Search term to be able more than one word. two words to be like a group, sometimes you need to search first name and last name together. 2) a way to choose all the results in one variable. – Ionut Bejinariu Jan 18 '23 at 18:10