How to restart batch file itself?












2














How to restart batch itself automatically?



If the filename of batch changed,



start <fixedbatchfilename>


must fail because though the filename changes, the command doesn't.



Is there a feasible way, like



start <autogetbatchfilenameitself> ?









share|improve this question






















  • why would the file name change?
    – Keltari
    Nov 23 '13 at 3:12










  • @Keltari Maybe end-user will change the filename, so the batch file will become invalid
    – Kevin Dong
    Nov 23 '13 at 3:14








  • 1




    "you cant fix stupid" - Ron White
    – Keltari
    Nov 23 '13 at 3:16






  • 1




    @Keltari I just want my batch file be valid even if my end-user change the batchfile name
    – Kevin Dong
    Nov 23 '13 at 3:19






  • 1




    Also on the subject of restarting a batch file generally, you can put a label at the top like :begin then a line anywhere that says goto begin
    – barlop
    Nov 23 '13 at 16:55
















2














How to restart batch itself automatically?



If the filename of batch changed,



start <fixedbatchfilename>


must fail because though the filename changes, the command doesn't.



Is there a feasible way, like



start <autogetbatchfilenameitself> ?









share|improve this question






















  • why would the file name change?
    – Keltari
    Nov 23 '13 at 3:12










  • @Keltari Maybe end-user will change the filename, so the batch file will become invalid
    – Kevin Dong
    Nov 23 '13 at 3:14








  • 1




    "you cant fix stupid" - Ron White
    – Keltari
    Nov 23 '13 at 3:16






  • 1




    @Keltari I just want my batch file be valid even if my end-user change the batchfile name
    – Kevin Dong
    Nov 23 '13 at 3:19






  • 1




    Also on the subject of restarting a batch file generally, you can put a label at the top like :begin then a line anywhere that says goto begin
    – barlop
    Nov 23 '13 at 16:55














2












2








2


0





How to restart batch itself automatically?



If the filename of batch changed,



start <fixedbatchfilename>


must fail because though the filename changes, the command doesn't.



Is there a feasible way, like



start <autogetbatchfilenameitself> ?









share|improve this question













How to restart batch itself automatically?



If the filename of batch changed,



start <fixedbatchfilename>


must fail because though the filename changes, the command doesn't.



Is there a feasible way, like



start <autogetbatchfilenameitself> ?






command-line batch






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '13 at 3:10









Kevin DongKevin Dong

340520




340520












  • why would the file name change?
    – Keltari
    Nov 23 '13 at 3:12










  • @Keltari Maybe end-user will change the filename, so the batch file will become invalid
    – Kevin Dong
    Nov 23 '13 at 3:14








  • 1




    "you cant fix stupid" - Ron White
    – Keltari
    Nov 23 '13 at 3:16






  • 1




    @Keltari I just want my batch file be valid even if my end-user change the batchfile name
    – Kevin Dong
    Nov 23 '13 at 3:19






  • 1




    Also on the subject of restarting a batch file generally, you can put a label at the top like :begin then a line anywhere that says goto begin
    – barlop
    Nov 23 '13 at 16:55


















  • why would the file name change?
    – Keltari
    Nov 23 '13 at 3:12










  • @Keltari Maybe end-user will change the filename, so the batch file will become invalid
    – Kevin Dong
    Nov 23 '13 at 3:14








  • 1




    "you cant fix stupid" - Ron White
    – Keltari
    Nov 23 '13 at 3:16






  • 1




    @Keltari I just want my batch file be valid even if my end-user change the batchfile name
    – Kevin Dong
    Nov 23 '13 at 3:19






  • 1




    Also on the subject of restarting a batch file generally, you can put a label at the top like :begin then a line anywhere that says goto begin
    – barlop
    Nov 23 '13 at 16:55
















why would the file name change?
– Keltari
Nov 23 '13 at 3:12




why would the file name change?
– Keltari
Nov 23 '13 at 3:12












@Keltari Maybe end-user will change the filename, so the batch file will become invalid
– Kevin Dong
Nov 23 '13 at 3:14






@Keltari Maybe end-user will change the filename, so the batch file will become invalid
– Kevin Dong
Nov 23 '13 at 3:14






1




1




"you cant fix stupid" - Ron White
– Keltari
Nov 23 '13 at 3:16




"you cant fix stupid" - Ron White
– Keltari
Nov 23 '13 at 3:16




1




1




@Keltari I just want my batch file be valid even if my end-user change the batchfile name
– Kevin Dong
Nov 23 '13 at 3:19




