Take the 2-minute tour ×

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!
share|improve this question
I suggest you to see this answer: stackoverflow.com/questions/10166386/… –  Aacini May 12 '12 at 5:19
add comment

3 Answers

up vote 3 down vote accepted

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!

: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.

share|improve this answer
yeah thats about the same solution i came up with, thanks. Any reason why you used quotation marks? –  user1229895 May 11 '12 at 6:15
@user1229895 - Quotes aren't needed, but they make it very explicit where the SET assignment ends. As long as the opening quote is before the variable name, then any text after the final quote is ignored. SET "var=value" junk sets var to value and junk is ignored. It makes it visually obvious that there is a space at the end of the prompt, and it guards against inadvertent additional spaces in both SET statements. –  dbenham May 11 '12 at 11:17
add comment

This is not an answer, but a large comment as reply to dbenham's answer.

My viewpoint about this matter is precisely the opposite one to dbenhams's. In my opinion, in answers to Batch topics we must be very concise and clear, particularly with beginners, and provide the minimum info that aid to solve the problem, but not to overwhelm and make the answers confuse. There are two ways of simulate arrays in Batch: via the widely used square brackets to enclose the subscript, or not, but in both cases the concept is the same: select a particular element from a list of variables with same name via a numeric index.

If square brackets are not used, a beginner would not understand the core concept; instead they might think that the Batch file used a "strange trick" to solve the problem. An experienced programmer, on the other hand, would say for sure: "Hey! This is an array, but written in a different way". There is no way to mistake this feature for any other different thing, and no reason to disguise it as other different thing.

If square brackets are used, a beginner will found tons of information about "array" concept and plenty of examples on this topic in many programming languages, and even descriptions independent of any programming language that, of course, are of immediate use in Batch files. The use of square brackets in this case is much more clear that not do so. However, some people think that these benefits does not worth the "confusion" of assuming that Batch "formally support arrays".

In my opinion, the central point of this matter is not to discuss if Batch formally support arrays or not, but the fact that any people can use the array concept to manipulate data in Batch files. I see no problems at all with the possibility that some people may think Batch properly support arrays, especially the beginners; array management is different in different programming languages, so just would be necessary an explanation of Batch details. However, if a more formal discussion of this subject would seem to be important, I would like to state a different example.

Do you know that the C programming language does NOT "formally support" any Input-Output operation? When Dennis Ritchie devised it, he specifically left these operations outside the language design in order to keep the compiler small and compact. Does this mean that you can NOT read/write any data in C programs? Of course not! This just means that these operations are implemented outside the compiler via function libraries, so if you need to develop a C compiler, you need not be worried about how to compile PRINT, READ or any other I/O statement because they just does not exist in C language!

Interesting, isn't it?

Therefore, if a beginner would ask: "How to write a message in C language?", do you think the right answer should be: "You can't. C language does not formally support any I/O statement, but you can emulate such operations via library functions"? Of course not! Most people just would answer describing printf, but practically no one mentioned that printf() is NOT part of the C language and I think this is correct. In the ultimate analysis, what is the problem if someone may think that the C language supports I/O operations? People can use I/O operations in C programs no matter the way they were implemented, right?

Well, in my modest opinion, the same approach should be used in the case of arrays in Batch...


share|improve this answer
@RichardHarrison, user66001: thank you both for your kindly corrections. English is not my milk tongue! Antonio –  Aacini Jun 28 '12 at 1:16
My pleasure - although generally we'd say mother tongue or native language.. –  Richard Harrison Jun 28 '12 at 16:43
add comment

See this answer on Super User:


share|improve this answer
I don't believe thats putting it into an array though? I'm trying to get the directories into an array, and offer them later as a numerical choice. –  user1229895 May 11 '12 at 2:42
thanks preet! ;-) –  user1229895 May 11 '12 at 3:29
add comment

Your Answer


