09stephenb
09stephenb

Reputation: 9816

How to record how much data is deleted in a batch file

I have the following batch file

@echo off
Del *.tmp /s /Q
Del *.temp /s /Q
Del Thumbs.db /s /Q
pause

I need it to record how much data it has deleted in the following formats

How much in data (KB)

How many files it has deleted

Does any one know a code I could use?

Upvotes: 1

Views: 144

Answers (2)

Dale
Dale

Reputation: 1901

Try this:

@echo off
setLocal enableDelayedExpansion

set filePattern=*.temp

set totalSize=0
for /F "tokens=* usebackq" %%a IN (`dir /b ^| findstr /R .!filePattern!$`) DO (
    set size=%%~za
    set /a totalSize=!totalSize! + !size!
)

del !filePattern! /s /Q

set /a totalSize=!totalSize! / 1024
echo Total size deleted (!filePattern!): !totalSize! KB
pause

You may have to change the !filePattern! accordingly based on your desired files to be deleted. Besides that, you may have to place this batch script on the same directory as the files to be deleted.

Upvotes: 1

npocmaka
npocmaka

Reputation: 57272

@echo info about deleted files:
@for /f "delims=" %%a in ('dir /s /-c /a-s *.tmp^| findstr /i "File(s) Directory"') do (
   @echo  %%a
)
@del *.tmp /s /Q

Upvotes: 2

Related Questions