@Keltari I just want my batch file be valid even if my end-user change the batchfile name
– Kevin Dong
Nov 23 '13 at 3:19




1




1




Also on the subject of restarting a batch file generally, you can put a label at the top like :begin then a line anywhere that says goto begin
– barlop
Nov 23 '13 at 16:55




Also on the subject of restarting a batch file generally, you can put a label at the top like :begin then a line anywhere that says goto begin
– barlop
Nov 23 '13 at 16:55










2 Answers
2






active

oldest

votes


















2














You can call %0 in the same directory to call the batch itself again.



Here is a little batch-file to see the variables for yourself:



@echo off
echo Filename exactly as called: %0
echo Driveletter: %~d0
echo Path: %~p0
echo Filename: %~n0
echo Extension: %~x0
echo Complete: %~d0%~p0%~n0%~x0


Example:



echo The following will cause this to loop until Ctrl+C is pressed:
%0


Or:



echo The following will cause this to loop until Ctrl+C is pressed:
%~d0%~p0%~n0%~x0





share|improve this answer























  • wow, powerful internal command, THX!
    – Kevin Dong
    Nov 23 '13 at 17:40






  • 1




    Edited it a bit. The %0 always works great but the %CD% i suggested was for the current directory, not the directory of the batch itself. %d0 is driveletter and %p0 is the path to the batch. But you get the idea. This is all doable in the batch itself. You can find lots more of these goodies here.
    – Rik
    Nov 23 '13 at 17:48










  • (1) %~f0 seems to be equivalent to %~d0%~p0%~n0%~x0 and, obviously, is much shorter. (2) The %0 solution may fail if the batch file does a cd.  Your second solution (or %~f0) is better in that case.
    – Scott
    Dec 19 '18 at 6:05



















0














start "" "%~f0" OR start "title of batch window" "%~f0"



To restart a batch file use:



start "" "%~f0"
exit



This will start your batch file from within your batch file, regardless of file name or location, so long as the name or location has not changed during batch operation, IE after batch has started and before code: (start "" "%~f0") has had a chance to execute.




Always include a TITLE this can be a simple string like "My Script" or just a pair of empty quotes ""

According to the Microsoft documentation, the title is optional, but depending on the other options chosen you can have problems if it is omitted.




source: https://ss64.com/nt/start.html






share|improve this answer



















  • 1




    What advantage does this have over the five-year-old answer?
    – Scott
    Dec 17 '18 at 19:33










  • the other answer gave users the knowledge to build a working answer for themselves, not the answer itself. As a relatively new user to writing cmd batch files I struggled to wright code that would work as intended as I lacked simple knowledge like using quotation marks around fully qualified path names.
    – The Wizerd
    Dec 17 '18 at 19:50












  • What are you talking about?  %0 and %~d0%~p0%~n0%~x0 are complete, ready-to-use solutions.  OK, you made the trivial improvement of replacing %~d0%~p0%~n0%~x0 with %~f0, which seems to be equivalent.  Trivial abbreviations do not qualify as new answers.  The big change you introduced was the start command, but why?  You can run a batch file just by saying its name; you don't need to use start.
    – Scott
    Dec 17 '18 at 20:10










  • I didn't know you could run a batch file just by saying its name, I now understand the first answer, and see that %0 is the best solution.
    – The Wizerd
    Dec 17 '18 at 20:53











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%2f679607%2fhow-to-restart-batch-file-itself%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can call %0 in the same directory to call the batch itself again.



Here is a little batch-file to see the variables for yourself:



@echo off
echo Filename exactly as called: %0
echo Driveletter: %~d0
echo Path: %~p0
echo Filename: %~n0
echo Extension: %~x0
echo Complete: %~d0%~p0%~n0%~x0


Example:



echo The following will cause this to loop until Ctrl+C is pressed:
%0


Or:



echo The following will cause this to loop until Ctrl+C is pressed:
%~d0%~p0%~n0%~x0





share|improve this answer























  • wow, powerful internal command, THX!
    – Kevin Dong
    Nov 23 '13 at 17:40






  • 1




    Edited it a bit. The %0 always works great but the %CD% i suggested was for the current directory, not the directory of the batch itself. %d0 is driveletter and %p0 is the path to the batch. But you get the idea. This is all doable in the batch itself. You can find lots more of these goodies here.
    – Rik
    Nov 23 '13 at 17:48










  • (1) %~f0 seems to be equivalent to %~d0%~p0%~n0%~x0 and, obviously, is much shorter. (2) The %0 solution may fail if the batch file does a cd.  Your second solution (or %~f0) is better in that case.
    – Scott
    Dec 19 '18 at 6:05
















