Search code examples
batch-filecmdwhile-loop

Create a loop on a batch file


I want to create a while loop on a batch file.

The file should run repeatedly the following two lines (one is a fortran code and the other a matlab):

main.out
matlab -batch "path\matlab_code.m" 

Now the matlab code saves a file called boolean.txt that has a value of 1 if the while loop is to be run again (run again the fortran and that matlab code) or zero to stop it. I am unsure where to start.


Solution

  • :label
    main.out
    matlab -batch "path\matlab_code.m"
    set /p boolean=<boolean.txt
    if %boolean%==1 goto label
    

    You can use the set command to assign a value to a variable, in this case boolean. We are using the /p argument to retrieve the value from the user, but by using the < we are retrieving the value from the file boolean.txt.

    At this point, you can verify the value of boolean with an if statement and loop the code by using the goto command, associated with a label at the top of the batch script (I chose label in this case).

    Hope this wraps up your request, as well as helps you understand batch better, however, please be aware of the fact that you could have found all the answers you needed by searching actively on this forum. So typically questions as general as this one get closed pretty often.