[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: True batch mode

From   "Russell V. Lenth" <>
Subject   Re: st: True batch mode
Date   Wed, 09 Apr 2008 22:45:08 -0500

OK; following Sergiy's 1st suggestion, here's a Windows .BAT script that works. It is called with one argument - the .do file to run (if no
extension, defaults to .do). It requires the -log- command shown below as the last line of the source file, if you need an exit code.

@echo off

:: Batch file for Windows Stata that waits until the job is done
:: If there is a Stata error, an exit code of 1 is returned.
:: Correct exit-code behavior requires that the last statement
:: in the input .do file be:
:: log using c:\temp\stata_check.smcl
:: The drive, path, executable name may need to be changed in the
:: line below, depending on your installation.

set STATA="c:\Program Files\Stata10\wstata.exe"

if exist c:\temp\stata_check.smcl del c:\temp\stata_check.smcl

start "dummy title" /w %STATA% /e do %1

if not exist c:\temp\stata_check.smcl (
echo There was an error - see %~dpn1.log for details
exit /b 1

del c:\temp\stata_check.smcl

exit /b 0

Russell V. Lenth, Professor
Department of Statistics
& Actuarial Science (319)335-0814 FAX (319)335-3017
The University of Iowa
Iowa City, IA 52242 USA

Sergiy Radyakin wrote:

On 4/9/08, Russell V. Lenth <> wrote:
When I try to run Stata in batch mode on Windows...

       DOS> wstata /e do

... it runs in the background, returning a new DOS prompt before the job is
done.  That's fine if I'm doing it manually, but I want to run Stata as part
of a process, and the next step needs Stata to be finished creating its
output before it starts.  I suppose I could have Stata write some new file
at the very end, but then if there's an error, that file will never get
written.  Ideas?

Depends on the skill level:

1. start /w ........
    /w = /wait


DOS>delete file_marker
DOS> wstata /e do

// --- ---
 capture do  // capture will ensure we return back to write
the marker
  write file_marker
// --- end of ---

put your program into

3. CreateProcessEx(lcExe,lcCommandLine,lcStartDirectory,;

4. ShellExecuteEx(LPSHELLEXECUTEINFO lpExecInfo);

Both 3 and 4 are very similar and converge at one point to the same
thing. Which one to use is a matter of convenience.
5. if none of the above is ok, I can suggest other variants

Best regards, Sergiy Radyakin

[BTW, the context here is Windows, version Stata/IC, but I need to extend to
other platforms.  e.g., looks like maybe this isn't a problem on Linux --
just don't put an ampersand?]

Russell V. Lenth, Professor
Department of Statistics
 & Actuarial Science            (319)335-0814    FAX (319)335-3017
The University of Iowa 
Iowa City, IA 52242  USA

*   For searches and help try:

© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index