Solved

batch file writing to event log.

Posted on 2009-07-16
46
2,553 Views
Last Modified: 2013-11-10
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.
0
Comment
Question by:CalmSoul
  • 17
  • 16
  • 13
46 Comments
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 200 total points
ID: 24870858
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24870870
Obviously you can log this to the System or other logs, define your own event code etc. by changing the command line options
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24870886
but don't try id 9999 as max is 999 :-)
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24871716
nice tip dragon-it
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 24871811
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
 
LVL 5

Author Comment

by:CalmSoul
ID: 24872349
your solution is not writing to event log... do I have install anything?
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24872782
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
 
LVL 5

Author Comment

by:CalmSoul
ID: 24873008
I am first trying this on winxp is that an issue?
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24873431
Yes, this is a Windows 2003 server only command sadly (afaik) :-)
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24873465
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
 
LVL 5

Author Comment

by:CalmSoul
ID: 24873508
C:\>eventcreate
ERROR: Invalid Syntax. Mandatory option '/t' is missing.
Type "EVENTCREATE /?" for usage.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24873651
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24873690
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
 
LVL 5

Author Comment

by:CalmSoul
ID: 24873804
working .. it possible to put file name and file text in error
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24874211
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
 
LVL 5

Author Comment

by:CalmSoul
ID: 24874903
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24876935
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
 
LVL 5

Author Comment

by:CalmSoul
ID: 24878157
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24878180
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
 
LVL 5

Author Comment

by:CalmSoul
ID: 24879548
Yes, how we can write it?
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24879978
away from computer right now. will look back later if t0t0 doesn't write one first!
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24881042
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24881100
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 5

Author Comment

by:CalmSoul
ID: 24881206
Date is in this format

20090717

0
 
LVL 16

Expert Comment

by:t0t0
ID: 24881317
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
 
LVL 5

Author Comment

by:CalmSoul
ID: 24881401
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
 
LVL 16

Expert Comment

by:t0t0
ID: 24881448
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
 
LVL 16

Expert Comment

by:t0t0
ID: 24881559
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
 
LVL 16

Expert Comment

by:t0t0
ID: 24881995
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24882082
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
 
LVL 5

Author Comment

by:CalmSoul
ID: 24882151
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24882455
What is your date format:

echo %date%

Steve
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 24882468
my pc date is

Fri 07/17/2009
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24882495
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
 
LVL 16

Expert Comment

by:t0t0
ID: 24882498

dragon-it, i see you're back again. I did ask CalmSoul to confirm his date format and he replied "20090717"
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24882534
Just passing :-) I mean't his OS date in case it wasn't ddd mm/dd/yyyy  

really off now !!
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24882548
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
 
LVL 5

Author Comment

by:CalmSoul
ID: 24882691
I have spaces in my folder path is that going to make a difference
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24882715
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
 
LVL 5

Author Comment

by:CalmSoul
ID: 24882759
everytime is runs its creating funny folders...

2009Fr
2009Fr 0

This is my date folder

20090717
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24882840
okay...i understand now. you did not provide use with the correct date format so here we go again....
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 24882864
I think you forgot to copy paste code
0
 
LVL 16

Accepted Solution

by:
t0t0 earned 300 total points
ID: 24882866
@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
 
LVL 16

Expert Comment

by:t0t0
ID: 24883969
Phew!! I hope it's what you wanted I'm glad we got there in the end. Thank you.
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 24883984
I have few other queries I will post tongiht....
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24885562
looks like you all had fun there with that one :-)  thanks for the assist points.

Steve
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now