Error suppression

#1
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"?
 

samintz

Scott Mintz
May 20, 2008
1,271
11
Solon, OH, USA
#2
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"?
>
>
>
>
 

samintz

Scott Mintz
May 20, 2008
1,271
11
Solon, OH, USA
#3
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---
>
>
>
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,575
46
Albuquerque, NM
prospero.unm.edu
#4
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
 
#5
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).
 
#6
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