how to capture file not found in MS dos command prompt and write to event log

CalmSoul
CalmSoul used Ask the Experts™
on
I have a batch file which runs the following command

dir *file* /s

Sometime is give message "file not found" I would like to capture this file not found message in event log

How is this possible?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Put your command like this:
dir *file* /s >> D:\dos_message.txt
And messages will be appended into this file. Better still save as D:\dos_message.evt and then in Event Viewer window, choose Action menu => Open Log File....

Author

Commented:
I would like to automatically send in event log
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Since XP there is a command called eventcreate to report events via batch file.
The least you have to provide is the following

eventcreate /ID 1 /T Warning /D "The file has not been found"


Commented:
CalmSoul

When you do the folowing:

   DIR /S *file*

you are basically searching for any file which has the word "FILE" in it's filename OR extension name. The search will start from the current folder and include ALL of it's sub-folders (due to the '/S' command line option).

You will receive a "File not found" error message from DOS if no files exist with the word "FILE" in it's filename or extension name.

The problem with using the '/S' command line switch is that part of the message is sent to STDOUT and part of the message is sent to STDERR.

The following part is sent to STDOUT:

   Volume in drive C is ....
   Volume Serial Number is ....

While, if no files match the search pattern *FILE* then the following message is sent to STDERR

   File not found

If, on the other hand, there are matching files then the filenames of the matching files are also sent to STDOUT.

This means, if you don't want the message "Volume in drive C etc..." displayed but you want to display the filenames then this poses a slight problem.

Supressing the error message by redirecting it to NUL is not a problem. This can be done with the following:

   DIR /S *file* 2>NUL

but you will still get the "Volume in drive C etc...." message displayed even if there are no files.

Therefore, better way to use the '/S' switch is to use it in comjunction with the '/B' switch as in the following:

   DIR /B /S *file*

because this will output the full path and filename for each file that matches the search pattern *FILE*.

The advantage of using the '/B' switch is that it only displays filenames. This means you will not get the "Volume in drive C ....." message displayed.

So now, only filenames are sent to STDOUT while errors are still sent to STDERR.

Furthermore, you can combine this with redirecting STDERR to the NUL device as in the folowing:

   DIR /B /S *file* 2>NUL

Now, you will only get output to the screen if any files match the search pattern. If there are no matching files, you will not get any output at all.

Here's a tip.

You can 'silently' test whether there are any matching files prior to issuing your DIR command. Depending on the result of this 'silent' test, you can then customise your DIR command knowing in advance what results are likely to be output either to the the screen or to a file if using redirection. This is how you do it:

   DIR /S *file* >NUL 2>&1                         <-- This does a silent search
   IF %ERRORLEVEL% EQU 0 (                <-- ERRORLEVEL is automatiaclly set depending on the result
      REM Matching files were found
      DIR /S *file* >filelist.txt                        <-- Output - will only output filenames and NEVER errors
   ) ELSE (
      REM Matching files were NOT found
      ECHO NUL >filelist.txt                         <-- Don't have to do anything if you don't want to.
   )

This will create a file named FILELIST.TXT with a list of matching filenames. If there are no matching filenames then an empty file is created - although you could just as well not do anything.

Personally, in the above example, I would be inclined to use the the '/B' switch in the 'DIR /S *file* >filelist.txt' command as in the following:

   DIR /S *file* >NUL 2>&1
   IF %ERRORLEVEL% EQU 0 (
      REM Matching files were found
      DIR /B /S *file* >filelist.txt
   )

Commented:
If you definitely want an error message sent to a log file if there are no matching files then you could do it like this (see the explantion in my previous comment):

   DIR /S *file* >NUL 2>&1
   IF %ERRORLEVEL% EQU 1 (
      ECHO There are no matching files in %CD% >%~n0.log
   )

This will create a log file with the same base filename as the batch file itself, and write a message to the log file if there are no matching files. The %CD% just includes the path and folder you searched from.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
I would redirect the output of the first dir, and on error ignore it. No need to do the dir twice.


@echo off
dir /S /b *file* > filelist.txt 2> nul && (
  echo ok
  rem aso.
) || (
  eventcreate /ID 1 /T Warning /D "The file has not been found"
  rem aso.
)

Open in new window

Commented:
Well, of course, we're all stabbing about in the dark here as the asker has not made it ABSOLUTELY clear what he wants.

If it surely IS "Windows' Event Log" he wants to send this message to (instead of a file) then personally I prefer the following code:

   DIR /S *file* >NUL 2>&1
   IF %ERRORLEVE% EQU 1 (
      EVENTCREATE /T ERROR /ID 1 /SO %~nx0 /L APPLICATION /D "File not found: *file*" >NUL
   )
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
t0t0,

the statement made twice, "I would like to automatically send in event log", clearly states it should appear in EventLog.

Commented:
Qlemo.... It does appear to sound like "Event Log" but is that what he really means? - Because none of his example codes in any of his posts include EVENTCREATE.

However, I have included it here as a possible solution.

Commented:
Oh no not again.... will leave this one to you :-)

Steve

Author

Commented:
t0t0:

I am getting following error

DIR /S *file*  1>NUL 2>&1
1 was unexpected at this time.

C:\File>   IF  EQU 1 (

Qlemo:

Your solutions doesn't work

Commented:
Try:

   DIR /S *file* >NUL 2>&1

as shown in my text.

The followinf doesn't make sense:

   C:\File>   IF  EQU 1 (

You get this type of errer when the IF is missing something that should appear infront of the word EQU. As it is, it says the following:

   IF equals 1 then....

If WHAT equals '1'?.... a variable of yours is not expanding. Chech the variable. Is it a delayed expansion variable? Is it spelt incorrcetly? is it not SETting somewhere where it should be?....

Put double-quotes around both Lvalue and Rvlaue as in:

   IF "number" EQU "1" (....

Or:

   IF "number"=="1" (....

Command line parameters are usually placed inside double-quotes for this reason: in case the parameter does not exist in which case you get this very problem.

Come back if you try those suggestions and they don't work.

Author

Commented:
Here is what I am using

DIR /S *file* >NUL 2>&1
   IF %ERRORLEVE% == "1" (
      EVENTCREATE /T ERROR /ID 1 /SO %~nx0 /L APPLICATION /D "File not found: *file*" > NUL
   )

and I got following error

( was unexpected at this time.

Commented:
%ERRORLEVE% should be %ERRORLEVEL%

Commented:
and you probably actually want

if errorlevel 1 (

)

which works for any error.

Steve

Commented:
CalmSoul

I had to nip out to collect my child from school...

I was rushing ealier and didn't realise it was MY code you were refering to. Glad you got there in the end. Lots of apologies for not spotting such an obvious error myself.

Thank you for acceptin gmy answer as your solution. Thank you also for the points and grade.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial