I have this code here simply trying to create a file that the name is chosen from a randomized array. Why does it output incorrectly?
Code:
@echo off
set i=0
for %%a in (Cow Dog Bird Fish Meerkat Cat) do (
set /A i+=1
set operator[!i!]=%%a
)
set /a operator=%random%%%4+1
set operator=!operator[%operator%]!
copy /y NUL %operator%>NUL
The file created is titled something like !operator[3]!
instead of one of the strings in the array. Why is this the case? Any help is appreciated!
Alex K identified your primary problem in his comment - you are missing
setlocal enableDelayedExpansion
.But you have another problem - your array contains 6 values, but you are randomly selecting an index between 1 and 4 instead of between 1 and 6.
You should use
set /a operator=%random%%%i+1
. Note that true environment variables do not need to be expanded when used with SET /A.