oqrxke
oqrxke

Reputation: 351

How to get only the file size from win cmd "dir"?

Let's say there is a file named app.exe.

dir app.exe gives:

 Volume in drive C has no label.
 Volume Serial Number is CAA5-A19C

 Directory of C:\Users\Lazy\Downloads\batch

02/06/2015  23:50        20.280.135 app.exe
               1 File(s)     20.280.135 bytes
               0 Dir(s)  100.233.252.864 bytes free

From all this I need the file size (20.280.135). Ideally, a file named size.txt containing only "20.280.135", spaces and/or new lines should be removed. The ideia behind is to use that file (size.txt) as input in a LaTeX document.

How to get only the file size from the dir command?

Upvotes: 2

Views: 9713

Answers (3)

MC ND
MC ND

Reputation: 70923

Saved as ex. getFileSize.cmd and called as

getFileSize.cmd "C:\Users\Lazy\Downloads\batch\app.exe" > size.txt

will generate the indicated file with only the dotted file size, with no spaces, carriage returns or line feeds.

@echo off
    setlocal enableextensions disabledelayedexpansion

    rem Locate file to handle
    for %%a in ("%~1") do (
        rem Retrieve file size
        set "size=%%~za" & if not defined size set "size=0"

        rem Include dots in value
        setlocal enabledelayedexpansion
        for /l %%a in (3 4 35) do if not "!size:~0,-%%a!"=="" set "size=!size:~0,-%%a!.!size:~-%%a!"

        rem Output value without new lines
        <nul set /p"=!size!"
        endlocal
    )

Upvotes: 1

Hackoo
Hackoo

Reputation: 18827

Try something like that :

@echo off
Set Size=
Set Log=Size.txt
Set PathApp=C:\Program files\Mozilla Firefox\Firefox.exe
call :filesize "%PathApp%"
echo %Size%
echo %Size% > %Log%
Pause
Start %Log%
::**************************************************************
:: set filesize of 1st argument in %size% variable, and return
::**************************************************************
:filesize
 set size=%~z1
 Exit /b 0
::**************************************************************

Upvotes: 0

Rahul
Rahul

Reputation: 77866

Not exactly using DIR command but you can run the below command from CMD to get the file size like

for %I in (C:\Users\Lazy\Downloads\batch\app.exe) do @echo %~zI

Upvotes: 3

Related Questions