Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

\write18 & echo %ERRORLEVEL% (Windows)

59 views
Skip to first unread message

Will Robertson

unread,
Aug 5, 2008, 12:33:38 AM8/5/08
to
Hello,

I'm trying to execute tasks from with LaTeX and detect if they complete
successfully or not. This works tolerably under *NIX using
echo %? > status.txt
and reading the result with Heiko's catchfile. (After, sigh, writing
more text to status.txt so the write buffer is flushed.)

In MiKTeX 2.7, however, I'm running into troubles trying to do the same
thing. Here's an example:

\documentclass{minimal}
\begin{document}
\makeatletter
\immediate\write18{%
dir /h \string& echo \@percentchar ERRORLEVEL\@percentchar}
\end{document}

When this is executed on the command line:
dir /h & echo %ERRORLEVEL%
it correctly echoes "1". But through the \write18 it incorrectly echoes "0".

Does anyone know why this is? I suppose this is the sort of reason that
pipes are so necessary, but I can't use pipes because only MiKTeX
supports them (as far as I know). Oh, and I can't conditionally use
pipes in MiKTeX in case a Windows user happens to be running TeXLive :)

Many thanks,
Will

Joseph Wright

unread,
Aug 5, 2008, 1:22:43 AM8/5/08
to
On Aug 5, 5:33 am, Will Robertson <wsp...@gmail.com> wrote:
>
> When this is executed on the command line:
>   dir /h & echo %ERRORLEVEL%
> it correctly echoes "1".

On my system (WinXP Pro, SP3) this is not exactly true. The first
attempt always gives errorlevel 0:

C:\Documents and Settings\joseph\Desktop>dir /h & echo %ERRORLEVEL%
Invalid switch - "h".
0

C:\Documents and Settings\joseph\Desktop>dir /h & echo %ERRORLEVEL%
Invalid switch - "h".
1

The same is true for other error-generating commands. I'd go for a
batch file with the same commands in: this does not suffer from the
same problem:

\documentclass{minimal}
\catcode`\%=12\relax
\immediate\write18{del /q temp.bat}
\begin{filecontents*}{temp.bat}
@echo off
dir /h
echo %ERRORLEVEL%
\end{filecontents*}
\catcode`\%=14\relax
\begin{document}
\immediate\write18{temp.bat} \immediate\write18{del temp.bat}
\end{document}

--
Joseph Wright

t.m.tr...@gmail.com

unread,
Aug 5, 2008, 9:22:46 AM8/5/08
to
On Aug 5, 5:33 am, Will Robertson <wsp...@gmail.com> wrote:

Command line interpreter first reads the line to execute, scans for
and expands environmental variables and then executes the expanded
string. On first execution %ERRORLEVEL% gets expanded to 0 (unless
there was an error from the previous command) and next run(s) will
give 1. You can expand ERRORLEVEL at execution time with 'call' (this
should work as expected):

dir /h & call echo ^%ERRORLEVEL^%

In batch files use %% instead of ^% to escape percent character.

Cheers,

Tomek

Will Robertson

unread,
Aug 5, 2008, 9:59:18 AM8/5/08
to
On 2008-08-05 22:52:46 +0930, t.m.tr...@gmail.com said:

> dir /h & call echo ^%ERRORLEVEL^%

Hi Tomek and Joseph,

Thanks for the help! This looks perfect, thanks :) Someone else
recommended using || to conditionally write the status file only in
case of error, which also works well; depends what else you're doing
with the file.

I think I'll be using the solution Tomek posted above. Cheers!

Will

t.m.tr...@gmail.com

unread,
Aug 5, 2008, 11:47:40 AM8/5/08
to
On Aug 5, 2:59 pm, Will Robertson <wsp...@gmail.com> wrote:

I'm glad you like it :-)

However, if you want to execute something if command fails, I would
use || (it's cleaner, IMO).

Cheers,

Tomek

0 new messages