What's a correct way for a batch file to run itself with params?












1















I have a batch file. In which, I need to start another copy of itself in a new window with a parameter. I tried the command start "" "%~0" "Param" but it said '"Param"' is not recognized as an internal or external command, operable program or batch file. and didn't start anything. The only way I could get it to work was start %~0 Param , but I figured that would crash if the path had spaces. So What is the correct way to start another instance of the current batch file with parameters?










share|improve this question

























  • Do you mean another additional instance, and the current stay running?

    – LotPings
    Jan 25 at 16:05
















1















I have a batch file. In which, I need to start another copy of itself in a new window with a parameter. I tried the command start "" "%~0" "Param" but it said '"Param"' is not recognized as an internal or external command, operable program or batch file. and didn't start anything. The only way I could get it to work was start %~0 Param , but I figured that would crash if the path had spaces. So What is the correct way to start another instance of the current batch file with parameters?










share|improve this question

























  • Do you mean another additional instance, and the current stay running?

    – LotPings
    Jan 25 at 16:05














1












1








1








I have a batch file. In which, I need to start another copy of itself in a new window with a parameter. I tried the command start "" "%~0" "Param" but it said '"Param"' is not recognized as an internal or external command, operable program or batch file. and didn't start anything. The only way I could get it to work was start %~0 Param , but I figured that would crash if the path had spaces. So What is the correct way to start another instance of the current batch file with parameters?










share|improve this question
















I have a batch file. In which, I need to start another copy of itself in a new window with a parameter. I tried the command start "" "%~0" "Param" but it said '"Param"' is not recognized as an internal or external command, operable program or batch file. and didn't start anything. The only way I could get it to work was start %~0 Param , but I figured that would crash if the path had spaces. So What is the correct way to start another instance of the current batch file with parameters?







windows command-line batch parameters






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 25 at 16:21







Mark Deven

















asked Jan 25 at 14:42









Mark DevenMark Deven

589322




589322













  • Do you mean another additional instance, and the current stay running?

    – LotPings
    Jan 25 at 16:05



















  • Do you mean another additional instance, and the current stay running?

    – LotPings
    Jan 25 at 16:05

















Do you mean another additional instance, and the current stay running?

– LotPings
Jan 25 at 16:05





Do you mean another additional instance, and the current stay running?

– LotPings
Jan 25 at 16:05










3 Answers
3






active

oldest

votes


















2














I would start a second cmd shell, something like:



start "" Cmd.exe %~0 parameters


just to give each iteration of the batch file its own command shell.





Note by OP: I had to do the following:



start cmd.exe /C %~0 parameters





share|improve this answer


























  • Exactly what I was looking for, don’t know why I didn’t think of that. Thank you!!

    – Mark Deven
    Jan 25 at 16:11











  • Actually, this isn't working for me. I had to do start cmd.exe /C %~0 parameters

    – Mark Deven
    Feb 15 at 19:13





















1














