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

paiseha
  • 169
  • 1
  • 3
  • 10
  • 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)? – Floris Jul 19 '13 at 00:57
  • 1
    Did you see http://stackoverflow.com/questions/138497/batch-scripting-iterating-over-files-in-a-directory ? Combine that with http://stackoverflow.com/questions/9448651/bat-script-for-creating-array-from-txt-file and you have your answer. – Floris Jul 19 '13 at 01: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? – paiseha Jul 19 '13 at 01:04
  • ok i will try to look into it,thanks anyway – paiseha Jul 19 '13 at 01: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 – paiseha Jul 19 '13 at 01:17
  • I am glad you were able to figure this out with the link I provided. It's all about knowing what terms to google for... – Floris Jul 19 '13 at 02:11

1 Answers1

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

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • 1
    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. – Ansgar Wiechers Jul 19 '13 at 08:37
  • @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! – Aacini Jul 19 '13 at 21:48
  • 1
    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. – Ansgar Wiechers Jul 19 '13 at 22:23
  • 1
    @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. – Scott Nov 15 '19 at 15:50
  • @Scott Guess what else does not help: speculation about what the OP may or may not have a say in. 6 years later no less. – Ansgar Wiechers Nov 15 '19 at 15:58
  • 2
    @AnsgarWiechers You are the one who seems to want to start an argument, not me. Good day. – Scott Nov 15 '19 at 16:11