Counting all files from Batch File


TechnoMage2021

Retired PC Builder/Tech
Power User
VIP
Local time
10:11 PM
Posts
1,001
Location
Central Florida, USA
OS
Win-11/Pro/64, Optimum 11 V3, 23H2 22631.3085
For several years, I have been using the following commands (2) to count the files on my C: drive, before and after I delete temp and other junk files.
Sometimes, the difference between the two file counts, does not equal the number of files I actually see being deleted. So I can only conclude that the file counters are not actually counting ALL the files on the HD. Some files are missing! ??? Maybe some of the very files I want to delete.

Here are the two lines of code that do the file counting.
First Count, Before Cleaning:
for /f "delims=" %%A in ( ' dir /a /s "C:\" ^| find /c /v "" ' ) do set Count1=%%A

Second Count, After Cleaning:
for /f "delims=" %%A in ( ' dir /a /s "C:\" ^| find /c /v "" ' ) do set Count2=%%A

Then at the end of the program, Count 2 is deducted from Count 1 and the difference is printed as "Files Deleted".

So my question: Can these commands be revised to that they will indeed count every file on the PC's Hard Drive. ( the C: partition)
Or, do I need an entirely new command, possibly in "Power Shell" ?

Thank you for your consideration.
TechnoMage :cool:
 

My Computer

System One

  • OS
    Win-11/Pro/64, Optimum 11 V3, 23H2 22631.3085
    Computer type
    PC/Desktop
    Manufacturer/Model
    Home Made w/Gigabyte mobo/DX-10
    CPU
    AMD FX 6350 Six Core
    Motherboard
    Gigabyte, DX-10, GA-78LMT-USB3
    Memory
    Crucial, 16 GB
    Graphics Card(s)
    Onboard, ATI Radeon HD 3000; NVIDEA GeForce 210, 1GB DDR3 Ram.
    Sound Card
    Onboard
    Monitor(s) Displays
    24" Acer
    Screen Resolution
    1280x800
    Hard Drives
    Crucial SSD 500GB, SanDisk 126GB SSD, Toshiba 1TB HD
    PSU
    EVGA 500 W.
    Case
    Pac Man, Mid Tower
    Cooling
    AMD/OEM
    Keyboard
    101 key, Backlit/ Mechanical Switches/
    Mouse
    Logitech USB Wireless M310
    Internet Speed
    Hughes Net speed varies with the weather
    Browser
    Firefox 64x
    Antivirus
    Windows Defender, Super Anti Spyware
    Other Info
    Given to me as DEAD, and irreparable.
    Rebuilt with Gigabyte mobo, AMD cpu, 16GB ram and 500GB Crucial SSD.
maybe try

dir /b /a-d /s
 

My Computers

System One System Two

  • OS
    Win7
    Computer type
    PC/Desktop
    CPU
    i5-8400
    Motherboard
    gigabyte b365m ds3h
    Memory
    2x8gb 3200mhz
    Monitor(s) Displays
    benq gw2480
    PSU
    bequiet pure power 11 400CM
    Cooling
    cryorig m9i
  • Operating System
    win7
    Computer type
    PC/Desktop
    CPU
    pentium g5400
    Motherboard
    gigabyte b365m ds3h
    Memory
    1x8gb 2400
    PSU
    xfx pro 450
well, I tried it, and it went through a lot of files, printing them on the screen, but
every line started with "Windows\System32\ and there was no count. ???


The code that I posted, actually COUNTED every file in the HD (presumably) and ran up a count and added it to Count1=%%A
and never showed the files as they were being counted. It's actually a pretty complex bunch of code. (not mine, unfortunately)

On the second run, it counted files again, and added the count to Count2=%%A
Then the program subtracts Count2 from Count1 to give the "Files Deleted".
 

My Computer

