Page 1 of 1

Dir /b output into an Array

Posted: 06 Mar 2012 11:27
by wbg
Hi All !!

I have looked every where and tried a thousand things but I can not get this to work ... What I want to do is run a DIR /b cammand and display the subdirectories found on the screen with a number (subscript) for future reference / selection - the output should be similar to the following ...
1 firstSubdir
2 scndSubdir
3 anotherSubdir
4 lastSubdir

I have tried using the for loop to display the number and the subdir name but i cannot increment the number - my code

set /a cnt=0
for /f "tokens=*" %%g in ('dir /b') do (
echo '%%g'
set /A cnt=%cnt% + 1
echo cnt=%cnt% )

The output ends up showing the directory name but always 0 for the number
- What am I doing wrong ????

Re: Dir /b output into an Array

Posted: 06 Mar 2012 11:52
by Squashman

Code: Select all

@echo off
setlocal enabledelayedexpansion
set /a cnt=0
for /f "tokens=*" %%g in ('dir /b') do (
   set /A cnt+=1
   echo !cnt! '%%g'
)
endlocal

Re: Dir /b output into an Array

Posted: 06 Mar 2012 12:32
by foxidrive
In the MSdos days to create a simple menu, the screen was generated like this:

Code: Select all

dir /ad /b |find /n /v ""

Re: Dir /b output into an Array

Posted: 06 Mar 2012 13:03
by Squashman
foxidrive wrote:In the MSdos days to create a simple menu, the screen was generated like this:

Code: Select all

dir /ad /b |find /n /v ""

Totally forgot about the FIND usage. Thanks foxidrive.

Re: Dir /b output into an Array

Posted: 06 Mar 2012 15:22
by wbg
Awesome !!!

Thank you Squashman & Foxidrive :D