Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Error suppression

Aug
29
0
I have a script that monitors certain files on a shared network drive that are created & deleted by other processes running on the same or different hosts from the monitoring script.
Because this monitoring script runs independently of the processes that create & delete the files of interest, I've run into a timing issue with the following command:
if exist "%file" set age=%@fileage["%file"]
The symptom is that the script line will occasionally throw an error that "The system cannot find the file specified."

This indicates to me that the file exists at the time the condition is checked, but is deleted by the time the function can be executed. I've tried to add some redirection to the command (set age=%@fileage["%file"] 2> nul:) to suppress the error, but that doesn't appear to work.

I don't want to suppress the entire batch file's use of STDERR, just the one line where the error condition isn't relevant. Is there some way to make the function "quiet"?
 
I don't know if the inability to redirect the error message is an anomaly
or a feature. However, I did find that using ON ERROR REM allows you to
suppress the error.

I wrote a BTM script:
on error rem
echo %@fileage[%1]

I get no error text with an invalid input.

-Scott

millardjk <> wrote on 04/16/2010 03:10:39 PM:


> I have a script that monitors certain files on a shared network
> drive that are created & deleted by other processes running on the
> same or different hosts from the monitoring script.
> Because this monitoring script runs independently of the processes
> that create & delete the files of interest, I've run into a timing
> issue with the following command:if exist "%file" set
age=%@fileage["%file"]

>
> The symptom is that the script line will throw an error that "The
> system cannot find the file specified."
>
> This indicates to me that the file exists at the time the condition
> is checked, but is deleted by the time the function can be executed.
> I've tried to add some redirection to the command (set age=%
> @fileage["%file"] 2> nul:) to suppress the error, but that doesn't
> appear to work.
>
> I don't want to suppress the entire batch file's use of STDERR, just
> the one line where the error condition isn't relevant. Is there some
> way to make the function "quiet"?
>
>
>
>
 
Alternatively, you can assign a specific value to your age variable
instead of the REM command.

on error set age=0
set age=%@fileage[%1]
on error
echo age is %age

-Scott

samintz <> wrote on 04/16/2010 03:49:59 PM:


> I don't know if the inability to redirect the error message is an
anomaly

> or a feature. However, I did find that using ON ERROR REM allows you to


> suppress the error.
>
> I wrote a BTM script:
> on error rem
> echo %@fileage[%1]
>
> I get no error text with an invalid input.
>
> -Scott
>
> millardjk <> wrote on 04/16/2010 03:10:39 PM:
>
>
>
> ---Quote---
> > I have a script that monitors certain files on a shared network
> > drive that are created & deleted by other processes running on the
> > same or different hosts from the monitoring script.
> > Because this monitoring script runs independently of the processes
> > that create & delete the files of interest, I've run into a timing
> > issue with the following command:if exist "%file" set
> ---End Quote---
> age=%@fileage["%file"]
>
>
> ---Quote---
> >
> > The symptom is that the script line will throw an error that "The
> > system cannot find the file specified."
> >
> > This indicates to me that the file exists at the time the condition
> > is checked, but is deleted by the time the function can be executed.
> > I've tried to add some redirection to the command (set age=%
> > @fileage["%file"] 2> nul:) to suppress the error, but that doesn't
> > appear to work.
> >
> > I don't want to suppress the entire batch file's use of STDERR, just
> > the one line where the error condition isn't relevant. Is there some
> > way to make the function "quiet"?
> >
> >
> >
> >
> ---End Quote---
>
>
>
 
I have a script that monitors certain files on a shared network drive that are created & deleted by other processes running on the same or different hosts from the monitoring script.
Because this monitoring script runs independently of the processes that create & delete the files of interest, I've run into a timing issue with the following command:
if exist "%file" set age=%@fileage["%file"]
The symptom is that the script line will occasionally throw an error that "The system cannot find the file specified."

This indicates to me that the file exists at the time the condition is checked, but is deleted by the time the function can be executed. I've tried to add some redirection to the command (set age=%@fileage["%file"] 2> nul:) to suppress the error, but that doesn't appear to work.

I don't want to suppress the entire batch file's use of STDERR, just the one line where the error condition isn't relevant. Is there some way to make the function "quiet"?

TCC does variable expansion (including functions) before it checks for redirections. Otherwise, you wouldn't be able to redirect to a file specified by a variable.

You can use command grouping to make the redirection happen first:

Code:
set age=
( set age=%@fileage["%file"] ) >& nul
 
TCC does variable expansion (including functions) before it checks for redirections. Otherwise, you wouldn't be able to redirect to a file specified by a variable.

You can use command grouping to make the redirection happen first:

Code:
set age=
( set age=%@fileage["%file"] ) >& nul

While I like the simplicity of this technique, Scott's technique using "on error" is more "readable" for the other folks on my team that may have to edit this code in the future (they don't spend quite the same amount of time writing BTMs as I do).
 
Alternatively, you can assign a specific value to your age variable
instead of the REM command.

on error set age=0
set age=%@fileage[%1]
on error
echo age is %age

-Scott
Nice. I'm going to use this technique with a slight variation:
Code:
on error REM suppress "file not found" error in @fileage[]
if exist "%file" set age=%@fileage["%file"]
on error
iff defined age .and. %age lt %limit then
...
endiff
 

Similar threads

Back
Top