Thursday 10 July 2008

Batch programming tip#07: Retrieving argument filesize

Imagine you are using a file as argument (this can also be %0 of course) and you would like to know the file's size in bytes.

This is an example of how to do this:

@echo off

:action
echo File size of currently running batch file: %~z0
goto eof

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

:blackhole

Using %~z0 retrieves the file size of the currently running batch which should be 164 (unless of course you have added in or removed extra line breaks, comments, etc.)

One reason to use file size would be, for instance, you might want a batch to once use a given configuration file, but then use another the next time, and then to switch back to the first the time after that. If these configuration files are definitely of different size, you can use file size to operate the swap.

Voilà!

Thoughts?

No comments:

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