Link to home
Start Free TrialLog in
Avatar of CalmSoul
CalmSoulFlag for United States of America

asked on

batch file writing to event log.

I am looking to write a batch file ... which will run every 1 hour using windows 2003 server scheduler and if it see a text file (any text file) in a specific folder it writes to windows event log with message error found.
SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Obviously you can log this to the System or other logs, define your own event code etc. by changing the command line options
but don't try id 9999 as max is 999 :-)
nice tip dragon-it
Avatar of CalmSoul

ASKER

Steve:

How is this possible ?

We can do something with picking up which text file or taking the event details from the text file if needed.  What do you want??

your solution is not writing to event log... do I have install anything?
ok forget the if command for a while. have you tried the other command by itself, should work fine on 2003 server - works on mine.
n Please post what you are trying.

As to your last Q we can read a text file or the names of a text file or content in it and then act on it.  what do you want it to do, or just a genral event error saying "a file is there..."

Steve
I am first trying this on winxp is that an issue?
Yes, this is a Windows 2003 server only command sadly (afaik) :-)
Don't have xp machine to hand but actually it seems it is on XP....
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/eventcreate.mspx?mfr=true

What happens when you type eventcreate at a cmd.exe prompt...
C:\>eventcreate
ERROR: Invalid Syntax. Mandatory option '/t' is missing.
Type "EVENTCREATE /?" for usage.
Ok, good. So it is there then.  Try this:

eventcreate /t error /id 999 /l application /d "Text file of your event"

Then have a look in event viewer under the application log.
Steve
Looking at my original post on a decent screen... it seems there is two different variable names so if you were trying literally as I posted, try this:

@echo off
set finddin="c:\yourdir\*.txt"
if exist %finddin% eventcreate /t error /id 999 /l application /d "Text file of your event"
working .. it possible to put file name and file text in error
Thanks. Ok we need to know more then.  Are there going to be multiple text files, are you wanting an event each?  We can certainly add the filename into the event, e.g. as follows but could only maybe pull out a certain line or such of the text file as it all has to fit on one commandline to submit it.

@echo off
set finddin="c:\yourdir\*.txt"
For /f %%a in (%finddin%) do eventcreate /t error /id 999 /l application /d "File: %%a found"

Steve
Steve:

Things is we are going to have may be one file or may be two files or may be more... We don't want to repeat error logs...

secondly, my folder structure will be like this

c:\folder\20090716\look for txt files
c:\folder\20090715\look for txt files
....
....

So you're changing the goalposts again really... started off asking for checking for a file in a specific directory and then logging an event, now you don't want multiple events.  Please just say what you DO want.  

Do you want one log regardless of the amount of entries in the dir?
Where do the multiple directories come in?
What do you want in the log text?

Unfortunately it is not possible to guess what you want!

