views:

3041

answers:

5

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
A: 

Just an idea:

You may get the filesize by running command "dir":

>dir thing

Then again it returns so many things.

Maybe you can get it from there if you look for it.

But I am not sure.

ufukgun
A: 

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

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 on the command-line:

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

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

ghostdog74
+1  A: 

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

E.g. a test.bat containing

@echo %~z1

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

HerdplattenToni
+2  A: 

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

C:\> call /?

and

C:\> if /?

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
Sinan Ünür
+5  A: 

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
)
Anders
thanks! this helped me.
YourComputerHelpZ
Can you explain how the "usebackq" option helps? It doesn't seem to have anything to do with file size, but if I remove it, it stops working
JoelFan
@JoelFan: Without usebackq, the ' quote means command and not string (Run FOR /? for the details) Another alternative (To better deal with spaces in filenames) is to use: FOR /F "tokens=*" %%A IN ("%file%") DO ...
Anders