CREATING LOG FILES

By Amitesh Singh

 This is really great to make the log files. You should have some knowledge of dos commands (including batch file programming, redirection and pipeline tricks).

MAKING LOG FILES OF TXT

LAUNCHING LOG FILES

 First open a txt file or edit a file in dos by using edit command and save as "file_name.bat".

I will show you:

C:\>edit txtlog.bat

CODING OF LOGGING BATCH FILE

 The coding of the actual batch file that will log the user's activities is quiet simple.

A simple example:

@echo off

echo            TXT FILE>>c:\mylogfiles\usertxtlog.txt

echo %1>>c:\mylogfiles\usertxtlog.txt   /*send the file name of the file opened to the log file,c:\mylogfiles\usertxtlog.txt*/

notepad %1 /*launch notepad so that user does not know some thing going wrong*/

exit

 Well you can add date and time command in the batch file, but one problem is that you need to press enter too, which in turn has to be entered in the batch file too(try this and see what happen).

 

@echo off

echo             TXT FILE>>c:\mybatchfiles\amitesh.txt

time>>c:\mybatchfiles\amitesh.txt

echo FILE NAME: %1>>c:\mybatchfiles\amitesh.txt

date>>c:\mybatchfiles\amitesh.txt

time>>c:\mybatchfiles\amitesh.txt

notepad %1

exit

 As you are the system administrator so this can give some clue to the user that some thing is wrong in my computer. You can also remove this fault by using pipeline tricks which I am now going to discuss.

DATE and TIME are basically designed to interactively set the computer's record of today's date and current time, but if we only want to use this commands only to show the current date and time.

 We can use redirection of input to hand the enter key to the date and time program. First, we create an empty batch file (let name is empty.bat in C: directory) which is equivalent of the enter key pressed by itself.

Then we dos a command in this manner:

c:\>time<empty.bat

 Now we can make the above batch file smarter in this way:

TXTLOG.BAT

@echo off

echo                 TXT FILE>>c:\mybatchfiles\amitesh.txt

echo FILE NAME: %1>>c:\mybatchfiles\amitesh.txt

date<c:\empty.bat>>c:\mybatchfiles\amitesh.txt

time<c:\empty.bat>>c:\mybatchfiles\amitesh.txt

notepad %1

exit

 Now locate any text file, select it (at once) and press the SHIFT key. Keeping the SHIFT key pressed, right click on the txt file to bring up the OPEN WITH.... option. Click on the OPEN WITH... option and choose OTHER button and locate the batch file program which contains the logging code and selecting the icon "always use this program to open this type of files. Now click OPEN and OK.

 I include exit command at the end of program to "MAKE THE LOGGING BATCH FILE TO ESCAPE FROM THE USER'S EYES".

When any TXT file will open there will be log entry in the file named amitesh.txt.

You can attach this batch file to other types of files like *.dat,*.html,*.ps..etc.

NOTE :Using the above tricks you can easily change the files on which you want  to keep an eye.

CREATING LOGFILE IN WORDPAD

If you want to make log files in WordPad document then in the TXTLOG.BAT instead of writing "notepad %1", write "write %1".

.

HOW TO MAKE LOG FILES ON THE NET (only for Windows XP)

 If you have some knowledge of TELNET service then you can make log files of each server connection.

First go to dos prompt.

Type 'telnet' & press enter.

Now type set?

Set log file c:\lognotes.txt.

This txt file will store all the works you have done on the net.

You can make a batch file program which makes log files without going to type log files each time.

Examples:

TEL.BAT

@echo off

telnet

set logfile c:\log.txt

open %1 %2

HOW YOU RUN IT

C:\>TEL.BAT pop.mail.yahoo.com 110

ADTELNET.BAT

@echo off

echo Welcome to the 'TELNET' service

echo type of server(ports)

start

echo 1:TELNET(23)   

echo 2:FTP(21)

echo 3:SMTP(25)

echo 4:POP(110)

