0

I would to do size difference between two folders.

This is my batch code

@echo off
setlocal ENABLEDELAYEDEXPANSION
set firstline=.
set firstline2=.
set secondline=.
set secondline2=.
::GET SIZE FIRST FOLDER
@FOR /F "tokens=1 delims=^^" %%G IN ('dir  /a /s  "C:\Program files\"') DO (
   set firstline=!secondline!
   set secondline=%%G
)
::GET SIZE SECOND FOLDER
@FOR /F "tokens=1 delims=^^" %%G IN ('dir  /a /s  "C:\Temp\"') DO (
   set firstline2=!secondline2!
   set secondline2=%%G
)
echo !firstline!   ---->   8528 File(s)  3,767,627,449 bytes
echo !firstline2!  ---->     16 File(s)     37,620,834 bytes

::DO BYTES DIFFERENCE
set difference = !firstline! - !firstline2!

How can I get only bytes value on "firstline" and "firstline2", so I can do difference?

Thanks

  • 3
    Mmmm... You need to get the appropiate part of such a string, probably with `for /F` command with `tokens=3` switch, and then eliminate the commas with `%var:,=%` replacement. However, if you want to do the difference of numbers greater than 2147483647 (that is the max num that `set /A` command can manage), then you need a method to manage Big Numbers in a Batch file, like [this one](https://stackoverflow.com/a/20715059/778560) – Aacini Oct 06 '21 at 15:15
  • Sorry but I'm new in Batch language, please can you suggest how can I take only bytes value, explain part of code? For difference of Big numbers, this isn't a problem because I can transform Bytes in MegaBytes (n/1024) or GigaBytes (n/1024/1024) – Marco Collarini Oct 07 '21 at 12:45
  • I already gave you a series of hints, so please read the documentation on `for /F` command and "variable replacements", write a Batch file and return here if you have problems with it. How do you intend to transform 3767627449 Bytes in Megabytes? Using `set /A` command? – Aacini Oct 07 '21 at 21:48

1 Answers1

0

The following idea should provide you with the information you require, i.e. the number of bytes difference.

@Echo Off
SetLocal EnableExtensions

Set "First="
Call :DirSize First "C:\Program Files"
If Not Defined First GoTo :EOF

Set "Second="
Call :DirSize Second "C:\Temp"
If Not Defined Second GoTo :EOF

Set /A Diff = First - Second

Echo %Diff:-=%
Pause

GoTo :EOF

:DirSize
For %%G In ("%~2") Do If "%%~aG" GEq "d" For /F "EOL=- Tokens=3,8" %%H In ('
 %SystemRoot%\System32\robocopy.exe "%~2" Null /Bytes /L /NDL /NFL /NJH /S'
) Do If Not "%%I" == "" Set "%~1=%%H"
Exit /B

In the case of the above, the code to retrieve a string containing only the size in bytes of the directory is in the labelled section :DirSize

Please note the above code will not achieve the task as intended, if either directory has a size greater than 2147483647 bytes. As an aside however, should such a scenario be possible, you could make an adjustment to the labelled section:

:DirSize
For %%G In ("%~2") Do If "%%~aG" GEq "d" For /F "EOL=- Tokens=3,8" %%H In ('
 %SystemRoot%\System32\robocopy.exe "%~2" Null /L /NDL /NFL /NJH /S'
) Do If Not "%%I" == "" Set "%~1=%%H"
Exit /B

Removing the /bytes option means that your returned variables will be a potentially floating point string in MB, (likely to up to two decimal places). What you would need to do then is to adjust/split those variables as needed, and modify your arithmetic, (as Set /A does not work with floating point numbers, only integers).

Compo
  • 36,585
  • 5
  • 27
  • 39