2

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?

oqrxke
  • 331
  • 1
  • 3
  • 12

3 Answers3

3

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
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • Thanks @Rahul! It's possible to have punctuation, 20.280.135 instead of 20280135? And also remove the space and new line at the end? – oqrxke Jun 25 '15 at 00:55
  • I'm having difficulties trying to run this from a batch script file. The output is "~zI was unexpected at this time". – oqrxke Jun 25 '15 at 01:15
  • 1
    Within a batch script, you need to double the `%`s for the `metavariable` (ie. the loop-control variable, `%I` in this case). Try `for %%I in (C:\Users\Lazy\Downloads\batch\app.exe) do echo %%~zI` – Magoo Jun 25 '15 at 06:07
1

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
    )
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • It works fine if I call it from command line (as your suggestion). But If call it from another script it does not work. Is there some trick to call it from another script? – oqrxke Jun 26 '15 at 00:54
  • Please, could you update the script such that file name (size.txt) is hard coded inside the script. Then I'll copy and past this script in mine (I actually call this script from another). – oqrxke Jun 26 '15 at 01:59
  • @oqrxke, to call a batch file from another you need to use the `call` command. So it should be `call "x:\somewhere\getFileSize.cmd" "C:\Users\Lazy\Downloads\batch\app.exe" > "x:\some\place\size.txt"`. If you prefer to hardcode it, replace the `%~1` with the name of the executable and change `"x:\somewhere\size.txt" set /p"=!size!"` – MC ND Jun 26 '15 at 05:49
0

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
::**************************************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70