Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DIR output into BAT array?

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
like image 214
user1229895 Avatar asked May 11 '12 02:05

user1229895


2 Answers

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.

like image 87
dbenham Avatar answered Nov 23 '22 00:11

dbenham


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 humble opinion, the same approach should be used in the case of arrays in Batch.

Perhaps a closer example is this one: there are a lot of questions about "arithmetic operations in Batch" and the answer usually is: "use set /A command". I never saw an answer that indicate that "Batch files does not support numeric variables, just strings, but arithmetic operations can be emulated in several ways, like the set /A command". Why? It seems that the purists are "more purists" when they opine about arrays in Batch, but they don't cares about other topics, like numbers. I really don't understand the purpose of the frequent clarification about "batch does not support arrays"!

User rojo opined about this point:

It's a pedantic argument that offers no solution to the problems askers are trying to solve

(see his complete comment above this answer).

like image 40
Aacini Avatar answered Nov 23 '22 00:11

Aacini