Showing posts with label file size. Show all posts
Showing posts with label file size. 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!

Saturday, 12 July 2008

Batch tip#07 part 2: comparing files by size

Now we have seen how to retrieve the length of a file passed as argument, let's see how we can use this.

The code below does the following:

1. Command-line check
The process checks for command-line arguments. It expects two existing files otherwise shows an error.
To check whether a file exists, simply use if exist %file%.

2. Compare sizes
The batch retrieves and compares argument file sizes. To do this we use %~z1 and %~z2 which return the file sizes of command line arguments 1 and 2 respectively.

3. Take action
Finally, it outputs an adequate message. This is obviously the bit where you should include appropriate actions.

The code


@echo off

:param_check
if "" == "%1" goto invalid_call
if "" == "%2" goto invalid_call
goto file_one

:file_one
if exist %1 goto file_two
set file_path=%1
goto show_error

:file_two
if exist %2 goto run_content
set file_path=%2
goto show_error

:run_content
if %~z1==%~z2 goto same_size
if %~z1 LSS %~z2 goto first_smaller
goto second_smaller

:same_size
echo Both files are of same size.
goto eof

:first_smaller
echo %1 is smaller than %2
goto eof

:second_smaller
echo %2 is smaller than %1
goto eof

:show_error
echo File not found: "%file_path%"
goto eof

:invalid_call
echo Please call with the following parameters:
echo [1] the first file name
echo [2] the second file name
goto eof

:eof
echo Press any key to close window...
pause > nul
goto blackhole

:blackhole


To run this, simply use a second .bat file containing something like:

@echo off
call comp_files file1.txt file2.txt

Where file1.txt and file2.txt are replaced with the relative or absolute paths of the files your are comparing.

Simple as that!

Thoughts?

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