开发者

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


@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%"
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