echo 5:HTTP(80)

echo 6:EXIT

choice "choose the service"/c:123456 /n

if errorlevel 6 goto end

if errorlevel 5 start c:\telnet %1 80

if errorlevel 4 start c:\telnet %1 110

if errorlevel 3 start c:\telnet %1 25

if errorlevel 2 start c:\telnet %1 21

if errorlevel 1 start c:\telnet %1 23

:end

HOW YOU RUN IT

C:\>adtelnet.bat hotpop.com

 

AUTHOR Amitesh Singh

[email protected]

ISM

DHANBAD



Got something to add? Send me email.



14 comments



Increase ad revenue 50-250% with Ezoic


More Articles by © Amitesh Singh




---September 28, 2004







Wed Aug 12 09:43:23 2009: 6742   anonymous

gravatar
How to create a log file when multiple files in one directory are accessed, installed, copy etc

For example: C:\applications contains following files.

app1.exe
app2.exe
app3.exe
app4.exe
app5.exe


Assume app1.exe is selected for installation.
app2.exe is selected for copy.

log.txt should look like this:

Date Time app name operation
12-08-2009 23:00 app1.exe installation
12-08-2009 23:12 app2.exe copy






Wed Aug 12 10:09:07 2009: 6743   TonyLawrence

gravatar
app1.exe > yourlog
app2.exe >> yourlog
app3.exe >> yourlog



Wed Aug 12 10:28:38 2009: 6744   anonymous

gravatar
i m a beginner
i need to know about the process of creating log file for the above mentioned task



Wed Aug 12 10:36:36 2009: 6745   TonyLawrence

gravatar
And that's what I told you. Use a single ">" for app1, use ">>" for app2 and "yourlogfile" will contain what you want.



Wed Aug 12 17:02:38 2009: 6746   anonymous

gravatar
I dont know about programming in Windows
can you let me know how to create a log file and then include all the details as mentioned



Thu Aug 13 12:42:33 2009: 6748   TonyLawrence

gravatar
I did exactly that. You need to create a .bat file containing the things you want to run with redirection as I explained.









Thu Aug 13 15:57:47 2009: 6749   anonymous

gravatar
I did what you asked me to do, but the log file created contained no information. It was a blank file






Thu Aug 13 16:04:06 2009: 6750   TonyLawrence

gravatar
Then your applications are not writing to STDOUT.

That's often a problem with Windows programs: Windows programmers tend to be clueless, so simple things become difficult.

You really should go to a Windows site to get more help. We don't like Windows or Microsoft much here.



Wed May 18 06:49:57 2011: 9496   gmav

gravatar


you can use the command
time /t

this gives you the time without the need to press enter after that



Thu Jun 21 12:19:41 2012: 11127   anonymous

gravatar


hi how to create a log file for the same example you explained using powershell scripting



Thu Jun 21 12:32:04 2012: 11128   TonyLawrence

gravatar


You may not find much help for that here. We are mostly Unix/Linux people.



Wed Oct 31 09:21:47 2012: 11411   pascal

gravatar


hello every1
i figured, that if i run my batch via:
test.bat > testlog.txt 2> testerrors.txt
it will output "normal" text to testlog.txt and errors to testerrors.txt
however, when i run it like this, it will not display the execution of my batch in console and therefore, the user cannot press "any key" - which he should after a couple minutes. how can i log my batch and errors and still have my batch executed in cmd visibly?



Wed Oct 31 11:24:33 2012: 11412   TonyLawrence

gravatar


You need to do redirection within test.bat AS IS EXPLAINED IN THE ARTICLE AND IN COMMENTS.



Thu Nov 8 23:13:26 2012: 11423   DiegoBarboza

gravatar


You can avoid being asked to change date and time by adding /T modifier. For instance,

TIME /T

Will show current time and wont ask to change it; you can also use the global variable %time% to show current time:

echo %time%

Regards.

------------------------
Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

privacy policy