5

hi i have a big problem in batch, its kind of complicated to tell, but i figured out the way to solve it, the problem is i didnt know how to do it in batch, if in c# i can do it easily since im new in batch, below is c# , can u guys teach me how to do exactly like that in batch? i google'd whole day but cannot find a way, thanks in advance

ArrayList list = new ArrayList();
//let say variable "Filesx" consist of files count in one folder

for(int i = 0; i < Filesx; i++){
   list.Add("file number : " + i);
}

P/S: if arraylist is not possible in batch, array alone is ok

6
  • Are you trying to get an array with the numbers 0 to Filesx - 1? Or do you want to do something more useful (like a list of names, etc)? Commented Jul 19, 2013 at 0:57
  • 1
    Did you see stackoverflow.com/questions/138497/… ? Combine that with stackoverflow.com/questions/9448651/… and you have your answer. Commented Jul 19, 2013 at 1:01
  • thanks for the response, perhaps, but my purpose is only to POPULATE AN ARRAY INSIDE LOOP for next operation, is it possible in batch? Commented Jul 19, 2013 at 1:04
  • ok i will try to look into it,thanks anyway Commented Jul 19, 2013 at 1:04
  • the second link u provided solve my problem, many thanks, so array in batch doesnt need to specify the size beforehand, just like arraylist, so cool, so sad i cant mark u as an answer coz its in COMMENT Commented Jul 19, 2013 at 1:17

1 Answer 1

6
@echo off
setlocal EnableDelayedExpansion

rem Populate the array with existent files in folder
set i=0
for %%a in (*.*) do (
   set /A i+=1
   set list[!i!]=%%a
)
set Filesx=%i%

rem Display array elements
for /L %%i in (1,1,%Filesx%) do echo file number %%i: "!list[%%i]!"

You must note that, for convenience, subscripts in Batch arrays should start at 1, not 0.

For further description on array management in Batch files, see: Arrays, linked lists and other data structures in cmd.exe (batch) script

Sign up to request clarification or add additional context in comments.

6 Comments

It should be noted that whenever you need to use arrays, linked lists or other higher order data structures in batch you should seriously re-evaluate your choice of language.
@AnsgarWiechers: Are you suggesting that a guy that does not know any other language, but requires (or want to learn about) these features must learn another language and not use they in Batch? It seems that nobody had realized that Batch programming language may serve as a valuable learning tool!
I'm suggesting that someone who requires these features is far better off investing his time in learning a newer and more versatile language than wasting it on working around the limitations of old and busted languages like batch. YMMV.
@AnsgarWiechers Why would you make that suggestion here though, in the context of trying to help @paiseha? This person may very well have no say in the matter of what language may be used. In that case, you may very well be correct, but that doesn't really help.
@AnsgarWiechers You are the one who seems to want to start an argument, not me. Good day.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.