Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9105349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:05:21+00:00 2026-06-17T02:05:21+00:00

i would appreciate if some windows batch guru may be willing to help me

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:05:22+00:00Added an answer on June 17, 2026 at 2:05 am

    Some comments about your code:

    • FOR command is not executed in paralell, so SLEEP command is not required.

    • Variables in SET /A command does not require to be expanded; the SET command correctly takes its value by itself.

    • It is not a good idea to divide !RANDOM! by the max. limit and get the remainder because the resulting number have not the same distribution of the original !RANDOM!, so you may get multiple repetitions of the same result. The right way to use !RANDOM! is shown below.

    • Array management in Batch is pretty simple, as described in this post.

    This is my version of your code:

    @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   -----------
    REM -- AND CREATE THE INDUCTION ARRAY AT THE SAME TIME --
    ECHO AVAILABLE INDUCTION FILES:
    SET Nb_induction=0
    for /r "%folder%" %%a in (*.*) do (
        SET /a Nb_induction+=1
        SET InductionMP3[!Nb_induction!]=%%a
        ECHO InductionMP3[!Nb_induction!]=%%a
    )
    
    
    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) / 32768 + 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
        )
        REM sleep 1  NOT REQUIRED
    
        REM -----------   ADD RANDOM INDUCTION   -----------
        SET /a "x=(!RANDOM! * Nb_induction) / 32768 + 1"
    
        for %%x in (!x!) do (
            ECHO RANDOM INDUCTION # %%x: !InductionMP3[%%x]!
            ECHO copy /B %OutputMP3% + /B !InductionMP3[%%x]! /B %OutputMP3%
            copy /B %OutputMP3% + /B !InductionMP3[%%x]! /B %OutputMP3%   1> nul
        )
    
        echo %%i
    )
    

    Note that in the last COPY command, the name of the random induction file would be something like this:

        copy /B %OutputMP3% + /B !InductionMP3[!x!]! /B %OutputMP3%   1> nul
    

    However, it is no possible to nest a delayed expansion inside another one, so a FOR command is used to achieve the first expansion and use its replaceable value in the second one. See the previous link for further details.

    Antonio

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would appreciate some help with something I working on and have not done
I'm a newbie in Spring Batch, and I would appreciate some help to resolve
I would appreciate some help with an UPDATE statement. I want to update tblOrderHead
I would appreciate some help on creating the proper SQL to retrieve only one
I have a problem finding references to this subject and would appreciate some help.
I'm pretty new to Python programming and would appreciate some help to a problem
I would really appreciate it if some of you could help optimize my tables,
I'm currently looking into using WIF for an upcoming project and would appreciate some
I would appreciate any help on this issue. Lets say I want to load
I have an interesting problem which I've been looking into and would appreciate some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.