Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7933739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:25:08+00:00 2026-06-03T21:25:08+00:00

Is there a way to read in the output of a ‘dir’ command into

  • 0

Is there a way to read in the output of a ‘dir’ command into an array in a BAT file? Or would I need to output it to a file first, then read the file and delete the file after?

The purpose is to get a list of folders in a directory, append a number to each and then prompt the user for a numerical input to select a folder.

UPDATE : got it!

SETLOCAL EnableDelayedExpansion
SET /A c=1

FOR /F "tokens=*" %%F in ('dir /on /b /a:d /p %svnLOCAL%') DO ( 
    ECHO !c!. %%F
    SET dir_!c!=%%F
    SET /a c=c+1    
)

REM test array
ECHO !dir_4!
ENDLOCAL
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:25:09+00:00Added an answer on June 3, 2026 at 9:25 pm

    Batch does not formally support arrays, but you can emulate arrays using environment variables.

    @echo off
    setlocal enableDelayedExpansion
    
    ::build "array" of folders
    set folderCnt=0
    for /f "eol=: delims=" %%F in ('dir /b /ad *') do (
      set /a folderCnt+=1
      set "folder!folderCnt!=%%F"
    )
    
    ::print menu
    for /l %%N in (1 1 %folderCnt%) do echo %%N - !folder%%N!
    echo(
    
    :get selection
    set selection=
    set /p "selection=Enter a folder number: "
    echo you picked %selection% - !folder%selection%!
    

    In the code above, the “array” elements are named folder1, folder2, folder3…

    Some people use names like folder[1], folder[2], folder[3]… instead. It certainly looks more array like, but that is precisely why I don’t do that. People that don’t know much about batch see variables like that and assume batch properly supports arrays.

    The solution above will not work properly if any of the folder names contain the ! character – the folder name will be corrupted during expansion of the %%F variable because of delayed expansion. There is a work-around involving toggling the delayed expansion on and off, but it is not worth getting into unless it is needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to read the output of my command to array and store them
Is there a way to read off from an [external] xml (an xml file
Is there any way to read a file using sharpsvn................
Is there any way to read a TIFF file using AIR ? Looks like
Is there any way to read a file line by line in javascript, specifically
In C#/.NET (on Windows) is there a way to read a growing file using
I was wondering if there is any way to read the output of a
I would like to execute arbitrary command-line application and read its standard output as
Is there any way to read standard output/error from any, GUI and non-GUI, already
I would like to read the output of a zlib stream's inflate() into a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.