|Unfortunately, the other references to var%a% also need to include the delayed expanded variables, including those in the IF statements. I say "unfortunately" because to construct the variable, you do need a mix of both non-expanded and expanded variables to do it. In other words, the easy way of doing it would be to use !var!a!!, which absolutely does not work. I get around the problem by using CALL:|
for /f "tokens=*" %%1 in ('dir /b /s') do (
CALL :Var_Test !a!
if [!var%var_num%:~-4!]==[.mp3] (echo !var%var_num%:~%length%! & set /a a+=1)
if [!var%var_num%:~-4!]==[.wav] (echo !var%var_num%:~%length%! & set /a a+=1)
In the :Var_Test subroutine, the delayed expanded value of !a! is passed as the variable %1. The inclusion of "SET var_num=%1" is probably not needed, but I tend to use descriptive variable names when I can to help describe the intended logic I'm trying to use. I also avoid using numbers as (user) variables whenever possible. I didn't notice that the FOR loop was using the number 1 as it's variable name until pasting the above snippet, initially thinking it was lowercase L. From FOR's help:
Runs a specified command for each file in a set of files.
FOR %variable IN (set) DO command [command-parameters]
%variable Specifies a single letter replaceable parameter.
When your only tool is a hammer, every problem looks like a nail.