i would appreciate if some windows batch guru may be willing to help me clean my code. Ok the .bat is working, but it's messy implementation, and i'd like to make it simpler and cleaner on 2 things :
1-i've put a "sleep 1 s" to let the "copy" on the line above complete, else it seems the "for ()" is executed multitask-ingly with the rest of the program, is that right ??
2-i'd be willing to use arrays to store filenames instead of re-listing them every time!
[if you wonder : what's the code is doing ? it is concatenating MP3 files : silences.mp3 + inductions.mp3 to create a long track with the inductions with a bit of randomness]
thanks to anyone helping!
@ECHO OFF
CLS
SETLOCAL EnableExtensions EnableDelayedExpansion
REM ----------- SET VALUES -----------
REM RECOMMENDED FORMAT : MP3 CBR 16kHz --> 8kbps silence & 16kbps voice
SET Folder=.\inductions\
SET SilenceMP3=1s-blank.mp3
SET OutputMP3=output.mp3
REM ----------- GET USER INPUTS -----------
SET /p MAX_TIME=Maximum time seconds :
SET /p MIN_TIME=Minimum time seconds :
SET /p LOOP=Nombre de repetitions :
SET /a MAX_TIME-=MIN_TIME
REM ----------- COUNTING NB OF INDUCTION AVAIL -----------
SET Nb_induction=0
for /r "%folder%" %%a in (*.*) do ( SET /a Nb_induction+=1 )
REM ----------- INIT THE OUTPUT WITH 1s SILENCE -----------
copy /Y %SilenceMP3% %OutputMP3% 1> nul
for /L %%i IN (0,1,%LOOP%) do (
REM ----------- GET A RANDOM TIME SILENCE -----------
SET /A REP=!RANDOM! %% !MAX_TIME! + !MIN_TIME!
REM ----------- ADD !REP! seconds of silence -----------
for /L %%j IN (0,1,!REP!) do (
copy /B %OutputMP3% + /B %SilenceMP3% /B %OutputMP3% 1> nul
)
sleep 1
REM ----------- ADD RANDOM INDUCTION -----------
SET /a x=!RANDOM! %% %Nb_induction%
for /r "%folder%" %%a in (*.*) do (
IF !x!==0 ( SET InductionMP3=%%a )
SET /a x-=1
)
copy /B %OutputMP3% + /B !InductionMP3! /B %OutputMP3% 1> nul
echo %%i
)
Antonio (1st thanks), your code outputs:
Maximum time seconds : 20
Minimum time seconds : 10
Nombre de repetitions : 2
AVAILABLE INDUCTION FILES:
InductionMP3[1]=D:\auto-induction\inductions\je_sors_de_mon_corps1.mp3
InductionMP3[2]=D:\auto-induction\inductions\je_sors_de_mon_corps2.mp3
InductionMP3[3]=D:\auto-induction\inductions\je_sors_de_mon_corps3.mp3
RANDOM INDUCTION # 1: D:\auto-induction\inductions\je_sors_de_mon_corps1.mp3
copy /B output.mp3 + /B D:\auto-induction\inductions\je_sors_de_mon_corps1.mp3 /
B output.mp3
0
RANDOM INDUCTION # 1: D:\auto-induction\inductions\je_sors_de_mon_corps1.mp3
copy /B output.mp3 + /B D:\auto-induction\inductions\je_sors_de_mon_corps1.mp3 /
B output.mp3
1
RANDOM INDUCTION # 2: D:\auto-induction\inductions\je_sors_de_mon_corps2.mp3
copy /B output.mp3 + /B D:\auto-induction\inductions\je_sors_de_mon_corps2.mp3 /
B output.mp3
2
cmd issue: (don't understand this mysterious one? so i kept modulo op for now)
SET /a x=(!RANDOM! * Nb_induction) / 32768 + 1
yield error:
/ was unexpected at this time.
i tried () more : SET /a x=((!RANDOM! * Nb_induction) / 32768 ) + 1
no more success!
1- %% modulo operator is the remainder of an integer division: set /A mod=13 %% 5 gives 3; see en.wikipedia.org/wiki/Random_number_generation.
Agree, your solution is the proper math/stat implementation (if win can handle multiply/divide correctly ;-) )
2- I show the !InductionMP3[!x!]! example just for educative purposes, but it can NOT be used (as I said in my answer).
Right, i tried to twist it around !InductionMP3[!x!]!, !InductionMP3[%x%]! etc... still don't get it with the EnableDelayedExpansion stuff %a% %%a !a! etc...
3- I added some ECHO commands in my code that aids in finding problems. Please, copy the program above again, run it and post the output in code format as an edit in your question (NOT in a comment).
Sorry for the messy reply
4- Did you read the post about array management? I think it is not so difficult, but feel free to ask any question about it
I did, arrays are quite ok/easy, it's just the %variable% thing still out of my control...