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

2
  • possible duplicate of Windows command for file size only? Commented May 12, 2011 at 16:31
  • @Michael Burr No, this requires if statements as well Commented May 12, 2011 at 16:37

1 Answer 1

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%" ) 
Sign up to request clarification or add additional context in comments.

2 Comments

I would like to enter it e.g. it checks a predefined file to see if its 20kb or not
@James: I just used %1 (The first parameter) in the sample code (Yourbatch.cmd yourfile.txt)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.