James
James

Reputation: 109

How to check the size of a file from command line

I need to be able to check a files filesize, and then do an if statement in a batch file

e.g. if file < 20KB start notepad, if > 20KB start wordpad

Upvotes: 1

Views: 3264

Answers (1)

Anders
Anders

Reputation: 101606

@echo off
SETLOCAL ENABLEEXTENSIONS
if exist "%~f1" (
    if %~z1 GEQ 20480 (
        start "" "%ProgramFiles%\Windows NT\Accessories\wordpad.exe" "%~f1"
    ) else (
        start notepad "%~f1"
    )
)

Edit: The %~z syntax only works for parameters and FOR loops, for a hardcoded name you can use a helper function:

@echo off
SETLOCAL ENABLEEXTENSIONS
goto main

:getfilesize 
set %1=0
if exist "%~f2" set %1=%~z2
@goto :EOF

:main
set myfile=test.txt
call :getfilesize mysize "%myfile%"
if %mysize% GEQ 20480 (
    start "" "%ProgramFiles%\Windows NT\Accessories\wordpad.exe" "%myfile%"
) else (
    start notepad "%myfile%"
)

Upvotes: 2

Related Questions