Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check the size of a file in a Windows batch script?

I want to have a batch file which checks what the filesize is of a file.

If it is bigger than %somany% kbytes, it should redirect with GOTO to somewhere else.

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 
like image 228
Deniz Zoeteman Avatar asked Jul 29 '09 11:07

Deniz Zoeteman


People also ask

How do I find the exact file size?

Locate the file or folder whose size you would like to view. Click the file or folder. Press Command + I on your keyboard. A window opens and shows the size of the file or folder.

How do I change the size of a window in a batch file?

If you just open the batch file, click on the window, and then click "properties", and then to "layout", and scroll down to "Window Size", you can edit it from there.

How do I find the size of a file in Terminal?

What we need is to open the terminal and type du -sh file name in the prompt. The file size will be listed on the first column. The size will be displayed in Human Readable Format. This means we can see file sizes in Bytes, Kilobytes, Megabytes, Gigabytes, etc.


1 Answers

If the file name is used as a parameter to the batch file, all you need is %~z1 (1 means 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 ) 
like image 163
Anders Avatar answered Sep 21 '22 03:09

Anders