Sunday 6 July 2008

Batch programming tip#06: Getting the batch file path

Ok. Well, it's been a while since I posted anything to do with actual programming, and I have missed it. So here goes with the next batch programming trick planned: how to retrieve the batch file path. That is: the path where the batch file being run is located.

This is a dead easy variable: %~dp0. We'll get back to what this sequence actually means in a later post but you will notice how much it looks like the argument 0 (%0 or %~0 in this case) I mentioned in an earlier post. It's just had a couple of characters added in (dp).

So, now for a simple echo example:


@echo off

:action
echo Current batch run path: %~dp0
goto eof

:eof
echo Press a key to quit...
pause > NUL
goto blackhole

:blackhole

This looks too simple to actually be of any use but actually using the current run path can be very handy if you store configuration or content files in the same directory as your batch file (or a subdirectory thereof)!

More about that soon.

Thoughts?

No comments:

Online Marketing
Add blog to our blog directory blog search directory Blog Directory Blogarama - The Blog Directory