2














You can call %0 in the same directory to call the batch itself again.



Here is a little batch-file to see the variables for yourself:



@echo off
echo Filename exactly as called: %0
echo Driveletter: %~d0
echo Path: %~p0
echo Filename: %~n0
echo Extension: %~x0
echo Complete: %~d0%~p0%~n0%~x0


Example:



echo The following will cause this to loop until Ctrl+C is pressed:
%0


Or:



echo The following will cause this to loop until Ctrl+C is pressed:
%~d0%~p0%~n0%~x0





share|improve this answer























  • wow, powerful internal command, THX!
    – Kevin Dong
    Nov 23 '13 at 17:40






  • 1




    Edited it a bit. The %0 always works great but the %CD% i suggested was for the current directory, not the directory of the batch itself. %d0 is driveletter and %p0 is the path to the batch. But you get the idea. This is all doable in the batch itself. You can find lots more of these goodies here.
    – Rik
    Nov 23 '13 at 17:48










  • (1) %~f0 seems to be equivalent to %~d0%~p0%~n0%~x0 and, obviously, is much shorter. (2) The %0 solution may fail if the batch file does a cd.  Your second solution (or %~f0) is better in that case.
    – Scott
    Dec 19 '18 at 6:05














2












2








2






You can call %0 in the same directory to call the batch itself again.



Here is a little batch-file to see the variables for yourself:



@echo off
echo Filename exactly as called: %0
echo Driveletter: %~d0
echo Path: %~p0
echo Filename: %~n0
echo Extension: %~x0
echo Complete: %~d0%~p0%~n0%~x0


Example:



echo The following will cause this to loop until Ctrl+C is pressed:
%0


Or:



echo The following will cause this to loop until Ctrl+C is pressed:
%~d0%~p0%~n0%~x0





share|improve this answer














You can call %0 in the same directory to call the batch itself again.



Here is a little batch-file to see the variables for yourself:



@echo off
echo Filename exactly as called: %0
echo Driveletter: %~d0
echo Path: %~p0
echo Filename: %~n0
echo Extension: %~x0
echo Complete: %~d0%~p0%~n0%~x0


Example:



echo The following will cause this to loop until Ctrl+C is pressed:
%0


Or:



echo The following will cause this to loop until Ctrl+C is pressed:
%~d0%~p0%~n0%~x0






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '13 at 17:43

























answered Nov 23 '13 at 17:32









RikRik

10.9k12133




10.9k12133












  • wow, powerful internal command, THX!
    – Kevin Dong
    Nov 23 '13 at 17:40






  • 1




    Edited it a bit. The %0 always works great but the %CD% i suggested was for the current directory, not the directory of the batch itself. %d0 is driveletter and %p0 is the path to the batch. But you get the idea. This is all doable in the batch itself. You can find lots more of these goodies here.
    – Rik
    Nov 23 '13 at 17:48










  • (1) %~f0 seems to be equivalent to %~d0%~p0%~n0%~x0 and, obviously, is much shorter. (2) The %0 solution may fail if the batch file does a cd.  Your second solution (or %~f0) is better in that case.
    – Scott
    Dec 19 '18 at 6:05


















  • wow, powerful internal command, THX!
    – Kevin Dong
    Nov 23 '13 at 17:40






  • 1




    Edited it a bit. The %0 always works great but the %CD% i suggested was for the current directory, not the directory of the batch itself. %d0 is driveletter and %p0 is the path to the batch. But you get the idea. This is all doable in the batch itself. You can find lots more of these goodies here.
    – Rik
    Nov 23 '13 at 17:48










  • (1) %~f0 seems to be equivalent to %~d0%~p0%~n0%~x0 and, obviously, is much shorter. (2) The %0 solution may fail if the batch file does a cd.  Your second solution (or %~f0) is better in that case.
    – Scott
    Dec 19 '18 at 6:05
















wow, powerful internal command, THX!
– Kevin Dong
Nov 23 '13 at 17:40




wow, powerful internal command, THX!
– Kevin Dong
Nov 23 '13 at 17:40




1




1




Edited it a bit. The %0 always works great but the %CD% i suggested was for the current directory, not the directory of the batch itself. %d0 is driveletter and %p0 is the path to the batch. But you get the idea. This is all doable in the batch itself. You can find lots more of these goodies here.
– Rik
Nov 23 '13 at 17:48




Edited it a bit. The %0 always works great but the %CD% i suggested was for the current directory, not the directory of the batch itself. %d0 is driveletter and %p0 is the path to the batch. But you get the idea. This is all doable in the batch itself. You can find lots more of these goodies here.
– Rik
Nov 23 '13 at 17:48












