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?
No comments:
Post a Comment