Is it possible to test result of redirection?

S

Stephen Quist

Hi,

I'd like to find out if it is possible to test whether a command redirection
operation,
for instance, > or >> succeeded. I ran a quick test and found that
errorlevel was
0 after I tried one that I knew failed. Is this even possible?

Thanks,

Steve
 
M

Matthias Tacke

:
...
I'd like to find out if it is possible to test whether a command redirection
operation,
for instance, > or >> succeeded. I ran a quick test and found that
errorlevel was
0 after I tried one that I knew failed. Is this even possible?
The errorlevel is set by the command you ran, if an operation of the
os fails it might be in the error output, normally the screen if not
itself redirected with 2>.

hth
Matthias
 
R

Ritchie

Stephen Quist said:
I'd like to find out if it is possible to test whether a command redirection
operation,
for instance, > or >> succeeded. I ran a quick test and found that
errorlevel was
0 after I tried one that I knew failed. Is this even possible?

Yes, use the 'conditional processing symbols', that's the double pipe and
double ampersand to you and me. For example:-

md a
dir>a || echo/Redirection failed
 
O

Ole

I'd like to find out if it is possible to test whether a command
redirection
operation,
for instance, > or >> succeeded. I ran a quick test and found that
errorlevel was
0 after I tried one that I knew failed. Is this even possible?
Thanks,
Steve

i'd just use an if exist command after the redirection. Cause that's only
any good if the file didn't exist prior.

As for testing if it doesn't set an errorlevel make a copy of notepad.exe
run it and then try and overwrite (should fail cause its in use) with a >,
at the end of the batch stick:
echo %errorlevel%
 
S

Stephen Quist

Those are good ideas. Thanks to you and to Ritchie and Matthias.
I'll play around with these ideas and see if I can come up with
something.

Steve

Ole wrote:
::: I'd like to find out if it is possible to test whether a command
::: redirection operation,
::: for instance, > or >> succeeded. I ran a quick test and found that
::: errorlevel was
::: 0 after I tried one that I knew failed. Is this even possible?
::: Thanks,
::: Steve
::
:: i'd just use an if exist command after the redirection. Cause that's
:: only any good if the file didn't exist prior.
::
:: As for testing if it doesn't set an errorlevel make a copy of
:: notepad.exe run it and then try and overwrite (should fail cause its
:: in use) with a >, at the end of the batch stick:
:: echo %errorlevel%
::
:: --
:: Ole
 
G

Guest

@ECHO OFF
ECHO LINE 1 TEST > file.ext || GOTO FAIL
ECHO LINE 2 TEST >> file.ext || GOTO FAIL
IF NOT EXIST file.ext GOTO FAIL
DIR c:\folder\file.ext | FIND/I "1 FILE(S) 0 BYTES"
IF ERRORLEVEL 1 GOTO SUCCESS
IF ERRORLEVEL 0 GOTO FAIL
:FAIL
echo error
GOTO END
:SUCCESS
echo success
:END
 
G

Guest

Replace any parts of this script with the commands you require.

This script is 13 lines total.
It may appear to be broken into more on screen.
There must be exactly 14 spaces between FILE(S) and 0 BYTES.
IF ERRORLEVEL 1 GOTO SUCCESS and IF ERRORLEVEL 0 GOTO FAIL
may look wrong at first glance, but it is correct.

@ECHO OFF
ECHO LINE 1 TEST > file.ext || GOTO FAIL
ECHO LINE 2 TEST >> file.ext || GOTO FAIL
IF NOT EXIST file.ext GOTO FAIL
DIR c:\folder\file.ext | FIND/I "1 FILE(S) 0 BYTES"
IF ERRORLEVEL 1 GOTO SUCCESS
IF ERRORLEVEL 0 GOTO FAIL
:FAIL
echo error
GOTO END
:SUCCESS
echo success
:END

This covers four different scenarios that may arise:
1. A command for redirection is incorrect (bad command entered or typo).
2. The file cannot be created (access denied issue).
3. The file cannot have data added to it (read-only file).
4. The file is created, but is empty (0 bytes).

Austin M. Horst
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top