Path: ...!weretis.net!feeder6.news.weretis.net!feeder7.news.weretis.net!news.mixmin.net!aioe.org!.POSTED.QcSrxfDl/MmnA2iJbNdbXw.user.gioia.aioe.org!not-for-mail
From: Herbert Kleebauer <klee@unibwm.de>
Newsgroups: alt.msdos.batch
Subject: Re: A failed output redirection does not set ERRORLEVEL?
Date: Mon, 11 Nov 2019 16:26:44 +0100
Organization: Aioe.org NNTP Server
Lines: 41
Message-ID: <qqbujk$144k$1@gioia.aioe.org>
References: <20191111164209.c970ed8e1393fe7982d4ce1b@g{oogle}mail.com>
NNTP-Posting-Host: QcSrxfDl/MmnA2iJbNdbXw.user.gioia.aioe.org
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Complaints-To: abuse@aioe.org
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101
 Thunderbird/68.2.1
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
Bytes: 1746

On 11.11.2019 14:42, Anton Shepelev wrote:

> I  can't seem to intercept an error while writing to
> a file at an invalid path:
> 
> @echo off
> type a > C:\NOSUCHNDIR   est.txt
> IF ERRORLEVEL 1 goto ERROR
> echo All is well.
> goto END
> :ERROR
> echo Error detected.
> :END
> 
> The script above writes:
> 
> The system cannot find the path specified.
> All is well.
> 
> C:\NOSUCHNDIR does not exist.  How can  I  test  the
> success of an output redirection?

type resets the errorlevel to 0 on success, so set it
to 1 before you execute type.

@echo off
call :set_err 1
echo test>a

type a > C:\NOSUCHNDIR\est.txt
IF ERRORLEVEL 1 goto ERROR
echo All is well.
goto END
:ERROR
echo Error detected.
:END

goto :eof

:set_err
exit /b %1