How many Bytes?


TM21

Banned
Local time
8:32 AM
Posts
252
I've written a batch file that cleans out tons of garbage from my C: drive.
At the start of the batch file is a command that counts the number of files on C:
and after the cleanup, the command runs again to count the number of files left after cleaning.
The two numbers are used to calculate the number of files actually deleted. That works great, but,
I'd also like to know how many megabytes were deleted.
I can do that manually, by just checking the size of the Drive, in "Properties" before and after I run the batch file.
I did that today and it works great. But, it would be even neater if I could incorporate that check for drive size into my batch file.
Is that even possible, using either batch or Powershell language?
Either way, it's beyond my level of expertise. Any help would be greatly appreciated.
 
I've written a batch file that cleans out tons of garbage from my C: drive.
At the start of the batch file is a command that counts the number of files on C:
and after the cleanup, the command runs again to count the number of files left after cleaning.
The two numbers are used to calculate the number of files actually deleted. That works great, but,
I'd also like to know how many megabytes were deleted.
I can do that manually, by just checking the size of the Drive, in "Properties" before and after I run the batch file.
I did that today and it works great. But, it would be even neater if I could incorporate that check for drive size into my batch file.
Is that even possible, using either batch or Powershell language?
Either way, it's beyond my level of expertise. Any help would be greatly appreciated.

Use something like

fsutil volume diskFree C:

this tells you free space at beginning

You would probably need to direct output to a text file,

and run command after you haved finished to a second text file.

Then you can read 1st line from each and parse it to set two variables with value before and after.

Then you subtract one from the other.
 

My Computer My Computer

At a glance

Windows 11 Pro + Win11 Canary VM.I9 13th gen i9-13900H 2.60 GHZ16 GB solderedIntegrated Intel Iris XE
OS
Windows 11 Pro + Win11 Canary VM.
Computer type
Laptop
Manufacturer/Model
ASUS Zenbook 14
CPU
I9 13th gen i9-13900H 2.60 GHZ
Motherboard
Yep, Laptop has one.
Memory
16 GB soldered
Graphics Card(s)
Integrated Intel Iris XE
Sound Card
Realtek built in
Monitor(s) Displays
laptop OLED screen
Screen Resolution
2880x1800 touchscreen
Hard Drives
1 TB NVME SSD (only weakness is only one slot)
PSU
Internal + 65W thunderbolt USB4 charger
Case
Yep, got one
Cooling
Stella Artois (UK pint cans - 568 ml) - extra cost.
Keyboard
Built in UK keybd
Mouse
Bluetooth , wireless dongled, wired
Internet Speed
900 mbs (ethernet), wifi 6 typical 350-450 mb/s both up and down
Browser
Edge
Antivirus
Defender
Other Info
TPM 2.0, 2xUSB4 thunderbolt, 1xUsb3 (usb a), 1xUsb-c, hdmi out, 3.5 mm audio out/in combo, ASUS backlit trackpad (inc. switchable number pad)

Macrium Reflect Home V8
Office 365 Family (6 users each 1TB onedrive space)
Hyper-V (a vm runs almost as fast as my older laptop)
Thank you, but more steps than I really want.
I'd like some command that would fit inside of my Batch File, and do everything without any action on my part.
That's the beauty of a Batch File, it automates a job, with NO user intervention required.

In reality, the command would have to run twice, once at the start and once more at the end. Just like my file counting command.
On each run, the results would be entered into a variable. At the end, the ending variable would be subtracted from the beginning variable to give me the total Megabytes deleted.

It's not critical for the program to do what it's designed to do. It's just a curiosity for me to get a Byte count.

Someone else had asked me just how many Bytes of HD space my program was freeing up, and I couldn't tell them. I could only get a file count.

I can get a Byte count by just looking at the Properties of C: before and after I run my batch file. But it would just be so neat if I could get my batch file to give me that information.
 
Copy this line before and after clean in bat file:
for /f "Tokens=2 delims=()" %%a in ('fsutil volume diskfree C: ^| find "Total free bytes"') do echo Before = %%a
Clean up code
for /f "Tokens=2 delims=()" %%a in ('fsutil volume diskfree C: ^| find "Total free bytes"') do echo After = %%a
 

My Computers My Computers

  • At a glance

    Windows 11 Pro 25H2, Linux Mint 21.3Intel Core i9-14900K @5.8GHZ64GB (2XG Skill F5-6400J3239G32G) @6800Radeon (TM) RX 480 Graphics (8 GB)
    OS
    Windows 11 Pro 25H2, Linux Mint 21.3
    Computer type
    PC/Desktop
    Manufacturer/Model
    Self Build Feb.2024
    CPU
    Intel Core i9-14900K @5.8GHZ
    Motherboard
    MSI MAG Z790 Tomahawk Max WiFi 7
    Memory
    64GB (2XG Skill F5-6400J3239G32G) @6800
    Graphics Card(s)
    Radeon (TM) RX 480 Graphics (8 GB)
    Sound Card
    Intergrated Realtek
    Monitor(s) Displays
    Samsung
    Screen Resolution
    3840x2160
    Hard Drives
    1 - Crucial 705 1TB GEN5
    1 - Crucial 705 2TB GEN5
    2 - Crucial T500 2TB Gen4
    PSU
    750W EVGA-G3
    Case
    Antec NX410
    Cooling
    H2O Thermalright
    Keyboard
    Logitech K800
    Mouse
    Logitech Master 2S
    Internet Speed
    600 Mbps
    Browser
    Chrome
    Antivirus
    WD
  • At a glance

    Windows 11 Pro & Linux Mint X64Intel(R) Core(TM) i7-3770K CPU @ 4.20 GHz32.0GB Dual-Channel CMY16GX3M2A1600C9NVIDIA GeForce GT 740
    Operating System
    Windows 11 Pro & Linux Mint X64
    Computer type
    PC/Desktop
    Manufacturer/Model
    MSI Z77 MPower (MS-7751)
    CPU
    Intel(R) Core(TM) i7-3770K CPU @ 4.20 GHz
    Motherboard
    Z77 MPower (MS-7751)
    Memory
    32.0GB Dual-Channel CMY16GX3M2A1600C9
    Graphics card(s)
    NVIDIA GeForce GT 740
    Monitor(s) Displays
    40" Samsung
    Screen Resolution
    1920X1080
    Hard Drives
    WIN10 - 111GB Samsung SSD 840 Series
    WIN11 - 223GB Crucial CT240BX500SSD
    2X931GB Crucial CT1000BX500SSD1
    PSU
    Antec 850W
    Case
    Antec 900
    Cooling
    H20
    Keyboard
    Logitech K800 Wireless
    Mouse
    Logitech MX Master 2S
    Internet Speed
    100 Mbps
    Browser
    Chrome
    Antivirus
    Windows Defender
CMD's "set /a" stops working on any byte counts > 4 GB (2^32).

You have to use PowerShell or another language to subtract the two byte counts. Since I didn't have any actual cleanup functions, cleanup is simulated here by using a fsutil command to create a large file and then delete it.

Code:
@echo off
setlocal EnableDelayedExpansion

REM Simulate cleanup by first creating a large DUMMY FILE
fsutil file createnew test 123456789 > NUL
REM

for /f "Tokens=5 delims=() " %%a in ('fsutil volume diskfree C: ^| find "Total free bytes"') do (
    set bytes=%%a
    set before_bytes=!bytes:,=!
)

REM Free up disk space by removing DUMMY FILE
del test
REM

for /f "Tokens=5 delims=() " %%a in ('fsutil volume diskfree C: ^| find "Total free bytes"') do (
    set bytes=%%a
    set after_bytes=!bytes:,=!
)

REM Number format is padded to 12 digits wide
powershell -nop "$Saved = $env:after_bytes - $env:before_bytes; '{0,12} bytes saved' -f $Saved; '{0,12} GB saved' -f ('{0:N2}' -f ($Saved/1GB))"
endlocal
 

My Computer My Computer

At a glance

Windows 7
OS
Windows 7
Powerhsell to give you total size and the freespace. This is returning the C drive on my laptop.


Get-WmiObject -Class Win32_LogicalDisk -ComputerName LOCALHOST | ? {$. DriveType -eq 3} | select DeviceID, {[int]($.Size /1MB)}, {[int]($_.FreeSpace /1MB)}

1683571213159.png
 

My Computers My Computers

  • At a glance

    Windows 11 ProIntel Core i5-8279u32GB DDR4 2666MhzIntel Iris Plus 655
    OS
    Windows 11 Pro
    Computer type
    PC/Desktop
    Manufacturer/Model
    Beelink SEI8
    CPU
    Intel Core i5-8279u
    Motherboard
    AZW SEI
    Memory
    32GB DDR4 2666Mhz
    Graphics Card(s)
    Intel Iris Plus 655
    Sound Card
    Intel SST
    Monitor(s) Displays
    Asus ProArt PA278QV
    Screen Resolution
    2560x1440
    Hard Drives
    512GB NVMe
    PSU
    NA
    Case
    NA
    Cooling
    NA
    Keyboard
    NA
    Mouse
    NA
    Internet Speed
    500/50
    Browser
    Edge
    Antivirus
    Defender
    Other Info
    Mini PC used for testing Windows 11.
  • At a glance

    Windows 10 ProRyzen 9 5900x64GB DDR4-3600EVGA GeForce 3080 FT3 Ultra
    Operating System
    Windows 10 Pro
    Computer type
    PC/Desktop
    Manufacturer/Model
    Custom
    CPU
    Ryzen 9 5900x
    Motherboard
    Asus Rog Strix X570-E Gaming
    Memory
    64GB DDR4-3600
    Graphics card(s)
    EVGA GeForce 3080 FT3 Ultra
    Sound Card
    Onboard
    Monitor(s) Displays
    ASUS TUF Gaming VG27AQ. ASUS ProArt Display PA278QV 27” WQHD
    Screen Resolution
    2560x1440
    Hard Drives
    2TB WD SN850 PCI-E Gen 4 NVMe
    2TB Sandisk Ultra 2.5" SATA SSD
    PSU
    Seasonic Focus 850
    Case
    Fractal Meshify S2 in White
    Cooling
    Dark Rock Pro CPU cooler, 3 x 140mm case fans
    Keyboard
    Corsiar K65 RGB Lux
    Mouse
    Logitech G9 Laser Mouse
    Internet Speed
    500/50
    Browser
    Chrome
    Antivirus
    Defender.