System One

  • OS
    Win-11/Pro/64, Optimum 11 V3, 23H2 22631.3085
    Computer type
    PC/Desktop
    Manufacturer/Model
    Home Made w/Gigabyte mobo/DX-10
    CPU
    AMD FX 6350 Six Core
    Motherboard
    Gigabyte, DX-10, GA-78LMT-USB3
    Memory
    Crucial, 16 GB
    Graphics Card(s)
    Onboard, ATI Radeon HD 3000; NVIDEA GeForce 210, 1GB DDR3 Ram.
    Sound Card
    Onboard
    Monitor(s) Displays
    24" Acer
    Screen Resolution
    1280x800
    Hard Drives
    Crucial SSD 500GB, SanDisk 126GB SSD, Toshiba 1TB HD
    PSU
    EVGA 500 W.
    Case
    Pac Man, Mid Tower
    Cooling
    AMD/OEM
    Keyboard
    101 key, Backlit/ Mechanical Switches/
    Mouse
    Logitech USB Wireless M310
    Internet Speed
    Hughes Net speed varies with the weather
    Browser
    Firefox 64x
    Antivirus
    Windows Defender, Super Anti Spyware
    Other Info
    Given to me as DEAD, and irreparable.
    Rebuilt with Gigabyte mobo, AMD cpu, 16GB ram and 500GB Crucial SSD.
I meant use dir /b /a-d /s instead of dir /a /s

something like this:
for /f "delims=" %%A in ( ' dir "c:\" /b /a-d /s ^| find /c /v "" ' ) do set Count1=%%A

I dont think quotes aound "c:\" is needed just c:\


echo-count.jpg
 
Last edited:

My Computers

System One System Two

  • OS
    Win7
    Computer type
    PC/Desktop
    CPU
    i5-8400
    Motherboard
    gigabyte b365m ds3h
    Memory
    2x8gb 3200mhz
    Monitor(s) Displays
    benq gw2480
    PSU
    bequiet pure power 11 400CM
    Cooling
    cryorig m9i
  • Operating System
    win7
    Computer type
    PC/Desktop
    CPU
    pentium g5400
    Motherboard
    gigabyte b365m ds3h
    Memory
    1x8gb 2400
    PSU
    xfx pro 450
I appreciate your help.
I tried that line of yours in a little test Batch file and this was the result, after several minutes, of just a flashing cursor on the screen.
(that is all I wanted to see) The file count went into a variable, which I then printed out, below.

"File Count before cleaning: 494728 files"

So I'd have to say, "VOILA! It Worked!"

Update:
I've not run my "Cleanup.bat" program for two days, and when I first ran it this evening, here was the final report:

Clean 3-1-23.jpg Yikes! 261K+ in just a couple of days.
But I didn't see any .tmp, .old, or .bak files, so I checked the code in the batch file.
I found that somehow I had forgotten the three lines of code that delete those files, so I added those three lines and ran the
batch file again.
This time I got this, after the above mentioned files were deleted:


Clean 3-1-23.2.jpg
And, that was just the three file types, that I mentioned earlier.
I'm not sure why, but I notice NVIDEA is creating a lot of junk files.
Well, I'm not giving up my war on junk files.
I'll NUKE the little buggers.

Thanks for all the participation in this topic.
TechnoMage :cool:
 

My Computer

System One

  • OS
    Win-11/Pro/64, Optimum 11 V3, 23H2 22631.3085
    Computer type
    PC/Desktop
    Manufacturer/Model
    Home Made w/Gigabyte mobo/DX-10
    CPU
    AMD FX 6350 Six Core
    Motherboard
    Gigabyte, DX-10, GA-78LMT-USB3
    Memory
    Crucial, 16 GB
    Graphics Card(s)
    Onboard, ATI Radeon HD 3000; NVIDEA GeForce 210, 1GB DDR3 Ram.
    Sound Card
    Onboard
    Monitor(s) Displays
    24" Acer
    Screen Resolution
    1280x800
    Hard Drives
    Crucial SSD 500GB, SanDisk 126GB SSD, Toshiba 1TB HD
    PSU
    EVGA 500 W.
    Case
    Pac Man, Mid Tower
    Cooling
    AMD/OEM
    Keyboard
    101 key, Backlit/ Mechanical Switches/
    Mouse
    Logitech USB Wireless M310
    Internet Speed
    Hughes Net speed varies with the weather
    Browser
    Firefox 64x
    Antivirus
    Windows Defender, Super Anti Spyware
    Other Info
    Given to me as DEAD, and irreparable.
    Rebuilt with Gigabyte mobo, AMD cpu, 16GB ram and 500GB Crucial SSD.
I assume there's some reason for not just using Disk cleanup.
Disk Cleanup - ElevenForumTutorials

In Windows 10, many of us automated its use but some had problems with doing so.
CleanMgr setup, run - my short ditty #9 - TenForums

This tool is a replacement for the built-in Disk cleanup. I don't know why its author thinks Disk cleanup is going to be removed.
Cleanmgr+
You are using an unofficial version of Cleanmgr+ - TenForums
CleanmgrPlus - GitHub
Cleanmgr+ - Mirinsoft
Cleanmgr+ v1.50.1300 - contains download link


All the best,
Denis
 

My Computer

System One

  • OS
    Windows 11 Home x64 Version 23H2 Build 22631.3296
I wonder how aggressive you can be doing disk cleaning, how do you know when to do it, how do you know when to stop? To use a comparison, you can only do so much ear cleaning before you do more harm than good :eek1:
 

My Computer

System One

  • OS
    Windows 10 Pro
Well, fellers, I didn't just decide to do this, last week. I've been at it in one form or another for 40+ years.
In all that time, can you actually believe that I've not use Disk Cleanup? Even Extended Disk Cleanup.???

All the way back to DOS days (pre-windows) there have been programs that were just plain "Messy".
And it always ticked me off, that people write programs that create a mess and then don't take the extra steps to clean it up.
Just too much trouble, I guess.

Then there are the temp files created by programs, like Firefox, that don't look like temp files. They don't come with the .tmp extensions to make them easy to spot. Most so-called Cleanup programs won't remove them.
Then Edge, for instance, creates a bunch of .old files. They basically go UN-noticed.
I search out spots in the HD where programs dump their junk, and then I do a mass delete on those spots.

Then Microsoft protects some of it's pet projects, like the ever accumulating "Prefetch" files. To delete those files, one must first take ownership of the "Prefetch" folder. It's much simpler to just disable Prefetch all together. I did both.

My Cleanup batch file is not and never can be the fix all to end all, but just maybe my efforts will encourage some others to go after the junk in their own computers.

Create your own batch file and start it off with just a few lines,and then build on that as you find more and more places that need a good cleaning.

Start Here: (put the following lines, in a new text file in Wordpad, and then save it, and change the .txt extension to .bat, then you can click on it and run it.)


del /s /f /q C:\*.tmp
del /s /f /q C:\*.bac
del /s /f /q C:\*.old

Pause


The pause at the end, will keep the window open on the screen, so you can see just what has been removed.

In my own batch file, I've even added lines to empty the Recycle Bins from my two hard drive partitions.
And, I do a file count on the HD, before and after cleaning, and print out the difference on the screen.

For an old, retired, computer tech, this has been a lot of fun.

You probably have programs on your own PC that generate garbage files, that I don't have, so your own Cleanup.bat program would look different than mine.

I've personally run just about every cleanup program that's out there in the public domain, and I have yet to find one that gets everything.
The closest I've come to the Perfect Utility is the "Windows Utilities" (WinUtil.exe) It not only cleans out junk, it also cleans out the registry, and can even Un-Install programs. And, so much more. But what it leaves, is what I go after with my Batch File.

Cheers Mates!

TM :cool:

That's why I decided to write my own, for my computer.
 

My Computer

System One

  • OS
    Win-11/Pro/64, Optimum 11 V3, 23H2 22631.3085
    Computer type
    PC/Desktop
    Manufacturer/Model
    Home Made w/Gigabyte mobo/DX-10
    CPU
    AMD FX 6350 Six Core
    Motherboard
    Gigabyte, DX-10, GA-78LMT-USB3
    Memory
    Crucial, 16 GB
    Graphics Card(s)
    Onboard, ATI Radeon HD 3000; NVIDEA GeForce 210, 1GB DDR3 Ram.
    Sound Card
    Onboard
    Monitor(s) Displays
    24" Acer
    Screen Resolution
    1280x800
    Hard Drives
    Crucial SSD 500GB, SanDisk 126GB SSD, Toshiba 1TB HD
    PSU
    EVGA 500 W.
    Case
    Pac Man, Mid Tower
    Cooling
    AMD/OEM
    Keyboard
    101 key, Backlit/ Mechanical Switches/
    Mouse
    Logitech USB Wireless M310
    Internet Speed
    Hughes Net speed varies with the weather
    Browser
    Firefox 64x
    Antivirus
    Windows Defender, Super Anti Spyware
    Other Info
    Given to me as DEAD, and irreparable.
    Rebuilt with Gigabyte mobo, AMD cpu, 16GB ram and 500GB Crucial SSD.

Latest Support Threads

Back
Top Bottom