Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I have the following path : Z:\logs\computername\username

I would like to check if all files within "username" are over 30 days old and if so delete the computername folder too (not only username subfolder).

Using forfiles I can delete username but not delete up to parent folder (Z:\logs\computername) :
@echo off
forfiles.exe /s /d -30 /p "Z:\logs\computername\username" /c "cmd.exe /c IF @isdir == TRUE rd /s /q @path"
exit

Do you have any idea how to do this ?

There is no relevant content in Z:\logs and Z:\logs\computername other than this username subfolder.

Thank you for your assistance.

What I have tried:

I have no specific idea of about how to achieve this.
Any help would greatly help me improve my script.
Thank you.
Posted
Updated 9-Mar-21 6:19am
v7

1 solution

You may like to look at Use Windows PowerShell to search for files | Scripting Blog[^]. Although it is PowerShell it could be used to create the list that you need.


This script will get you a count of all files more than 30 days old.
PowerShell
$subdir="Z:\logs\computername\username"
$today=Get-Date
$FindDate=$today.adddays(-31)

$num=@(Get-ChildItem -Path $subdir `
	-File -Recurse -ErrorAction SilentlyContinue `
	| Where-Object { $_.LastWriteTime -LT $FindDate }).Count

Write-Output "$num files found"
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900