Steve
Do you want one log regardless of the amount of entries in the dir?
[Yes, one log file]
Where do the multiple directories come in?
[Sorry, I originally forgot about this ... we create folder based on date.. for example today is 7172009...
A new folder will be created for this date and then we have to look for text files under today's date folder.
So out batch file should check for text files under today's date
What do you want in the log text?
[In the log file I want the name of the text file]
OK clearer now.  You say there may be more than one text file though, so presumably then.... we need to:

Find todays date and the right dir
find any files in there and record all their filenames in the same event log entry

Steve
Yes, how we can write it?
away from computer right now. will look back later if t0t0 doesn't write one first!
CalmSoul

I don't want to take points away from dragon-it however, I notice my name mentioned. Was that for a cry of help perhaps? I don't know.

Please try the following batch file:

IMPORTANT NOTE: If you type: 'ECHO %DATE%' in a DOS box, you can confirm your date format. if it differs from 'DD/MM/YYYY' then let us know as the batch file below uses DD/MM/YYYY format and it will need modifying.


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET today=c:\folder\%DATE:~-4%%DATE:~3,2%%DATE:~0,2%
SET error=35

IF NOT EXIST !today!\ (
   MD !today!
)

IF EXIST !today!\*.txt (
   SET msg=File^(s^) found in !today!:
   FOR %%a IN (!today!\*.txt) DO (
      SET msg=!msg! %%~nxa
   )
   EVENTCREATE /T ERROR /ID !error! /SO %~nx0 /L APPLICATION /D "!msg!" >NUL
)

EXIT /B
Haha no I was on the mobile and despite a qwerty keyboard batch files with lots of % ~ an the like aren;t the easitest to type in...  quite happy to share points there as you got there first fair and square...

Steve
Date is in this format

20090717

Okay, in that case try this modified batch file:

NOTE: If you accept my code, please ALSO share points with dragon-it because he has covered much ground.


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET today=c:\folder\%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%
SET error=35

IF NOT EXIST !today!\ (
   MD !today!
)

IF EXIST !today!\*.txt (
   SET msg=File^(s^) found in !today!:
   FOR %%a IN (!today!\*.txt) DO (
      SET msg=!msg! %%~nxa
   )
   EVENTCREATE /T ERROR /ID !error! /SO %~nx0 /L APPLICATION /D "!msg!" >NUL
)

EXIT /B
t0t0:

I am talking about different question ... not this one..

You have provided me a solution which worked... but I want to capture results in event log? is it possible?

here the example code...


@echo off
DEL %~n0.log 2>NUL
 
FOR /F "TOKENS=*" %%a IN ('DIR /AD /B /S c:\folder\results\*') DO (
   IF NOT EXIST %%a\results.txt (
      ECHO %%a >>%~n0.log
   )
)
 
IF EXIST %~n0.log (
   ECHO Some folders did not contain Results.txt. See %~n0.log for details.
)

Open in new window

Could you please explain what you mean by "capture results in event log".

Do you mean you want the names of the .TXT files written to a file with a .LOG extension?
Sorry, I didn't see your example code... Of course you can do something like that. The question now is do you still want to use Windows' Events for capturing errors too (ie 'eventcreate') - or is this not required whatsoever?

I recognise the example code... I wrote it myself in another thread.

Anyway, please try this (if this is what you are after)


@ECHO OFF
SET today=%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%
MD "c:\folder\%today%" 2>NUL
DEL "%~n0.log" 2>NUL

IF EXIST "c:\folder\%today%\*.txt" (
   FOR %%a IN ("c:\folder\%today%\*.txt") DO (
      ECHO %%~nxa >>"%~n0.log"
   )
)
EXIT /B
I think (and you confuse me CalmSoul) is he was asking about two different things .... but heh I think I'll leave him with you now, signing off for the night!  Good luck!

Steve
t0t0:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET today=c:\folder\%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%
SET error=35

IF NOT EXIST !today!\ (
   MD !today!
)

IF EXIST !today!\*.txt (
   SET msg=File^(s^) found in !today!:
   FOR %%a IN (!today!\*.txt) DO (
      SET msg=!msg! %%~nxa
   )
   EVENTCREATE /T ERROR /ID !error! /SO %~nx0 /L APPLICATION /D "!msg!" >NUL
)

EXIT /B

This is not running properly .. here is the error

The syntax of the command is incorrect.
What is your date format:

echo %date%

Steve
my pc date is

Fri 07/17/2009
it worked fine for me.... i can only assume the format of your date is not what you stated it is. Please try again with this one and let us know if you are still getting the same error.


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET today=c:\folder\%DATE:~-4%%DATE:~3,2%%DATE:~0,2%
SET error=35

IF NOT EXIST !today!\ (
   MD !today!
)

IF EXIST !today!\*.txt (
   SET msg=File^(s^) found in !today!:
   FOR %%a IN (!today!\*.txt) DO (
      SET msg=!msg! %%~nxa
   )
   EVENTCREATE /T ERROR /ID !error! /SO %~nx0 /L APPLICATION /D "!msg!" >NUL
)

EXIT /B

This is not running properly .. here is the error

The syntax of the command is incorrect.

dragon-it, i see you're back again. I did ask CalmSoul to confirm his date format and he replied "20090717"
Just passing :-) I mean't his OS date in case it wasn't ddd mm/dd/yyyy  

really off now !!
Well that makes all the difference!!! Please try this.


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET today=c:\folder\%DATE:~-4%%DATE:~0,2%%DATE:~3,2%
SET error=35

IF NOT EXIST !today!\ (
   MD !today!
)

IF EXIST !today!\*.txt (
   SET msg=File^(s^) found in !today!:
   FOR %%a IN (!today!\*.txt) DO (
      SET msg=!msg! %%~nxa
   )
   EVENTCREATE /T ERROR /ID !error! /SO %~nx0 /L APPLICATION /D "!msg!" >NUL
)

EXIT /B
I have spaces in my folder path is that going to make a difference
Your example doesn't indicate this. You'll need this then:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET today=c:\folder\%DATE:~-4%%DATE:~0,2%%DATE:~3,2%
SET error=35

IF NOT EXIST "!today!\" (
   MD "!today!"
)

IF EXIST "!today!\*.txt" (
   SET msg=File^(s^) found in !today!:
   FOR %%a IN ("!today!\*.txt") DO (
      SET msg=!msg! %%~nxa
   )
   EVENTCREATE /T ERROR /ID !error! /SO "%~nx0" /L APPLICATION /D "!msg!" >NUL
)

EXIT /B
everytime is runs its creating funny folders...

2009Fr
2009Fr 0

This is my date folder

20090717
okay...i understand now. you did not provide use with the correct date format so here we go again....
I think you forgot to copy paste code
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Phew!! I hope it's what you wanted I'm glad we got there in the end. Thank you.
I have few other queries I will post tongiht....
looks like you all had fun there with that one :-)  thanks for the assist points.

Steve