In order to call another batch file from a batch file, use call "name of script.bat" or `start "name of script.bat"



Although you can do it without, unexpected results will happen, given that it will continuously call itself.



so technically, you can just write



%0 MyParam





share|improve this answer































    1














    To avoid an infinite loop, check if args are present:



    :: Q:Test2019125SU_1298393.cmd
    @Echo off
    If "%~1" neq "" goto :HasArgs
    Echo restart with parms
    "%~0" "parms"

    :HasArgs
    Echo %0 started with %*
    Pause
    Exit /B


    Sample run:



    > SU_1298393.cmd
    restart with parms
    "SU_1298393.cmd" started with "parms"
    Press any key to continue . . .

    > SU_1298393.cmd foo bar
    SU_1298393.cmd started with foo bar
    Press any key to continue . . .





    share|improve this answer
























    • It’s only in a specific case but thank you!

      – Mark Deven
      Jan 25 at 16:11











    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1398393%2fwhats-a-correct-way-for-a-batch-file-to-run-itself-with-params%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    I would start a second cmd shell, something like:



    start "" Cmd.exe %~0 parameters


    just to give each iteration of the batch file its own command shell.





    Note by OP: I had to do the following:



    start cmd.exe /C %~0 parameters





    share|improve this answer


























    • Exactly what I was looking for, don’t know why I didn’t think of that. Thank you!!

      – Mark Deven
      Jan 25 at 16:11











    • Actually, this isn't working for me. I had to do start cmd.exe /C %~0 parameters

      – Mark Deven
      Feb 15 at 19:13


















    2














    I would start a second cmd shell, something like:



    start "" Cmd.exe %~0 parameters


    just to give each iteration of the batch file its own command shell.





    Note by OP: I had to do the following:



    start cmd.exe /C %~0 parameters





    share|improve this answer


























    • Exactly what I was looking for, don’t know why I didn’t think of that. Thank you!!

      – Mark Deven
      Jan 25 at 16:11











    • Actually, this isn't working for me. I had to do start cmd.exe /C %~0 parameters

      – Mark Deven
      Feb 15 at 19:13
















    2












    2








    2







    I would start a second cmd shell, something like:



    start "" Cmd.exe %~0 parameters


    just to give each iteration of the batch file its own command shell.





    Note by OP: I had to do the following:



    start cmd.exe /C %~0 parameters





    share|improve this answer















    I would start a second cmd shell, something like:



    start "" Cmd.exe %~0 parameters


    just to give each iteration of the batch file its own command shell.





    Note by OP: I had to do the following:



    start cmd.exe /C %~0 parameters






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 15 at 20:15









    Run5k

    11.4k73253




    11.4k73253










    answered Jan 25 at 15:38









    LarrycLarryc

    67139




    67139













    • Exactly what I was looking for, don’t know why I didn’t think of that. Thank you!!

      – Mark Deven
      Jan 25 at 16:11











    • Actually, this isn't working for me. I had to do start cmd.exe /C %~0 parameters

      – Mark Deven
      Feb 15 at 19:13





















    • Exactly what I was looking for, don’t know why I didn’t think of that. Thank you!!

      – Mark Deven
      Jan 25 at 16:11











    • Actually, this isn't working for me. I had to do start cmd.exe /C %~0 parameters

      – Mark Deven
      Feb 15 at 19:13



















    Exactly what I was looking for, don’t know why I didn’t think of that. Thank you!!

    – Mark Deven
    Jan 25 at 16:11





    Exactly what I was looking for, don’t know why I didn’t think of that. Thank you!!

    – Mark Deven
    Jan 25 at 16:11













    Actually, this isn't working for me. I had to do start cmd.exe /C %~0 parameters

    – Mark Deven
    Feb 15 at 19:13







    Actually, this isn't working for me. I had to do start cmd.exe /C %~0 parameters

    – Mark Deven
    Feb 15 at 19:13















    1














    In order to call another batch file from a batch file, use call "name of script.bat" or `start "name of script.bat"



    Although you can do it without, unexpected results will happen, given that it will continuously call itself.



    so technically, you can just write



    %0 MyParam





    share|improve this answer




























      1














      In order to call another batch file from a batch file, use call "name of script.bat" or `start "name of script.bat"



      Although you can do it without, unexpected results will happen, given that it will continuously call itself.



      so technically, you can just write



      %0 MyParam





      share|improve this answer


























        1












        1








        1







        In order to call another batch file from a batch file, use call "name of script.bat" or `start "name of script.bat"



        Although you can do it without, unexpected results will happen, given that it will continuously call itself.



        so technically, you can just write



        %0 MyParam





        share|improve this answer













        In order to call another batch file from a batch file, use call "name of script.bat" or `start "name of script.bat"



        Although you can do it without, unexpected results will happen, given that it will continuously call itself.



        so technically, you can just write



        %0 MyParam






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 25 at 15:32









        LPChipLPChip

        36.3k55487




        36.3k55487























            1














            To avoid an infinite loop, check if args are present:



            :: Q:Test2019125SU_1298393.cmd
            @Echo off
            If "%~1" neq "" goto :HasArgs
            Echo restart with parms
            "%~0" "parms"

            :HasArgs
            Echo %0 started with %*
            Pause
            Exit /B


            Sample run:



            > SU_1298393.cmd
            restart with parms
            "SU_1298393.cmd" started with "parms"
            Press any key to continue . . .

            > SU_1298393.cmd foo bar
            SU_1298393.cmd started with foo bar
            Press any key to continue . . .





            share|improve this answer
























            • It’s only in a specific case but thank you!

              – Mark Deven
              Jan 25 at 16:11
















            1














            To avoid an infinite loop, check if args are present:



            :: Q:Test2019125SU_1298393.cmd
            @Echo off
            If "%~1" neq "" goto :HasArgs
            Echo restart with parms
            "%~0" "parms"

            :HasArgs
            Echo %0 started with %*
            Pause
            Exit /B


            Sample run:



            > SU_1298393.cmd
            restart with parms
            "SU_1298393.cmd" started with "parms"
            Press any key to continue . . .

            > SU_1298393.cmd foo bar
            SU_1298393.cmd started with foo bar
            Press any key to continue . . .





            share|improve this answer
























            • It’s only in a specific case but thank you!

              – Mark Deven
              Jan 25 at 16:11














            1












            1








            1







            To avoid an infinite loop, check if args are present:



            :: Q:Test2019125SU_1298393.cmd
            @Echo off
            If "%~1" neq "" goto :HasArgs
            Echo restart with parms
            "%~0" "parms"

            :HasArgs
            Echo %0 started with %*
            Pause
            Exit /B


            Sample run:



            > SU_1298393.cmd
            restart with parms
            "SU_1298393.cmd" started with "parms"
            Press any key to continue . . .

            > SU_1298393.cmd foo bar
            SU_1298393.cmd started with foo bar
            Press any key to continue . . .





            share|improve this answer













            To avoid an infinite loop, check if args are present:



            :: Q:Test2019125SU_1298393.cmd
            @Echo off
            If "%~1" neq "" goto :HasArgs
            Echo restart with parms
            "%~0" "parms"

            :HasArgs
            Echo %0 started with %*
            Pause
            Exit /B


            Sample run:



            > SU_1298393.cmd
            restart with parms
            "SU_1298393.cmd" started with "parms"
            Press any key to continue . . .

            > SU_1298393.cmd foo bar
            SU_1298393.cmd started with foo bar
            Press any key to continue . . .






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 25 at 16:02









            LotPingsLotPings

            5,1151823




            5,1151823













            • It’s only in a specific case but thank you!

              – Mark Deven
              Jan 25 at 16:11



















            • It’s only in a specific case but thank you!

              – Mark Deven
              Jan 25 at 16:11

















            It’s only in a specific case but thank you!

            – Mark Deven
            Jan 25 at 16:11





            It’s only in a specific case but thank you!

            – Mark Deven
            Jan 25 at 16:11


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1398393%2fwhats-a-correct-way-for-a-batch-file-to-run-itself-with-params%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Список кардиналов, возведённых папой римским Каликстом III

            Deduzione

            Mysql.sock missing - “Can't connect to local MySQL server through socket”