-1

How to create five different batch files in five directories respectively?

@echo off
setlocal enabledelayedexpansion

set "ba[0]=v1"
set "ba[1]=v2"
set "ba[2]=v3"
set "ba[3]=v4"
set "ba[4]=v5"

set folderList=f1 f2 f3 f4 f5
 
set /A k=0
for %%a in (%folderList%) do (
    mkdir %%a
    set batNumber=!ba[%k%]!
 
    >"C:\testBat\%%a\test.bat" (
     
        echo @echo off
        echo:
        echo "!batNumber!"
        echo pause
    ) 
    set /A k+=1
)

I only get v1 on every batch file. How do I get v2, v3, v4, v5 on each file respectively?

Compo
  • 36,585
  • 5
  • 27
  • 39
hhh
  • 1
  • replace `set batNumber=!ba[%k%]!` with `call set batNumber=%%ba[!k!]%%`. [Delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) is a bit unintuitive at times... – Stephan Aug 03 '23 at 17:22
  • 1
    You could also have an additional `for` loop to process `"!k!"` and then use `set "batNumber=!ba[%%B]!"` (or whatever variable you pick). – SomethingDark Aug 03 '23 at 17:26
  • BTW, if you're going to use ```"C:\testBat\%%a\test.bat"```, surely you should be using ```MD "C:\testBat\%%a"``` instead of ```mkdir %%a```. – Compo Aug 03 '23 at 19:20
  • All these methods are described in detail at [this answer](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990)... – Aacini Aug 04 '23 at 03:45

0 Answers0