1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Error suppression

Discussion in 'Support' started by millardjk, Apr 16, 2010.

  1. millardjk

    Joined:
    Aug 22, 2008
    Messages:
    29
    Likes Received:
    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"?
     
  2. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,191
    Likes Received:
    11
    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:


    age=%@fileage["%file"]

     
  3. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,191
    Likes Received:
    11
    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:


    anomaly


     
  4. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,311
    Likes Received:
    39
    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. millardjk

    Joined:
    Aug 22, 2008
    Messages:
    29
    Likes Received:
    0
    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. millardjk

    Joined:
    Aug 22, 2008
    Messages:
    29
    Likes Received:
    0
    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
     

Share This Page