How to check file size in windows script package?

I want to have a batch file that checks that filesize belongs to a file.

If it is larger than %somany% kbytes, , it should redirect from GOTO to another location.

Example:

 [check for filesize] IF %file% [filesize thing Bigger than] GOTO No echo Great! Your filesize is smaller than %somany% kbytes. pause exit :no echo Um... You have a big filesize. pause exit 
+57
batch-file filesize
Jul 29 '09 at 11:40
source share
13 answers

If the file name is used as the parameter for the batch file, all you need is %~z1 (1 means the first parameter)

If the file name is not a parameter, you can do something like:

 @echo off setlocal set file="test.cmd" set maxbytesize=1000 FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA if %size% LSS %maxbytesize% ( echo.File is ^< %maxbytesize% bytes ) ELSE ( echo.File is ^>= %maxbytesize% bytes ) 
+94
Jul 29 '09 at 12:15
source share

I like @Anders answer because the explanation for the secret sauce is% ~ z1. However, as indicated, this only works when the file name is passed as the first parameter to the batch file.

@Anders circumvented this problem using FOR , which is a great solution to a 1 line problem, but harder to read.

Instead, we can return to a simpler answer with% ~ z1 using CALL . If you have a file name stored in an environment variable, it will become% 1 if you use it as a parameter for a routine in your batch file:

 @echo off setlocal set file=test.cmd set maxbytesize=1000 call :setsize %file% if %size% lss %maxbytesize% ( echo File is less than %maxbytesize% bytes ) else ( echo File is greater than or equal %maxbytesize% bytes ) goto :eof :setsize set size=%~z1 goto :eof 

I was curious about the concern of J. Bouvri regarding 32-bit restrictions. He seems to be talking about a problem using LSS not about the file size logic itself. To deal with J. Bouvrie's problem, I rewrote the solution to use comparison with the added string:

 @echo on setlocal set file=test.cmd set maxbytesize=1000 call :setsize %file% set checksize=00000000000000000000%size% set checkmaxbytesize=00000000000000000000%maxbytesize% if "%checksize:~-20%" lss "%checkmaxbytesize:~-20%" ( echo File is less than %maxbytesize% bytes ) else ( echo File is greater than or equal %maxbytesize% bytes ) goto :eof :setsize set size=%~z1 goto :eof 
+13
Apr 24 '15 at 11:43
source share

%~z1 expands to the size of the first argument in the batch file. Cm

 C:\> call /? 

and

 C:\> if /? 

A simple example:

 @ECHO OFF SET SIZELIMIT=1000 SET FILESIZE=%~z1 IF %FILESIZE% GTR %SIZELIMIT% Goto No ECHO Great! Your filesize is smaller than %SIZELIMIT% kbytes. PAUSE GOTO :EOF :No ECHO Um ... You have a big filesize. PAUSE GOTO :EOF 
+11
Jul 29 '09 at 12:08
source share

I prefer to use the DOS function. I feel cleaner.

 SET SIZELIMIT=1000 CALL :FileSize %1 FileSize IF %FileSize% GTR %SIZELIMIT% Echo Large file GOTO :EOF :FileSize SET %~2=%~z1 GOTO :EOF 
+9
Jun 04 '14 at 18:42
source share

If your %file% is an input parameter, you can use %~zN , where N is the parameter number.

eg. a test.bat containing

 @echo %~z1 

The size of the first parameter will be displayed, so if you use " test myFile.txt ", it will display the size of the corresponding file.

+8
Jul 29 '09 at 12:07
source share

As usual, VBScript is available to you .....

 Set objFS = CreateObject("Scripting.FileSystemObject") Set wshArgs = WScript.Arguments strFile = wshArgs(0) WScript.Echo objFS.GetFile(strFile).Size & " bytes" 

Save as filesize.vbs and enter at the command line:

 C:\test>cscript /nologo filesize.vbs file.txt 79 bytes 

Use a for loop (in batch mode) to get the return result.

+4
Jul 29 '09 at 12:06
source share

Another example

  FOR %I in (file1.txt) do @ECHO %~zI 
+4
May 20 '14 at 6:02
source share

Create a batch file with one line GetFileSize.bat containing

 GetFileSize=%~z1 

then call him

 call GetFileSize myfile.txt if (%GetFileSize) == () goto No_File if (%GetFileSize) == (0) goto No_Data if (%GetFileSize) GTR 1000 goto Too_Much_Data rem Etc. 

You can even create your test file on the fly to eliminate the required file with the inscription, note the double percentage in the echo expression:

 echo set GetFileSize=%%~z1 > %temp%\GetFileSize.bat call %temp%\GetFileSize myfile.txt if (%GetFileSize) GTR 1000 goto Too_Much_Data rem etc 

The last decision is antispaghetti. So cute. However, more is written to disk. Check the amount of use.

+2
Jul 24 '14 at 3:49
source share

Just saw this old question to see if there is something built-in in Windows. ~ z thing is something that I did not know about, but not applicable to me. I ended up with one Perl layer:

 @echo off set yourfile=output.txt set maxsize=10000 perl -e "-s $ENV{yourfile} > $ENV{maxsize} ? exit 1 : exit 0" rem if %errorlevel%. equ 1. goto abort if errorlevel 1 goto abort echo OK! exit /b 0 :abort echo Bad! exit /b 1 
+1
Jul 14 '13 at 20:06
source share

This was my solution for estimating file sizes without using VB / perl / etc. and stick to your own windows shell commands:

 FOR /F "tokens=4 delims= " %%i in ('dir /-C %temp% ^| find /i "filename.txt"') do ( IF %%i GTR 1000000 ( echo filename.txt filesize is greater than 1000000 ) ELSE ( echo filename.txt filesize is less than 1000000 ) ) 

Not the cleanest solution, but it does its job.

+1
Jul 25 '13 at 16:42
source share

It is important to note the INT32 packet limit: 'Invalid number. Numbers are limited by 32-bit precision.'

Try the following statements:

 IF 2147483647 GTR 2147483646 echo A is greater than B (will be TRUE) IF 2147483648 GTR 2147483647 echo A is greater than B (will be FALSE!) 

Any number greater than the maximum value of INT32, BREAK THE SCRIPT! Seeing how file size is measured in bytes, scripts will support a maximum file size of about 255.9999997615814 MB!

0
Jan 26 '15 at 15:07
source share

After several iterations of "try and test", I found a way (still not presented here) to get the file size in a loop variable (and not a command line parameter):

 for %%i in (*.txt) do ( echo %%~z%i ) 
0
Oct 13 '16 at 10:45
source share

Just an idea:

You can get the file size by running the "dir" command:

 >dir thing 

Then so many things come back again.

Perhaps you can get it from there if you are looking for it.

But I'm not sure.

-four
Jul 29 '09 at 11:54
source share



All Articles