(1) %~f0 seems to be equivalent to %~d0%~p0%~n0%~x0 and, obviously, is much shorter. (2) The %0 solution may fail if the batch file does a cd.  Your second solution (or %~f0) is better in that case.
– Scott
Dec 19 '18 at 6:05




(1) %~f0 seems to be equivalent to %~d0%~p0%~n0%~x0 and, obviously, is much shorter. (2) The %0 solution may fail if the batch file does a cd.  Your second solution (or %~f0) is better in that case.
– Scott
Dec 19 '18 at 6:05













0














start "" "%~f0" OR start "title of batch window" "%~f0"



To restart a batch file use:



start "" "%~f0"
exit



This will start your batch file from within your batch file, regardless of file name or location, so long as the name or location has not changed during batch operation, IE after batch has started and before code: (start "" "%~f0") has had a chance to execute.




Always include a TITLE this can be a simple string like "My Script" or just a pair of empty quotes ""

According to the Microsoft documentation, the title is optional, but depending on the other options chosen you can have problems if it is omitted.




source: https://ss64.com/nt/start.html






share|improve this answer



















  • 1




    What advantage does this have over the five-year-old answer?
    – Scott
    Dec 17 '18 at 19:33










  • the other answer gave users the knowledge to build a working answer for themselves, not the answer itself. As a relatively new user to writing cmd batch files I struggled to wright code that would work as intended as I lacked simple knowledge like using quotation marks around fully qualified path names.
    – The Wizerd
    Dec 17 '18 at 19:50












  • What are you talking about?  %0 and %~d0%~p0%~n0%~x0 are complete, ready-to-use solutions.  OK, you made the trivial improvement of replacing %~d0%~p0%~n0%~x0 with %~f0, which seems to be equivalent.  Trivial abbreviations do not qualify as new answers.  The big change you introduced was the start command, but why?  You can run a batch file just by saying its name; you don't need to use start.
    – Scott
    Dec 17 '18 at 20:10










  • I didn't know you could run a batch file just by saying its name, I now understand the first answer, and see that %0 is the best solution.
    – The Wizerd
    Dec 17 '18 at 20:53
















0














start "" "%~f0" OR start "title of batch window" "%~f0"



To restart a batch file use:



start "" "%~f0"
exit



This will start your batch file from within your batch file, regardless of file name or location, so long as the name or location has not changed during batch operation, IE after batch has started and before code: (start "" "%~f0") has had a chance to execute.




Always include a TITLE this can be a simple string like "My Script" or just a pair of empty quotes ""

According to the Microsoft documentation, the title is optional, but depending on the other options chosen you can have problems if it is omitted.




source: https://ss64.com/nt/start.html






share|improve this answer



















  • 1




    What advantage does this have over the five-year-old answer?
    – Scott
    Dec 17 '18 at 19:33










  • the other answer gave users the knowledge to build a working answer for themselves, not the answer itself. As a relatively new user to writing cmd batch files I struggled to wright code that would work as intended as I lacked simple knowledge like using quotation marks around fully qualified path names.
    – The Wizerd
    Dec 17 '18 at 19:50












  • What are you talking about?  %0 and %~d0%~p0%~n0%~x0 are complete, ready-to-use solutions.  OK, you made the trivial improvement of replacing %~d0%~p0%~n0%~x0 with %~f0, which seems to be equivalent.  Trivial abbreviations do not qualify as new answers.  The big change you introduced was the start command, but why?  You can run a batch file just by saying its name; you don't need to use start.
    – Scott
    Dec 17 '18 at 20:10










  • I didn't know you could run a batch file just by saying its name, I now understand the first answer, and see that %0 is the best solution.
    – The Wizerd
    Dec 17 '18 at 20:53














0












0








0






start "" "%~f0" OR start "title of batch window" "%~f0"



To restart a batch file use:



start "" "%~f0"
exit



This will start your batch file from within your batch file, regardless of file name or location, so long as the name or location has not changed during batch operation, IE after batch has started and before code: (start "" "%~f0") has had a chance to execute.




Always include a TITLE this can be a simple string like "My Script" or just a pair of empty quotes ""

According to the Microsoft documentation, the title is optional, but depending on the other options chosen you can have problems if it is omitted.




source: https://ss64.com/nt/start.html






share|improve this answer














start "" "%~f0" OR start "title of batch window" "%~f0"



To restart a batch file use:



start "" "%~f0"
exit



