I want to use %random% %8
to pick a random variable from a number of variables called "song1" through "song118" Here's my current code:
@ECHO off
TITLE Music Randomize
SET count=0
GOTO cont
:func
ECHO %1
SET /A count =1
SET "song%count%=%1"
GOTO :eof
:cont
FOR %%F IN (C:\Users\153651\Desktop\usic\*) DO CALL :func "%%F"
ECHO %song%random% %8 1%
PAUSE
Right now the last ECHO command gives me the output "random18 1"
. How can I get it to output a random one of the variables?
CodePudding user response:
I'm a little confused by what your problem is.
As an example, this works:
set SONG=TaylorSwift
REM Choose a random number between 1 and 118
set /a SONGNUM=%RANDOM% * 118 / 32768 1
REM Here's the random song name:
set MYSONG=%SONG%%SONGNUM%
echo %MYSONG%
REM Prints: TaylorSwift9 (if "9" was the random number)
I think you might have a mismatch in how your %
signs are aligned, but I'm struggling to understand the aims of your code.
CodePudding user response:
There are a few minor errors in your code:
- Arithmetic works only with
set /a
, so you need to pre-calculate the value before using it - you should use
%~1
to remove the quotes - to use a "variable within a variable", you need either delayed expansion or another trick to force a second instance of parsing (I decided not to use delayed expansion, because there might be
!
in your files (thinkP!nk
for example))
The corrected code could look like this:
@ECHO off
setlocal
TITLE Music Randomize
SET count=0
GOTO cont
:func
rem ECHO %1
SET /A count =1
SET "song%count%=%~1"
GOTO :eof
:cont
FOR %%F IN (C:\Users\153651\Desktop\usic\*) DO CALL :func "%%F"
set /a rnd=%random% %8 1
call ECHO %%song%rnd%%%
pause