Tuesday 20 May 2008

I have been thinking

[Note: the thinking process has not brought on fits, high fever, sweating, or any other adverse effects until now. I should engage in this activity more often perhaps?]

I don't really like the idea of complicating life with lots of useless rules but I am starting to believe that 'good practices' really also do apply to batch files as simple as they may be, as they apply to more or less anything when it comes to sharing, reusing some nifty piece of code, or deciding to chuck it for now.

So, I may be coming up with some more generalist posts on this issue, although it may quickly get superseded by other pressing issues I would like to share with you.

One of these (and this is the true reason behind my short absence from the blogosphere) is that I am working on a personal website.

Why?

Well, firstly, I would like to provide a platform to download code which I have posted to this blog (for you ;-)).

Secondly, I would like to actually start working on a project of mine that's been simmering for some time: Anxcity, a website for English and French speaking anxiety disorder sufferers. And, hopefully, I can tag Dutch onto that too!

As you probably know anxiety disorders affect up to 18% of adult population at one time in their lives (according to U.S statistics). Which means about one in five people suffer from some form of anxiety. Which in turn means you are either a sufferer yourself or are close to someone who is, has been or will be.

So, let's recap some of the most common disorders: Generalized Anxiety Disorder (GAD), Obsessive-Compulsive Disorder (OCD), Panic Disorder (PD), Social Anxiety Disorder, Agoraphobia, Impulse Phobia, Specific Phobias, Post Traumatic Stress Disorder (PTSD) and ... I have probably forgotten some.

For most people their disorders are only slight and therefore hardly debilitating, but for others anxiety plays a major, even central, role in their lives, preventing them from doing the most trivial things: food and clothes shopping, working, going out with friends or family, visiting places, traveling, ... you name it.

Of course, at this stage you have already betted a month's wages that I myself am a sufferer, and you were silly not to bet two :p. I have Panic Disorder with Agoraphobia and Social Anxiety Disorder. And finding 'help' has been quite a challenge by itself! The internet, with its vast pool of information and links and references and addresses, wasn't able to provide the answers I was looking for. Yup! Incredible but true. Things are slowly changing and I would like to help bring on that change. That is what the Anxcity project is all about: bringing on awareness, understanding and a common forum for sufferers to share experiences, and stories. And more importantly, a place to share answers to the most important question: where to get help.

So, that's what this project is about. And I hope to be getting it running shortly. Any suggestions, ideas, comments, questions, ... are welcome.

Just drop me a line at: elinor [DOT] hurst [AT] gmail [DOT] com.

Thoughts?

How to clear the log file

Now, there is one important issue we didn't cover in our previous post about logs: how to clear the log when it starts taking up too much space.

Probably the simplest way to do this is to redirect type NUL to your log file, as follows:

type NUL > your_log.log


That's it! You will find your_log.log file size drops back to 0 once you have called this command.

Thoughts?

Monday 19 May 2008

Using CON... or not

Hi folks. I am back from the wilderness of, well, my inner-self I guess.
And here with another tip: using CON or not.

When you are programming a batch file, it's usually pretty useful to get the output to show on screen. Why? Well, simply because it saves you the trouble of finding and opening a potential log file just to read some silly message about a file not being found, or some syntax error for instance.

So, let's start from the top.

1. How do I redirect output from the console to a logfile?
Here are perhaps the more useful redirection techniques.

@echo off
echo Hello World > hello.txt
echo Press any key to quit...
pause > NUL

This will redirect the command echo Hello World to the hello.txt file (which is created in the directory where the batch file is located).
You can immediately see the way to turn this into a logging functionality:

@echo off
echo Something happened > my_log.log
echo Press any key to quit...
pause > NUL

The poor thing about this is, if you run the batch twice consecutively you will loose the first output. The > is in effect making sure the file is rewritten completely. If you want to append to a file, simply use >>.

@echo off
echo Something else happened >> my_log.log
echo Press any key to quit...
pause > NUL

Great! Now we are actually adding to the log as we go along. The beauty in this is that if the file doesn't already exist it's automatically created (in the same way as when you use the simple > redirect).

So, all is cool here except echo is a pretty boring command. So let's liven things up a little. First we will copy hello.txt to hello1.txt, then we will remove directory hello.txt (of course, we will assume there is no hello.txt directory on your system, at least please make sure you check for this! - this will fail hello.txt not being a directory).

With these more "complex" commands you can distinguish standard output (numbered 1) from error output (numbered 2). So you can actually redirect standard output to one file, and error output to the console for instance.

@echo off
echo Before copy >> my_log.log
copy hello.txt hello1.txt >> my_log.log 2>&1
echo Before rmdir 1 >> my_log.log
rmdir hello.txt >> my_log.log
echo Before rmdir 2 >> my_log.log
rmdir hello.txt >> my_log.log 2>&1
echo Press any key to quit...
pause > NUL

Using this example you can see how adding 2>&1 after the redirect will make sure both standard and error output are redirected to file. Otherwise error output will show on screen (first rmdir).


2. Dynamically choosing where to redirect to

Now, let's say in certain cases you want to redirect output to screen but in other cases (say your batch is being called from within another script) you would rather use a file.

You can do this using CON.

@echo off

:init_vars
set out=CON
if not "%1"=="" set out="%1"
goto do_stuff

:do_stuff
echo Copying hello.txt >> %out%
copy hello.txt hello1.txt >> %out% 2>&1
echo Removing directory hello.txt >> %out%
rmdir hello.txt >> %out% 2>&1
goto eof

:eof
if not "%out%"=="CON" goto blackhole
echo Press any key to quit...
pause > NUL
goto blackhole

:blackhole

If you just launch the batch file, all the output will show up on console (because no %1 argument has been provided).

If you launch the batch via a call as follows, however:

@echo off
redirect5 my_second_log.log

The output will get sent to my_second_log.log. Note that on top of that we can avoid pausing the process 'for nothing' by using the if not "%out%"=="CON" goto blackhole condition in :eof. Neat, don't you think?

I have never actually seen a redirection based on anything else but a "on-a-command" basis i.e. how to redirect output for the whole current process. Have you? If so, drop me a line, I would love to know how to do this, if possible!

Another post will be coming up shortly. Just a few thoughts I would like to share.
Online Marketing
Add blog to our blog directory blog search directory Blog Directory Blogarama - The Blog Directory