Windows batch file, wait for command to finish?

76,595

Solution 1

I would use /B to stay in the same process and /wait tot wait until the first command is finished. You don't need the /wait in the second line unless there are more commands to follow. If this doens't work, experiment with leaving the cmd /K away. Since runas is an executable and the batch waits until it is finished it's possible you can let away the start command all together.

If all this doesn't work insert 5 ping commands, that is the classic way to wait for ± one second.

START /B /wait "runas /user:administrator & cd C:\users\MyName\dropbox\!!GIT_HUB_REPOS_ALL\tangycode.github.io & hexo generate & hexo serve --draft"


START /B "" http://localhost:4000/

Solution 2

I'll take a stab at this, (completely untested).

@Echo Off

(Set SrcDir=%UserProfile%\dropbox\!!GIT_HUB_REPOS_ALL\tangycode.github.io)

If Not Exist "%SrcDir%\" Exit/B
Start "" /D"%SrcDir%" /Wait /B RunAs /User:administrator^
 "Cmd /C Start /Wait hexo generate & Start hexo serve --draft"

Start http://localhost:4000/
Share:
76,595
Vincent Tang
Author by

Vincent Tang

I mostly develop with NodeJS / Javascript for full-stack applications, but I dabble in Python . I am a confident developer willing to learn new things as they come. Please check out my GitHub or go to my blog to learn more about projects I have done. Below are a few of my best answers and questions, not based on votes, but rather brevity and helpfulness. As well as projects I've made that solve real-world applications. I contribute to a number of communities besides StackOverflow, listed below QUESTIONS Javascript: Split a string into array matching parameters Javascript access json property javascript how to get json data with api call to redirected url ANSWERS Excel - Seperating / Showing Deleted Duplicated Data Excel VBA Grab Ancestor value from Relational Data Creating URL Paginator with Excel VBA PROJECTS ExcelVBA - Bulk Downloader & Renamer Script used by 300+ database-developers for ETL and local backup COMMUNITIES Alternativeto.net and Software Recommendations Freecodecamp forums solutions SuperUser Forums CONTACT I live on US East Coast. You can reach me here :::::: vincentntang (at) gmail If you email me please write bananas in the subject line.

Updated on October 25, 2020

Comments

  • Vincent Tang
    Vincent Tang over 3 years

    I can't seem to figure this out.

    I am creating a live site and deploying it online

    batch file:

    START cmd /K "runas /user:administrator & cd C:\users\MyName\dropbox\!!GIT_HUB_REPOS_ALL\tangycode.github.io & hexo generate & hexo serve --draft"
    
    
    START /wait "" http://localhost:4000/
    

    The thing is running the command hexo generate & hexo serve --draft takes about 5-10 seconds, time varies. Ideally I want to wait for this to occur before going to live site at http://localhost:4000

    Some reason this windows batch command just automatically opens up localhost:4000 right away though