Deletes files and folders using batch script

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi Guys, Not sure if I can post any batch file related question in this forum. If this is not appropriate, please let me know. Thanks in advance.
In any case, here is my quest looking for resolution if possible. I use this code to delete files and folders but I want to know how many files and folders being deleted. A volunteer suggests a powershell script to count the total files before deletion but my concern is there may be some files denying deletion which make the count result not accurate.

VBA Code:
REM Remove All Files older than 180 days
forfiles /p "%windir%\ABC\Log" /s /m *.* /d -180 /c "cmd /c del /f /q @path"

REM Remove Folders with specified folder name
For /D %%F ("%windir%\ABC\Log\*-*-*") do rd /s /q "%%F"

Here is the PS code suggested from voluteer :

Code:
set-location d:\testing -ErrorAction stop               # Change directory
$files = get-childitem -File                            # Get all the files in this directory

$count = ($files|measure).count                         # ($files|measure).count gives how
                                                        # many files are in this collection.
write-host "There are $($count) files to be deleted."   # Write that to the screen.
$files | remove-item                                    #remove the files
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Is this a network location or at least somewhere you don't have to worry about folks messing around while your scripts run?

If so, then, assuming you're scripts both work, I suppose the easiest way to adapt them to your particular case would be to run the pshell count, then delete, then count again. The difference of the first and second counts should be the number of files removed(?).
 
Upvote 0
HI JonXL, You're right. I worried too much. The script works anyway.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,749
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top