I have a problem where I need to clone one virtual machine into several machines for production. The machines have names that is assigned by variables and a rdp port is also assigned by a variable. Both variables is increased by 1 at the end of the script. The problem I have is that I can't figure out how to loop the create machine and increase variable value code until the %M% value is at a defined number.
Here is my current code:
SET VBoxManage="C:\Program Files\Oracle\VirtualBox\VBoxManage.exe"
SET M=1
SET P=25553
if %%M < 4 (
%VBoxManage% clonevm Win2012 --mode all --name M%M% --register
%VBoxManage% modifyvm M%M% --vrde on --vrdeauthtype null --vrdemulticon on --vrdeport %P%
SET /A M=%M%+1
SET /A P=%P%+1
ECHO Done
ECHO %M%
ECHO %P%
)
ECHO All cloning finished.
pause
I have tried with FOR, IF and WHILE but I can't figure out how to get it to work.
How FOR did look like:
SET VBoxManage="C:\Program Files\Oracle\VirtualBox\VBoxManage.exe"
SET M=1
SET P=25553
FOR /L (if %%M IN (1,1,5)
(
%VBoxManage% clonevm Win2012 --mode all --name M%M% --register
%VBoxManage% modifyvm M%M% --vrde on --vrdeauthtype null --vrdemulticon on --vrdeport %P%
SET /A M=%M%+1
SET /A P=%P%+1
ECHO Done
ECHO %M%
ECHO %P%
)
ECHO All cloning finished.
pause
Best Answer
I think you just need to review the
FOR /?
help a bit. You've mixed a couple different things together which won't work. Thecmd
interpreter is limited compared to other scripting environments you may be familiar with.I think all you're asking is "How do I use
FOR
in a batch file to count from 1 to 5 in increments of 1?" And that's simple enough:The second part of your question is about incrementing another variable within the
FOR
loop. For that, you'll want to lookupSETLOCAL EnableDelayedExpansion
. Here's an example using your provided script.