Copy this line before and after clean in bat file:
for /f "Tokens=2 delims=()" %%a in ('fsutil volume diskfree C: ^| find "Total free bytes"') do echo Before = %%a
Clean up code
for /f "Tokens=2 delims=()" %%a in ('fsutil volume diskfree C: ^| find "Total free bytes"') do echo After = %%a
OK, I ran these lines in a Test batch file, with some cleaning lines in between them.
there was no line to subtract the second result from the first result, thus giving me the net gain.
So I suptracted the second result from the first one (pencil & paper) and got a positive .1 GB.
It was showing me more MB of files After Cleaning than Before. Not what I was after!!!
It was actually showing me the increase in Free Space, or my net gain.


Maybe the problem is that the code was showing me Free Space and not occupied space. Eh?
I'm getting more and more certain that was the case, because the numbers I got from those lines of code, were way higher than the amount of data I have on that drive.
On closer examination, I notice the phrase "diskfree"in the code which I'm thinking means Free Space.
So, how can I change that to show Occupied Space and not Free Space?

Each time the count line of code is run, it must place the result into a variable. First time variable A and the second time variable B. Then another line to do the math: Variable A - Variable B = C print C

Something like this from my batch file:

set /a deleted=%Count1% - %Count2%
Echo:
Echo File Count before cleaning: %Count1% files
Echo:
Echo File count after cleaning: %Count2% files
Echo:
Echo Files deleted: %deleted% files




One thing this definitely shows me, is my own lack of programming expertise.
 
Last edited:
Here is how to get it in powershell (I also include formatting to two decimal places) from my nightly run script

Code:
$cdrv = Get-CimInstance -Class Win32_logicaldisk -Filter "DeviceID = 'C:'"
$used = ($cdrv.Size - $cdrv.FreeSpace)/1000000000
$free= $cdrv.Freespace /1000000000
$u=($used).tostring("#.##")
$f=($free).tostring("#.##")
Write-Host C:  $u GB used, $f GB free

Here is the .bat snippet that shows how I run a Daily.ps1 powershell script from batch that will display all the powershell output in log.txt

Code:
@ECHO OFF

(

ECHO.

pwsh -ExecutionPolicy Unrestricted  -NoLogo -NonInteractive -WindowStyle Hidden -File  E:\Gene\Run-Daily\Daily.ps1

) > log.txt 2>&1
 
Last edited:

My Computers My Computers

  • At a glance

    Windows 11 Pro x64i9 14900K P/E 5.8/4.5 GHz, cache 5.0 GHz96GB (2x48) G.skill Ripjaws 6800 MT/sAsus ROG Strix 4070 Ti OC
    OS
    Windows 11 Pro x64
    Computer type
    PC/Desktop
    Manufacturer/Model
    📷🔈🎧 🪛 DIY Photoshop/Audio/Game/tinker
    CPU
    i9 14900K P/E 5.8/4.5 GHz, cache 5.0 GHz
    Motherboard
    Asus ROG Maximus Z790 Dark Hero
    Memory
    🐏 96GB (2x48) G.skill Ripjaws 6800 MT/s
    Graphics Card(s)
    Asus ROG Strix 4070 Ti OC
    Sound Card
    🔊Bowers & Wilkins 606 S3 speakers; Audiolabs 7000a integrated amp; RSL 10S Mk2 sub; Creative Pebble Pro Minimilist
    Monitor(s) Displays
    🖥️🖥️ Eizo CG2730 ColorEdge, ViewSonic VP2768
    Screen Resolution
    🖥️🖥️ 2560 x 1440p x 2
    Hard Drives
    💾 WDC SN850X 4TB nvme, SN850 1TB nvme, SK-Hynix 2 TB P41 nvme,. Sabrent USB-C DS-SC5B 5-bay docking station: 6TB WDC Black, 6TB Ironwolf Pro; 2x 2TB WDC Black HDD
    PSU
    ⚡️ 850W Seasonic Vertex PX-850 ATX 3.0/PCI-E 5.0
    Case
    Fractal Design North XL Mesh, Black Walnut
    Cooling
    ❄️ EK Nucleus black 360 AIO w/Phanteks T30-120 fans, 2 Noctua NF-A14 Chromax case fan, 1 T30-120 fan cooling memory
    Keyboard
    ⌨️ Keychron Q3 Max TKL with custom GMK Redsuns Red Samuri keycaps, TX Stabs
    Mouse
    🖱️ Logitech G305 wireless gaming
    Internet Speed
    ⬇️ 500 Mb/s ⬆️ 12 Mb/s
    Browser
    🔥🦊 Firefox
    Antivirus
    🦺 Defender, Macrium Reflect X 🏆
    Other Info
    Phangkey Amaterasu V2 Desk Mat
  • At a glance

    Apple M1
    Computer type
    Laptop
    Manufacturer/Model
    💻 Apple 13" Macbook Pro 2020 (m1)
    CPU
    Apple M1
    Screen Resolution
    2560x1600
    Browser
    Firefox