This will start your batch file from within your batch file, regardless of file name or location, so long as the name or location has not changed during batch operation, IE after batch has started and before code: (start "" "%~f0") has had a chance to execute.




Always include a TITLE this can be a simple string like "My Script" or just a pair of empty quotes ""

According to the Microsoft documentation, the title is optional, but depending on the other options chosen you can have problems if it is omitted.




source: https://ss64.com/nt/start.html







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 17 '18 at 19:35

























answered Dec 17 '18 at 19:24









The WizerdThe Wizerd

12




12








  • 1




    What advantage does this have over the five-year-old answer?
    – Scott
    Dec 17 '18 at 19:33










  • the other answer gave users the knowledge to build a working answer for themselves, not the answer itself. As a relatively new user to writing cmd batch files I struggled to wright code that would work as intended as I lacked simple knowledge like using quotation marks around fully qualified path names.
    – The Wizerd
    Dec 17 '18 at 19:50












  • What are you talking about?  %0 and %~d0%~p0%~n0%~x0 are complete, ready-to-use solutions.  OK, you made the trivial improvement of replacing %~d0%~p0%~n0%~x0 with %~f0, which seems to be equivalent.  Trivial abbreviations do not qualify as new answers.  The big change you introduced was the start command, but why?  You can run a batch file just by saying its name; you don't need to use start.
    – Scott
    Dec 17 '18 at 20:10










  • I didn't know you could run a batch file just by saying its name, I now understand the first answer, and see that %0 is the best solution.
    – The Wizerd
    Dec 17 '18 at 20:53














  • 1




    What advantage does this have over the five-year-old answer?
    – Scott
    Dec 17 '18 at 19:33










  • the other answer gave users the knowledge to build a working answer for themselves, not the answer itself. As a relatively new user to writing cmd batch files I struggled to wright code that would work as intended as I lacked simple knowledge like using quotation marks around fully qualified path names.
    – The Wizerd
    Dec 17 '18 at 19:50












  • What are you talking about?  %0 and %~d0%~p0%~n0%~x0 are complete, ready-to-use solutions.  OK, you made the trivial improvement of replacing %~d0%~p0%~n0%~x0 with %~f0, which seems to be equivalent.  Trivial abbreviations do not qualify as new answers.  The big change you introduced was the start command, but why?  You can run a batch file just by saying its name; you don't need to use start.
    – Scott
    Dec 17 '18 at 20:10










  • I didn't know you could run a batch file just by saying its name, I now understand the first answer, and see that %0 is the best solution.
    – The Wizerd
    Dec 17 '18 at 20:53








1




1




What advantage does this have over the five-year-old answer?
– Scott
Dec 17 '18 at 19:33




What advantage does this have over the five-year-old answer?
– Scott
Dec 17 '18 at 19:33












the other answer gave users the knowledge to build a working answer for themselves, not the answer itself. As a relatively new user to writing cmd batch files I struggled to wright code that would work as intended as I lacked simple knowledge like using quotation marks around fully qualified path names.
– The Wizerd
Dec 17 '18 at 19:50






the other answer gave users the knowledge to build a working answer for themselves, not the answer itself. As a relatively new user to writing cmd batch files I struggled to wright code that would work as intended as I lacked simple knowledge like using quotation marks around fully qualified path names.
– The Wizerd
Dec 17 '18 at 19:50














What are you talking about?  %0 and %~d0%~p0%~n0%~x0 are complete, ready-to-use solutions.  OK, you made the trivial improvement of replacing %~d0%~p0%~n0%~x0 with %~f0, which seems to be equivalent.  Trivial abbreviations do not qualify as new answers.  The big change you introduced was the start command, but why?  You can run a batch file just by saying its name; you don't need to use start.
– Scott
Dec 17 '18 at 20:10




What are you talking about?  %0 and %~d0%~p0%~n0%~x0 are complete, ready-to-use solutions.  OK, you made the trivial improvement of replacing %~d0%~p0%~n0%~x0 with %~f0, which seems to be equivalent.  Trivial abbreviations do not qualify as new answers.  The big change you introduced was the start command, but why?  You can run a batch file just by saying its name; you don't need to use start.
– Scott
Dec 17 '18 at 20:10












I didn't know you could run a batch file just by saying its name, I now understand the first answer, and see that %0 is the best solution.
– The Wizerd
Dec 17 '18 at 20:53




I didn't know you could run a batch file just by saying its name, I now understand the first answer, and see that %0 is the best solution.
– The Wizerd
Dec 17 '18 at 20:53


















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%2f679607%2fhow-to-restart-batch-file-itself%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

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]