Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Is there a way to check the size of a file from a batch file? or at the very least a dir command that will return only file name and file size? I would like to simply check for a file size greater than some number and archive/delete when it grows beyond that point. ANY help would be appreciated
THANKS.

:: 2big.bat CALL with filename.ext
if %~z1 GTR 5000 del %1
:: DONE
If at first you don't succeed, you're about average.M2

I tried to use your solution but failed. It said 5000 was unexpected at this time.
Please help. Thanks.

vidukcom,
Are you using w2k? If so, did you run the bat with a filename to check?
Here's a version with syntax checking:
:: 2big2.bat CALL with filename.ext
@echo off
if %1'==' echo which file? && goto :eof
if not exist %1 echo %1 was not found && goto :eof
if %~z1 GTR 5000 del %1
:: DONE
If at first you don't succeed, you're about average.M2

Dear M2,
No, I am using Windows XP now. Yes, yesterday I change the word filename.ext with my file name, but failed.
Actually what I want to do is not the same with this case, but similar. I want to check whether the file size is 0 or not. If 0, I want to give message to the user and then abort the batch file.
Would you mind please help. Thanks a lot.

Hi,
Try this:
:: chk4zero.bat
@echo off
if %1'==' echo usage is: %0 FILENAME.EXT && goto :eof
if not exist %1 echo %1 was not found && goto :eof
if %~z1 EQU 0 goto :0
goto :eof
:: Put in any code you want to run if the file is not zero length.
::
:0
echo %1 is size zero
:: DONE
If at first you don't succeed, you're about average.M2

Hi,
So sorry again for me being so stupid. I still don't understand.
if %1'==' echo usage is: %0 FILENAME.EXT && goto :eof
About above line, I am confused, how to set the file I want to check as %1. For example, the file name is buj.rar.
Please help again. Thanks..

The %1 means the first command line "parameter".
So in this case you should run this command:
chk4zero buj.rar
If at first you don't succeed, you're about average.M2

Hi M2,
Sorry there is another problem with my batch file. Actually it has worked fine with your script before, but I have to run it like this : chk4zero buj.rar.
Whereas, buj.rar is produced inside the batch file, so there is a process inside.
User will only type the batch file without even know the name of the file produced inside.
So, when user type chk4zero, I want to check inside the batch process whether the file produced is zero or not, if zero, I want to delete it.
My problem now is how to set %1 with the file name inside the batch file.
Please help again. Thanks a lot.
Regards.

Hi,
This will check the size of buj.rar and if it is zero, delete it.
:: chkrar0.bat
@echo off
for %%R in (buj.rar) do if %%~zR equ 0 del buj.rar
:: DONE
If at first you don't succeed, you're about average.M2

Dear M2,
Big thanks. I now have succeeded creating the batch file I need.
Thanks a lot for your help.
Regards.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |