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.
LVL 5
CalmSoulAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve KnightIT ConsultancyCommented:
You can use the eventcreate command to ... well create an event :-)

On 2003 you can get the help from eventcreate /? or look here:
http://technet.microsoft.com/en-us/library/cc773003(WS.10).aspx

As to checking for the text file I imagine you want something like this

@echo off
set finddin="c:\yourdir\*.txt"
if exist %finddir% eventcreate /t error /id 9999 /l application /d "Text file of your event"

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?

Steve
0
Steve KnightIT ConsultancyCommented:
Obviously you can log this to the System or other logs, define your own event code etc. by changing the command line options
0
Steve KnightIT ConsultancyCommented:
but don't try id 9999 as max is 999 :-)
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

t0t0Commented:
nice tip dragon-it
0
CalmSoulAuthor Commented:
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??

0
CalmSoulAuthor Commented:
your solution is not writing to event log... do I have install anything?
0
Steve KnightIT ConsultancyCommented:
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
0
CalmSoulAuthor Commented:
I am first trying this on winxp is that an issue?
0
Steve KnightIT ConsultancyCommented:
Yes, this is a Windows 2003 server only command sadly (afaik) :-)
0
Steve KnightIT ConsultancyCommented:
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...
0
CalmSoulAuthor Commented:
C:\>eventcreate
ERROR: Invalid Syntax. Mandatory option '/t' is missing.
Type "EVENTCREATE /?" for usage.
0
Steve KnightIT ConsultancyCommented:
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
0
Steve KnightIT ConsultancyCommented:
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"
0
CalmSoulAuthor Commented:
working .. it possible to put file name and file text in error
0
Steve KnightIT ConsultancyCommented:
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
0
CalmSoulAuthor Commented:
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
....
....

0
Steve KnightIT ConsultancyCommented:
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
0
CalmSoulAuthor Commented:
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]
0
Steve KnightIT ConsultancyCommented:
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
0
CalmSoulAuthor Commented:
Yes, how we can write it?
0
Steve KnightIT ConsultancyCommented:
away from computer right now. will look back later if t0t0 doesn't write one first!
0
t0t0Commented:
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
0
Steve KnightIT ConsultancyCommented:
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
0
CalmSoulAuthor Commented:
Date is in this format

20090717

0
t0t0Commented:
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
0
CalmSoulAuthor Commented:
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

0
t0t0Commented:
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?
0
t0t0Commented:
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?

0
t0t0Commented:
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
0
Steve KnightIT ConsultancyCommented:
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
0
CalmSoulAuthor Commented:
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.
0
Steve KnightIT ConsultancyCommented:
What is your date format:

echo %date%

Steve
0
CalmSoulAuthor Commented:
my pc date is

Fri 07/17/2009
0
t0t0Commented:
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.
0
t0t0Commented:

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

really off now !!
0
t0t0Commented:
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
0
CalmSoulAuthor Commented:
I have spaces in my folder path is that going to make a difference
0
t0t0Commented:
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
0
CalmSoulAuthor Commented:
everytime is runs its creating funny folders...

2009Fr
2009Fr 0

This is my date folder

20090717
0
t0t0Commented:
okay...i understand now. you did not provide use with the correct date format so here we go again....
0
CalmSoulAuthor Commented:
I think you forgot to copy paste code
0
t0t0Commented:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET today=c:\folder\%DATE:~-4%%DATE:~-10,2%%DATE:~-7,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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
t0t0Commented:
Phew!! I hope it's what you wanted I'm glad we got there in the end. Thank you.
0
CalmSoulAuthor Commented:
I have few other queries I will post tongiht....
0
Steve KnightIT ConsultancyCommented:
looks like you all had fun there with that one :-)  thanks for the assist points.

Steve
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.