1

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

James
  • 109
  • 2
  • 5

1 Answers1

2
@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%"
)
Anders
  • 97,548
  • 12
  • 110
  • 164