Showing posts with label file path. Show all posts
Showing posts with label file path. Show all posts

Tuesday, 29 July 2008

Batch programming tip#15: Getting file information

This example speaks for itself I think. You can obviously also use it in FOR loops or subroutines. It show how to retrieve lots of useful information about files (in this exemple the first argument).

[info.bat]

@echo off

:init_vars
if [%1]==[] goto what_file
if not exist %1 goto what_file
goto show_info

:show_info
echo.
echo General information
echo -------------------
echo Fully qualified name (%%~f1): %~f1
echo Drive letter (%%~d1): %~d1
echo Path (%%~p1): %~p1
echo File name (%%~n1): %~n1
echo File extension (%%~x1): %~x1
echo Date time (%%~t1): %~t1
echo Size (%%~z1): %~z1
echo File attributes (%%~a1): %~a1
goto show_combinations

:show_combinations
echo.
echo Some possible combinations
echo --------------------------
echo Drive and path (%%~dp1): %~dp1
echo Filename and extension (%%~nx1): %~nx1
echo Path using short names (%%~fs1) : %~fs1
echo.
goto eof

:what_file
echo Please provide valid file.
goto eof

:eof
echo Press any key to quit
pause > NUL


Simply call it using something like:

[info_call.bat]

@echo off
call info test_file_info.longext


These allow you to get: file size, date time, etc. Really useful stuff =D.

Have fun!

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?
Online Marketing
Add blog to our blog directory blog search directory Blog Directory Blogarama - The Blog Directory