One godzillabyte...


GODZILLAbyte.png
 

My Computers My Computers

  • At a glance

    Win 11 Home ♦♦♦26200.8655 ♦♦♦♦♦♦♦25H2AMD Ryzen 7 3700XG.Skill (F4-3200C14D-16GTZKW)EVGA RTX 2070 (08G-P4-2171-KR)
    OS
    Win 11 Home ♦♦♦26200.8655 ♦♦♦♦♦♦♦25H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Built by Ghot® [May 2020]
    CPU
    AMD Ryzen 7 3700X
    Motherboard
    Asus Pro WS X570-ACE (BIOS 5302)
    Memory
    G.Skill (F4-3200C14D-16GTZKW)
    Graphics Card(s)
    EVGA RTX 2070 (08G-P4-2171-KR)
    Sound Card
    Realtek ALC1220P / ALC S1220A
    Monitor(s) Displays
    Dell U3011 30"
    Screen Resolution
    2560 x 1600
    Hard Drives
    2x Samsung 860 EVO 500GB,
    WD 4TB Black FZBX - SATA III,
    WD 8TB Black FZBX - SATA III,
    DRW-24B1ST CD/DVD Burner
    PSU
    PC Power & Cooling 750W Quad EPS12V
    Case
    Cooler Master ATCS 840 Tower
    Cooling
    CM Hyper 212 EVO (push/pull)
    Keyboard
    Ducky DK9008 Shine II Blue LED
    Mouse
    Logitech Optical M-100
    Internet Speed
    300/300
    Browser
    Firefox (latest)
    Antivirus
    Bitdefender Total Security
    Other Info
    Speakers: Klipsch Pro Media 2.1
  • At a glance

    Windows XP Pro 32bit w/SP3AMD Athlon 64 X2 5000+ (OC'd @ 3.2Ghz)TWIN2X2048-6400C4DHX (2 x 1GB, DDR2 800)EVGA 256-P2-N758-TR GeForce 8600GT SSC
    Operating System
    Windows XP Pro 32bit w/SP3
    Computer type
    PC/Desktop
    Manufacturer/Model
    Built by Ghot® (not in use)
    CPU
    AMD Athlon 64 X2 5000+ (OC'd @ 3.2Ghz)
    Motherboard
    ASUS M2N32-SLI Deluxe Wireless Edition
    Memory
    TWIN2X2048-6400C4DHX (2 x 1GB, DDR2 800)
    Graphics card(s)
    EVGA 256-P2-N758-TR GeForce 8600GT SSC
    Sound Card
    Onboard
    Monitor(s) Displays
    ViewSonic G90FB Black 19" Professional (CRT)
    Screen Resolution
    up to 2048 x 1536
    Hard Drives
    WD 36GB 10,000rpm Raptor SATA
    Seagate 80GB 7200rpm SATA
    Lite-On LTR-52246S CD/RW
    Lite-On LH-18A1P CD/DVD Burner
    PSU
    PC Power & Cooling Silencer 750 Quad EPS12V
    Case
    Generic Beige case, 80mm fans
    Cooling
    ZALMAN 9500A 92mm CPU Cooler
    Keyboard
    Logitech Classic Keybooard 200
    Mouse
    Logitech Optical M-BT96a
    Internet Speed
    300/300
    Browser
    Firefox 3.x ??
    Antivirus
    Symantec (Norton)
    Other Info
    Still assembled, still runs. Haven't turned it on for 15 years?
Back
Top Bottom