So been there, done that, time to move on from batch.
But before that, a quick list of all the batch programming scripts already available here:
Variables and spaces
Using CD /D
The Boring Black Hole
Working with arguments, part 1
Working with arguments, part 2
Working with arguments, part 3
Using CON... or not
How to clear the log file
Getting the batch file path
Retrieving argument file size
Comparing files by size
Reading from a file: part 1
Reading from a file, part 2: looping
Reading from a file, part 3: looping, part 2
Using FOR /D
Making and calling a subroutine, part 1
Making and calling a subroutine, part 2
Using The Substring Equivalent
Creating a timestamp
Using setlocal enabledelayedexpansion
The difference between :: and REM
Getting file information
Using help to help yourself (part 1)
Using help to help yourself (part 2): publishing batch help information in HTML format
Showing posts with label comparing file sizes. Show all posts
Showing posts with label comparing file sizes. Show all posts
Friday, 1 August 2008
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
To run this, simply use a second .bat file containing something like:
Where file1.txt and file2.txt are replaced with the relative or absolute paths of the files your are comparing.
Simple as that!
Thoughts?
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?
Labels:
argument,
batch,
comparing file sizes,
file size,
NT,
programming
Subscribe to:
Posts (Atom)